drkxgs9358 2017-08-23 06:07
浏览 52
已采纳

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

  • 写回答

1条回答 默认 最新

  • duanboshi1472 2017-08-23 06:41
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系