PHP页面中的嵌套产品类别

PHP页面中的嵌套产品类别

问题描述:

I want to fetch all the values to show them in the PHP front-end page. I have two tables in database:

  1. Category: cat_id, cat_name
  2. 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:

  1. Notice: Undefined index: product_id in on line 14

  2. Notice: Undefined index: body in on line 15

  3. Notice: Undefined index: price in on line 16

You seem to have 3 tables rather as 2? (the brands). And you seem to have a lot more keys in your result array as fields in your tables. So that made debugging not that easy. But that aside I think it's because you store your product in a different format. Take a look at this part of your code:

$product1[$row[2]][$row[0]] = array(
  'product_id' => $row[0],
  'product_name' => $row[1],
  'price' => $row[5]
);

You key the array by index 2 and index 0 of your row.

Secondly this part of your code

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);
        }

The $tmp variable is nowhere used, so it's strange this is not throwing an error to you.

For the body error, I don't see it used in your code so you're in the black for that one.

So using the following fixed code of you original question:

$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);
    }

And used this array as my input:

$result = array(
  array(
    '1', // p id
    '2', // p cat id
    'product category_name', // p cat name
    'product_x', // p name
    '100', // p price
    'category category name', // c name
    'x', // ?
    'x', // ?
    'x' // ?
  ),
);

I get the following array structure in the $product2 variable.

array (
  'product category_name' => 
  array (
    'cat_id' => 'product category_name',
    'cat_name' => 'x',
    'product' => 
    array (
      1 => 
      array (
        'product_id' => '1',
        'product_name' => '2',
        'price' => 'category category name',
      ),
    ),
  ),
)

TLDNR;

So conclusion, your 'product' key in the array is also an array. So you'll need to do another foreach like this: foreach ($row['product'] as $product => $pid) { ... } over them, so your echo's look like this then:

<h2><?php echo $product['product_id']?></h2>
<h2><?php echo $product['product_name']?></h2>
<p><?php echo  $product['price']?></p>