2015-05-07 43 views
0

我有一個用於我的網站的導航菜單,我使用鏈接數組來構建CodeIgniter的解析器類。當我想添加下拉菜單時,會出現問題。第一級變量對將會很好,除了它總是在列表項中顯示一個子ul元素,而不管是否存在子菜單鏈接。在CodeIgniter解析器類中添加額外的解析數組級別

當我使用這個數組時,子菜單的ul元素設置正確,但子菜單的鏈接都沒有解析,因爲它缺少這個額外的級別。我如何修改CI解析器類來解析額外的數組級別?

這是我的菜單的PHP的數組:

$data['navigation_links'] = array(
      array(
       'href' => site_url('home'), 
       'title' => 'My Home Page', 
       'target' => '', 
       'text' => 'Home', 
       'submenu' => array() 
       ), 
      array(
       'href' => site_url('blog'), 
       'title' => 'My Blog', 
       'target' => '', 
       'text' => 'Blog', 
       'submenu' => array() 
      ), 
      array(
       'href' => site_url('portfolio'), 
       'title' => 'My Portfolio', 
       'target' => '', 
       'text' => 'Portfolio', 
       'submenu' => array(
        'submenu_links' => array(
         array(
          'sub_href' => site_url('portfolio/gallery'), 
          'sub_title' => 'Photography Gallery', 
          'sub_target' => '', 
          'sub_text' => 'Photography Gallery' 
         ), 
         array(
          'sub_href' => site_url('portfolio/web-projects'), 
          'sub_title' => 'Web Projects', 
          'sub_target' => '', 
          'sub_text' => 'Web Projects' 
         ) 
        ) 
       ) 
      ), 
      array(
       'href' => site_url('services'), 
       'title' => 'My Services', 
       'target' => '', 
       'text' => 'Services', 
       'submenu' => array() 
      ), 
      array(
       'href' => site_url('resume'), 
       'title' => 'My Resume', 
       'target' => '', 
       'text' => 'Resume', 
       'submenu' => array() 
      ), 
      array(
       'href' => site_url('about'), 
       'title' => 'About Me', 
       'target' => '', 
       'text' => 'About', 
       'submenu' => array() 
      ), 
      array(
       'href' => site_url('contact'), 
       'title' => 'Contact Me', 
       'target' => '', 
       'text' => 'Contact', 
       'submenu' => array() 
      ) 
     ); 

這是視圖文件的菜單部分:

<nav class="container"> 
         <ul> 
          {navigation_links} 
          <li><a href="{href}" title="{title}"{target}>{text}</a> 
           {submenu} 
           <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel"> 
            {submenu_links} 
            <li><a href="{sub_href}" title="{sub_title}"{sub_target}>{sub_text}</a></li> 
            {/submenu_links} 
           </ul>{/submenu}</li> 
          {/navigation_links} 
         </ul> 
        </nav> 

這是HTML輸出:

<nav class="container"> 
        <ul> 

         <li><a href="http://localhost/scotthlacey/index.php/home" title="My Home Page">Home</a> 
          </li> 

         <li><a href="http://localhost/scotthlacey/index.php/blog" title="My Blog">Blog</a> 
          </li> 

         <li><a href="http://localhost/scotthlacey/index.php/portfolio" title="My Portfolio">Portfolio</a> 

          <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel"> 
           {submenu_links} 
           <li><a href="{sub_href}" title="{sub_title}"{sub_target}>{sub_text}</a></li> 
           {/submenu_links} 
          </ul></li> 

         <li><a href="http://localhost/scotthlacey/index.php/services" title="My Services">Services</a> 
          </li> 

         <li><a href="http://localhost/scotthlacey/index.php/resume" title="My Resume">Resume</a> 
          </li> 

         <li><a href="http://localhost/scotthlacey/index.php/about" title="About Me">About</a> 
          </li> 

         <li><a href="http://localhost/scotthlacey/index.php/contact" title="Contact Me">Contact</a> 
          </li> 

        </ul> 
       </nav> 

回答

0

嘗試將另一個級別的數組包裝到子菜單鏈接。因爲你的數據(你想要迭代的每個數組)都必須是多維數組才能被解析器迭代。

$data['navigation_links'] = array(
    array(
     'href' => site_url('home'), 
     'title' => 'My Home Page', 
     'target' => '', 
     'text' => 'Home', 
     'submenu' => array() 
     ), 
    array(
     'href' => site_url('blog'), 
     'title' => 'My Blog', 
     'target' => '', 
     'text' => 'Blog', 
     'submenu' => array() 
    ), 
    array(
     'href' => site_url('portfolio'), 
     'title' => 'My Portfolio', 
     'target' => '', 
     'text' => 'Portfolio', 
     'submenu' => array(
      array(
       'submenu_links' => array(
        array(
         'sub_href' => site_url('portfolio/gallery'), 
         'sub_title' => 'Photography Gallery', 
         'sub_target' => '', 
         'sub_text' => 'Photography Gallery' 
        ), 
        array(
         'sub_href' => site_url('portfolio/web-projects'), 
         'sub_title' => 'Web Projects', 
         'sub_target' => '', 
         'sub_text' => 'Web Projects' 
        ) 
       ) 
      ) 
     ) 
    ), 
    array(
     'href' => site_url('services'), 
     'title' => 'My Services', 
     'target' => '', 
     'text' => 'Services', 
     'submenu' => array() 
    ), 
    array(
     'href' => site_url('resume'), 
     'title' => 'My Resume', 
     'target' => '', 
     'text' => 'Resume', 
     'submenu' => array() 
    ), 
    array(
     'href' => site_url('about'), 
     'title' => 'About Me', 
     'target' => '', 
     'text' => 'About', 
     'submenu' => array() 
    ), 
    array(
     'href' => site_url('contact'), 
     'title' => 'Contact Me', 
     'target' => '', 
     'text' => 'Contact', 
     'submenu' => array() 
    ) 
); 

希望它對你有用。

+0

謝謝!它像一個魅力一樣工作! – ShoeLace1291