php 中的嵌套 foreach 仅显示最后一条记录..!

Nested foreach in php showing only last record..!

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

对此几乎不需要专家的帮助。

我的代码:

model/catalog/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;

        }   

controller/product/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'])

                );

            }

            }    

view/product/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 } ?>

问题已解决.. 我需要放 $数据['products_brand'] = 数组();

上面第一个 foreach 语句