2012-03-10 68 views
1

我正在製作插入站點的產品菜單。只有一個錯誤出現了,我無法弄清楚如何解決它。使用PHP和MYSQL故障動態產品菜單

我的代碼:

public function getProductList($type = NULL, $brand = NULL){ 
    if(empty($type) and empty($brand)){ 

    $types = $this->productmodel->getTypeList(); // Category as arraylist 
    $brands = $this->productmodel->getBrandList(); // Brandnames as arraylist 
    $products = $this->productmodel->getProduct(); // All available products 

    $menu = array(); 

    foreach($products as $product) { 
      [$product['brand_id']] 
      $menu[$product['type_id']] = array (
     'type' => array(
     'url' => base_url('index.php/productcontroller/getProductList/'.$product['type_id']), 
     'type_name' => $types[$product['type_id']] 
     ) 
    ); 

     // Brands 
     $menu[$product['type_id']]['brands'] = array (
     $product['brand_id']['brand'] => array(
     'url' => base_url('index.php/productcontroller/getProductList/'.$product['type_id'].'/'.$product['brand_id']), 
     'brand_name' => $brands[$product['brand_id']] 
     ) 
    ); 
} 
return $menu; 
} 

Futher解釋說:

我在我的數據庫中的三個表。 1.類型 - 帶有:type.id和type_name, 2.品牌 - 帶有:brand.id和brand_type, 3.產品 - 帶有:來自上面表格的帶有ID的產品的所有信息。

會發生什麼情況是隻顯示最新插入的產品。所有其他品牌都被覆蓋。我只想覆蓋重複的品牌名稱,因此我使用(品牌['id'])我如何做這項工作?我使用Codeigniter。

感謝您的時間和精力,

問候

+0

注: 只有最後BRAND_NAME將被顯示。該類型正常工作。 – Loed 2012-03-10 16:35:26

回答

0

您將要覆蓋每個迭代$menu[$type_id]['brands']陣列。我覺得你的意思 -

foreach($products as $product) { 

    $menu[$product['type_id']]['type'] = array (
     'url' => base_url('index.php/productcontroller/getProductList/'.$product['type_id']), 
     'type_name' => $types[$product['type_id']] 
    ); 

    // Brands 
    $menu[$product['type_id']]['brands'][$product['brand_id']]['brand'] = array(
     'url' => base_url('index.php/productcontroller/getProductList/'.$product['type_id'].'/'.$product['brand_id']), 
     'brand_name' => $brands[$product['brand_id']] 
    ); 

} 
+0

你是對的!非常感謝你。你能解釋我做錯了什麼嗎? – Loed 2012-03-10 18:44:07

+0

正如我在我的回答中所說的,你在每次迭代中覆蓋數組 - '$ menu ['type_id']] ['brands'] = array(' – nnichols 2012-03-10 18:48:52

+0

我沒有注意到,是否有一招幫助我識別將來的錯誤 – Loed 2012-03-10 18:55:32