2014-09-03 36 views
2

我在我的數據庫中有一個表,其中包含我所有的數據庫類別,我需要將其轉換爲多級菜單。表結構低於:codeigniter中的數據庫中的多級菜單

product_category_id | product_category_name | product_category_description | product_category_parent_id 
-------------------------------------------------------------------------------------------------------- 
1      test     ulghjbjjjh      NULL 
2      test2     yruktghkug      NULL 
3      test sub 1    yr5y346uij      1 
4      test sub sub 1   yfghvbnhtd      3 

使用I已經適應從一個在線教程(here)和研究的結果代碼將只顯示頂層(那些具有空的父ID)小時的功能。我確定這是sortMenu函數的問題,但是,我似乎無法解決這個問題。

這裏是一個提取數據我的模型功能:

public function getProductCategories() 
{ 
    $query = $this->db->get("tbl_product_categories"); 
    return $query->result_array(); 
} 

這裏是一個從索引功能,這反過來,調用私有create_list函數調用私有函數控制器:

class products extends CI_Controller { 

public function __construct() 
{ 
    parent::__construct(); 
    $this->load->model("products_model"); 
} 

public function index() 
{  
    $categories = $this->products_model->getProductCategories(); 
    echo $this->sortMenu($categories); 
    //print_r($this->products_model->getProductCategories()); 
} 

private function sortMenu($items) 
{ 
    // create an array to hold the references 
    $refs = array(); 

    // create and array to hold the list 
    $list = array(); 

    // loop over the results 
    //while($data = @mysql_fetch_assoc($result)) 
    foreach($items as $data) 
    { 
     // Assign by reference 
     $thisref = &$refs[ $data['product_category_id'] ]; 

     // add the the menu parent 
     $thisref['product_category_id'] = $data['product_category_parent_id']; 
     $thisref['product_category_title'] = $data['product_category_title']; 

     // if there is no parent id 
     if (is_null($data['product_category_parent_id'])) 
     { 
      $list[ $data['product_category_id'] ] = &$thisref; 
     } 
     else 
     { 
      $refs[ $data['product_category_id'] ]['children'][ $data['product_category_id'] ] = &$thisref; 
     } 

    } 
    print_r($list); 
    return $this->create_list($list); 
} 

/** 
* 
* Create a HTML list from an array 
* 
* @param array $arr 
* @param string $list_type 
* @return string 
* 
*/ 
private function create_list($arr) 
{ 
    $html = "\n<ul>\n"; 
    foreach ($arr as $key=>$v) 
    { 
     $html .= '<li>'.$v['product_category_title']."</li>\n"; 
     if (array_key_exists('children', $v)) 
     { 
      $html .= "<li>"; 
      $html .= create_list($v['children']); 
      $html .= "</li>\n"; 
     } 
     else{} 
    } 
    $html .= "</ul>\n"; 
    return $html; 
} 

} 

這讓我完全困惑:S有沒有人有任何想法?

+1

我已經回答了幾乎同樣的問題,請參見本http://stackoverflow.com/a/25376973/1208233 – Yang 2014-09-04 06:44:45

+0

要使它適用於您的情況,只需在*註釋後刪除兩行*第一步*在'foreach'內 – Yang 2014-09-04 06:46:02

+0

感謝您的幫助:) – 2014-09-04 10:27:31

回答

1

據我瞭解,

這條線:

$refs[ $data['product_category_id'] ]['children'][ $data['product_category_id'] ] = &$thisref; 

應該是這樣的:

$refs[ $data['product_category_parent_id'] ]['children'][ $data['product_category_id'] ] = &$thisref; 
+1

工作,謝謝:) – 2014-09-04 10:27:14

+0

多級分類多選CodeIgniter https://github.com/hamedhossani/Multi_level_category_multiselect_CodeIgniter – 2017-02-21 17:41:06