drfqfuhej48511519 2015-08-27 09:00
浏览 14

使用Codeigniter的内容动态菜单

I would like to have the following json structure. I have generate the half part but don't know how to put contents into menu item which is not a top menu.

my tables structure:

menu Table structure

Types table structure

and here is my controller code for generating the menu

public function index()
{
    $data['menus'] = $this->main_menu_model->get_all_menus3();
    $result=$this->build_menu($data['menus'],0);
    echo '<pre>'; print_r($result); die;
}

function build_menu($lists,$parent_id)
{
    // return an array of items with parent = $parentId
      $result = array();
      foreach ($lists as $item) 
      {
        if ($item['parent'] == $parent_id) 
        {
          $newItem = $item;
          $newItem['contents'] = $this->build_menu($lists, $newItem['id']);
          $result[] = $newItem;
        }
      }
      if (count($result) > 0) return $result;
      return null;
}

its give me this result :

[0] => Array
    (
        [id] => 1
        [title] => About Sotogrande
        [bg_image] => bg-4.jpg
        [position] => 1
        [parent] => 0
        [tbl_name] => 
        [contents] => Array
            (
                [0] => Array
                    (
                        [id] => 2
                        [title] => History
                        [bg_image] => bg-92.jpg
                        [position] => 1
                        [parent] => 1
                        [tbl_name] => history
                        [contents] => 
                    )

                [1] => Array
                    (
                        [id] => 3
                        [title] => Photos
                        [bg_image] => bg-8.jpg
                        [position] => 2
                        [parent] => 1
                        [tbl_name] => photos
                        [contents] => 
                    )

                [2] => Array
                    (
                        [id] => 4
                        [title] => Activities
                        [bg_image] => images3.jpg
                        [position] => 3
                        [parent] => 1
                        [tbl_name] => activities
                        [contents] => 
                    )

            )

I want to put the actual contents get from table into the contents= [] where its a sub menu not for top menu[parent]. I have table for every menu item. but in some sub menu i have type field and if its a type== gallery then i have a gallery table so the contents= [result of gallery table].

App.json

  • 写回答

1条回答 默认 最新

  • douwei6478 2015-08-27 12:24
    关注

    not sure if this question is related to html/css to display results or the issue is passing data from controller to a view or the problem is sql statement elaborate more

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题