2

データベースにすべてのデータベース カテゴリを含むテーブルがあり、これをマルチレベル メニューに変換する必要があります。テーブル構造は次のとおりです。

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

オンライン チュートリアル (ここ) から採用した関数と何時間もの調査を使用して、結果のコードはトップ レベル (親 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 誰かアイデアはありますか?

4

1 に答える 1

1

私が理解していることから、

この行:

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

これである必要があります:

$refs[ $data['product_category_parent_id'] ]['children'][ $data['product_category_id'] ] = &$thisref;
于 2014-09-04T06:34:16.213 に答える