I want to fetch all the values to show them in the PHP front-end page. I have two tables in database:
- Category:
cat_id
,cat_name
- Product:
pro_id
,cat_id
,cat_name
,pro_name
,price
My product.php
code (front-end PHP file):
<div class="content">
<div class="content_top">
<?php
$product = new Product();
$getProduct = $product->getAllProduct();
if (!empty($getProduct)) {
foreach($getProduct as $row) {
?>
<div class="heading">
<h3>Latest from <?php echo $row['cat_name']; ?></h3>
</div>
</div>
<div class="section group">
<h2><?php echo $row['product']['product_id']?></h2>
<h2><?php echo $row['product']['product_name']?></h2>
<p><?php echo $row['product']['price']?></p>
</div>
<?php
}
}
?>
</div>
My Product.php
code (back-end classes file):
public function getAllProduct() {
$query = "SELECT p.*, c.cat_name, b.brand_name
FROM product as p, category as c, brand as b
WHERE p.cat_id = c.cat_id AND p.brand_id= b.brand_id
ORDER BY p.product_id DESC";
$data = $this->db->select($query);
$result = $data->fetch_all();
$product = array();
$product1 = array();
foreach ($result as $row) {
$product[$row[2]] = array(
'cat_id' => $row[2],
'cat_name' => $row[8]
);
$product1[$row[2]][$row[0]] = array(
'product_id' => $row[0],
'product_name' => $row[1],
'price' => $row[5]
);
}
$product2 = array();
foreach ($product as $id => $val) {
$product2[$id]= array(
'cat_id' => $val['cat_id'],
'cat_name' => $val['cat_name'],
'product' => $product1[$id],
);
array_push($product2, $tmp);
}
return $product2;
}
The output is showing three errors like below:
Notice: Undefined index: product_id in on line 14
Notice: Undefined index: body in on line 15
Notice: Undefined index: price in on line 16