2013-06-24 39 views
0

我正在cakephp項目中工作。實際上我有表menu_items在數據庫中,其中包含id,parent_id,title,urlis_active。我的父母菜單項正常顯示,但問題出現在與parent_id有關的子菜單中。保留第一列(即id)的值並與同一表中另一列(即parent_id)的所有值匹配php

foreach($menusitems as $menu) 
{ 
    if($menu['MenuItem']['is_active'] == true) 
    { 
      echo $this->Html->link($menu['MenuItem']['title'], array('controller' => 'pages', action' => $menu['MenuItem']['url'])); 

     foreach ($menu['MenuItem'] as $temp) 
     { 
      if($id == $temp['parent_id']) 
      { 
       echo "match"; 
      } 
     } 

    } 

} 

這不是一個正確的代碼,我只是想匹配特定的ID(例如:1)想匹配與第一父ID的每個值去那麼我要在那裏找到匹配的價值,我必須打印值在子菜單列表中的標題以及與下一個標識符相同的過程(例如:2)等等。但我與父母id的匹配ID不起作用。

When i use 

'echo "<pre>" 
print_r($menus); 
echo "</pre>" 
' 
'Array 
(
    [id] => 1 
    [parent_id] => 
    [title] => Home 
    [url] => home 
    [is_active] => 1 
) 

Array 
(
    [id] => 2 
    [parent_id] => 
    [title] => Profile 
    [url] => 
    [is_active] => 1 
) 

Array 
(
    [id] => 3 
    [parent_id] => 
    [title] => Home 
    [url] => home 
    [is_active] => 1 
) 

Array 
(
    [id] => 4 
    [parent_id] => 2 
    [title] => Bussiness Profile 
    [url] => bussiness_profile 
    [is_active] => 1 
)' 

我只是想顯示商業作爲配置文件下的子菜單。如果id與1相等,則我匹配。如果任何父母身份證在我的案例中存在,如「商務資料」,那麼該項目應顯示爲配置文件的子菜單。

+1

是什麼「這是不是一個正確的代碼」是什麼意思?聽起來你想用[find('threaded')](http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-threaded) - 很難說。 – AD7six

+0

我編輯了一些代碼,希望對你來說足夠了。 – user2314433

回答

0

find ('threaded')爲你做了訣竅。 使用$this->MenuItem->find('threaded', array('conditions' => array('MenuItem.is_active')))在你的控制器,應該返回你的陣列看起來像

[0] => array(
    ['MenuItem'] => array(
     [id] => 1 
     [parent_id] => 
     [title] => Home 
     [url] => home 
     [is_active] => 1 
    ), 
), 
[1] => array(
    ['MenuItem'] => array(
     [id] => 2 
     [parent_id] => 
     [title] => Profile 
     [url] => home 
     [is_active] => 1 
    ), 
    ['children'] => array(
      [MenuItem] => array(
       [id] => 4 
       [parent_id] => 2 
       [title] => Bussiness Profile 
       [url] => bussiness_profile 
       [is_active] => 1 
     ) 
    ) 
. 
. 
) 

所以,在你看來:

<ul class="menu"> 
<?php foreach($menuitems as $menu_item_parent): ?> 
    <li class="parent"> 
     //$this->Html->link .. (with $menu_item_parent) 

     <?php if(!empty($menu_item_parent['children'])): ?> 
       <ul class="submenu"> 
       <?php foreach($menuitems['children'] as $menu_item_child): ?> 
        <li class="child"> 
          //$this->Html->link .. (with $menu_item_child) 
        </li> 
       <?php endforeach; ?> 
       </ul> 
     <?php endif; ?> 
    </li> 
<?php endforeach; ?> 
</ul> 
+0

感謝Christoph爲我工作。 – user2314433

相關問題