在PHP中嵌套的foreach只显示最后的记录..!

时间:2016-12-17 15:25:35

标签: php join opencart opencart2.x

在Opencart的产品页面上,我想按类别显示相同的品牌产品列表。 使用下面的代码显示特定品牌产品的类别。但产品并未在类别下显示。产品仅在一个类别下显示。添加结果页面的屏幕截图。

enter image description here

为此需要专家的帮助。

我的代码:

模型/目录/ product.php

    public function getManufacturerCategories($manufacturer_id) {
    $query = $this->db->query("
    SELECT 
    DISTINCT c.category_id,cd.name
    FROM
    ". DB_PREFIX . "manufacturer m 
    LEFT JOIN ". DB_PREFIX. "product p ON (m.manufacturer_id = p.manufacturer_id)
    LEFT JOIN ". DB_PREFIX. "product_to_category p2c ON (p2c.product_id = p.product_id)
    LEFT JOIN ". DB_PREFIX. "category c ON (c.category_id = p2c.category_id)
    LEFT JOIN ". DB_PREFIX. "category_description cd ON (cd.category_id = p2c.category_id)
    WHERE
    p.status = 1
    AND m.manufacturer_id = '".(int)$manufacturer_id."'
    AND c.status= 1
    ");

    return $query->rows;

    }

    public function getProductSameBrand($manufacturer_id,$product_id,$cat_id,$cat_name) {

        $product_data = array();

        $query = $this->db->query("
        SELECT 
        DISTINCT p.manufacturer_id,p.product_id,p2c.category_id,cd.name
        FROM 
        " . DB_PREFIX . "product p
         LEFT JOIN ". DB_PREFIX. "product_to_category p2c ON (p2c.product_id = p.product_id)
         LEFT JOIN ". DB_PREFIX. "category_description cd ON (cd.category_id = p2c.category_id)
        WHERE        
        cd.name = '".$cat_name."'
        AND p.manufacturer_id = '".(int)$manufacturer_id."'
        "
        );

            foreach ($query->rows as $result2) {

            $product_data[$result2['product_id']] = $this->getProduct($result2['product_id']); 

            }    

            return $product_data;

        }   

控制器/产品/ product.php

   $data['category_brand'] = array(); 

        if((int)$product_info['manufacturer_id'] > 0){ 
        $results = $this->model_catalog_product->getManufacturerCategories($product_info['manufacturer_id']);
        }

         foreach ($results as $result) { 
             $cat_name =    $result['name'];
             $cat_id = $result['category_id'];

                $data['category_brand'][] = array(               
                    'category_id' => $cat_id,
                    'name'         =>  $cat_name
                );


            $data['products_brand'] = array();


             if((int)$product_info['manufacturer_id'] > 0){ 

        $results = $this->model_catalog_product->getProductSameBrand($product_info['manufacturer_id'],$this->request->get['product_id'],$cat_id,$cat_name);

                } 

            foreach ($results as $result2) {


                $data['products_brand'][] = array(

                    'product_id' => $result2['product_id'], 
                    'name'         => $result2['name'],
                    'catname'      => $cat_name,
                    'href'         => $this->url->link('product/product', 'product_id=' . $result2['product_id'])

                );

            }

            }    

查看/产品/ product.tpl

     <?php foreach ($category_brand as $cat) { ?>
                            <h4><?php echo $cat['name']; ?></h4>  
                          <?php foreach ($products_brand as $product) { ?>
                          <?php if($cat['name']==$product['catname']){?>
                         <a href="<?php   echo $product['href']; ?>"> <?php echo $product['name']; ?></a><br/>
                         <?php } ?>
                         <?php } ?>
                         <hr/> 
                    <?php } ?>

1 个答案:

答案 0 :(得分:0)

问题解决了.. 我需要放  $ data ['products_brand'] = array();

高于第一个foreach语句