2013-06-24 33 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

相关问题