codeigniter中数据库的多级菜单

时间:2014-09-03 14:58:17

标签: php list codeigniter drop-down-menu adjacency-list-model

我的数据库中有一个包含所有数据库类别的表,我需要将其转换为多级菜单。表格结构如下:

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

使用我从在线教程(here)调整的函数和研究时间,生成的代码将只显示顶级(父ID为null的那些)。我确定它是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 个答案:

答案 0 :(得分:1)

据我了解,

这一行:

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

应该是这样的:

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