Php CodeIgniter-3级菜单阵列
我的主要开发人员不在,所以我正在扩展我的能力,因为我不是一个程序员,能够胜任某个级别,但并不出色,所以请容忍我 我们有一个两级菜单生成器。它需要第三级。框架是CI 数据库表为“页面导航” 字段=navid(唯一)| pageid | parentid |条目|类型|目标|顺序 与名为“pages”的表相关- 我加入页面和导航页面以获得URL和标题 条目类型为1、2或3(顶层、子项或菜单分隔符(水平规则/菜单项之间的分隔符) 数据库查找是:Php CodeIgniter-3级菜单阵列,php,arrays,codeigniter,Php,Arrays,Codeigniter,我的主要开发人员不在,所以我正在扩展我的能力,因为我不是一个程序员,能够胜任某个级别,但并不出色,所以请容忍我 我们有一个两级菜单生成器。它需要第三级。框架是CI 数据库表为“页面导航” 字段=navid(唯一)| pageid | parentid |条目|类型|目标|顺序 与名为“pages”的表相关- 我加入页面和导航页面以获得URL和标题 条目类型为1、2或3(顶层、子项或菜单分隔符(水平规则/菜单项之间的分隔符) 数据库查找是: $menu = $this->db -
$menu = $this->db
->select('P.id, P.title, P.url, N.entry_type, N.target, N.order, N.parentid, N.navid')
->join('pages P', 'P.id = N.pageid', 'left')
->order_by('N.parentid, N.order')
->get('pages_nav N')
->result_array();
然后我从中得到一个数组(都在模型中),循环并传回父级或子级,如下所示:
if(!$menu) return [];
$menu_out = [];
foreach($menu as $item) {
if($item['parentid'] == 0) {
$menu_out[$item['id']] = ['details'=>$item, 'children'=>[] ];
} else {
$menu_out[$item['parentid']]['children'][] = $item;
}
}
return $menu_out;
<?php if(!empty($menu_items)): ?>
<?php foreach($menu_items as $item): ?>
[ PRINT OUT ROWS: title, URL, Order etc]
<?php if(count($item['children'])): ?>
<?php foreach($item['children'] as $child): ?>
[ PRINT OUT ROWS of child items: title, URL, Order etc]
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php else: ?>
[ no entries found text ]
<?php endif; ?>
在控制器中,我只是调用模型并将$data['menu_items']作为$data传递给视图
在视图中,我在一个foreach中有一个foreach来打印父母和任何孩子,如下所示:
if(!$menu) return [];
$menu_out = [];
foreach($menu as $item) {
if($item['parentid'] == 0) {
$menu_out[$item['id']] = ['details'=>$item, 'children'=>[] ];
} else {
$menu_out[$item['parentid']]['children'][] = $item;
}
}
return $menu_out;
<?php if(!empty($menu_items)): ?>
<?php foreach($menu_items as $item): ?>
[ PRINT OUT ROWS: title, URL, Order etc]
<?php if(count($item['children'])): ?>
<?php foreach($item['children'] as $child): ?>
[ PRINT OUT ROWS of child items: title, URL, Order etc]
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php else: ?>
[ no entries found text ]
<?php endif; ?>
[打印行:标题、URL、顺序等]
[打印出子项的行:标题、URL、顺序等]
[在文本中未找到任何条目]
我不想更改数据库,除非在必要时添加一列。我不想完全重新构建代码,也不需要超过3级的递归(100%确定)所以,我需要的是一个无压力的,简单的编辑,我必须允许第三级,但无论我尝试什么,我都失败得很惨,所以我在寻找一些方向
我一直在寻找的是第三级的条目类型=4作为标识符,但我正在努力
编辑--现在将我的视图添加到帮助中:
<?php if(!empty($menu_items)): ?>
<?php foreach($menu_items as $item): ?>
<?php
if($item['details']['target']==0)
$link_target = "Current";
else
$link_target = "New";
?>
<tr class="parent">
<td><span class="anchor" data-id="<?php echo $item['details']['id']; ?>"><i class="fa fa-chevron-down <?php echo count($item['children']) ? '' : 'off'; ?>"></i> <?php echo $item['details']['title']; ?></span></td>
<td><?php echo $item['details']['url']; ?></td>
<td><?php echo $item['details']['order']; ?></td>
<td><?php echo $link_target; ?> Tab</td>
</tr>
<?php if(count($item['children'])): ?>
<?php foreach($item['children'] as $child): ?>
<?php
if($child['target']==0)
$link_target = "Current";
else
$link_target = "New";
?>
<tr class="child">
<?php if($child['entry_type'] == 3): ?>
<td colspan>-- DIVIDER --</td>
<?php else: ?>
<td><?php echo $child['title']; ?></td>
<?php endif; ?>
<td><?php echo $child['url']; ?></td>
<td><?php echo $child['order']; ?></td>
<td><?php echo $link_target; ?> Tab</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="6">No navigation entries</td>
</tr>
<?php endif; ?>
标签
--分隔器--
标签
没有导航条目
这是数据库中的条目。最后一个是第三级条目,与上面两行(第二级)相关,与上面两行(父级/顶级)相关
因此有3个顶级项,最后一个有3个子项(1是分隔线),其中一个子项有一个子项(或子项本身)。我想你会看到如下情况: 模型 控制器 现在,在您的视图中,您应该可以使用一个新的级别
<?php if(!empty($menu_items)): ?>
<?php foreach($menu_items as $item): ?>
<?php $link_target = $item['details']['target']==0 ? "Current" : "New"; ?>
<tr class="parent">
<td>
<span class="anchor" data-id="<?php echo $item['details']['id']; ?>">
<i class="fa fa-chevron-down <?php echo count($item['children']) ? '' : 'off'; ?>"></i> <?php echo $item['details']['title']; ?>
</span>
</td>
<td><?php echo $item['details']['url']; ?></td>
<td><?php echo $item['details']['order']; ?></td>
<td><?php echo $link_target; ?> Tab</td>
</tr>
<?php if(count($item['children'])): ?>
<?php foreach($item['children'] as $child): ?>
<?php $link_target = $child['target']==0 ? "Current" : "New"; ?>
<tr class="child">
<?php if($child['entry_type'] == 3): ?>
<td colspan>-- DIVIDER --</td>
<?php else: ?>
<td><?php echo $child['title']; ?></td>
<?php endif; ?>
<td><?php echo $child['url']; ?></td>
<td><?php echo $child['order']; ?></td>
<td><?php echo $link_target; ?> Tab</td>
</tr>
<!-- ¡¡¡NEW LEVEL!!! -->
<!-- Not really sure where you want it, so I just created a new row,
with a colored background so you can "see" the change -->
<? php if (!empty($child['level3'])): ?>
<?php foreach($child['level3'] as $lvl3): ?>
<?php $link_target = $lvl3['target']==0 ? "Current" : "New"; ?>
<tr style="background: yellow;">
<td><?php echo $lvl3['title']; ?></td>
<td><?php echo $lvl3['url']; ?></td>
<td><?php echo $lvl3['order']; ?></td>
<td><?php echo $link_target; ?> Tab</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="6">No navigation entries</td>
</tr>
<?php endif; ?>
标签
--分隔器--
标签
标签
没有导航条目
我在一堆我自己的真实作品和这件事之间来回工作,所以如果事情不太对劲,请发表评论,我会设法解决它!祝你好运!我想你看到的是如下情况: 模型 控制器 现在,在您的视图中,您应该可以使用一个新的级别
<?php if(!empty($menu_items)): ?>
<?php foreach($menu_items as $item): ?>
<?php $link_target = $item['details']['target']==0 ? "Current" : "New"; ?>
<tr class="parent">
<td>
<span class="anchor" data-id="<?php echo $item['details']['id']; ?>">
<i class="fa fa-chevron-down <?php echo count($item['children']) ? '' : 'off'; ?>"></i> <?php echo $item['details']['title']; ?>
</span>
</td>
<td><?php echo $item['details']['url']; ?></td>
<td><?php echo $item['details']['order']; ?></td>
<td><?php echo $link_target; ?> Tab</td>
</tr>
<?php if(count($item['children'])): ?>
<?php foreach($item['children'] as $child): ?>
<?php $link_target = $child['target']==0 ? "Current" : "New"; ?>
<tr class="child">
<?php if($child['entry_type'] == 3): ?>
<td colspan>-- DIVIDER --</td>
<?php else: ?>
<td><?php echo $child['title']; ?></td>
<?php endif; ?>
<td><?php echo $child['url']; ?></td>
<td><?php echo $child['order']; ?></td>
<td><?php echo $link_target; ?> Tab</td>
</tr>
<!-- ¡¡¡NEW LEVEL!!! -->
<!-- Not really sure where you want it, so I just created a new row,
with a colored background so you can "see" the change -->
<? php if (!empty($child['level3'])): ?>
<?php foreach($child['level3'] as $lvl3): ?>
<?php $link_target = $lvl3['target']==0 ? "Current" : "New"; ?>
<tr style="background: yellow;">
<td><?php echo $lvl3['title']; ?></td>
<td><?php echo $lvl3['url']; ?></td>
<td><?php echo $lvl3['order']; ?></td>
<td><?php echo $link_target; ?> Tab</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="6">No navigation entries</td>
</tr>
<?php endif; ?>
标签
--分隔器--
标签
标签
没有导航条目
我在一堆我自己的真实作品和这本书之间来回工作,因此,如果有些东西不太对劲,请发表评论,我会尝试修复它!祝你好运!我想,更改将从你的第二个代码示例(菜单循环)开始。目前,它只创建了两个级别。第一个级别基于查找所有父id=0的位置。这将创建“主菜单”.2级是该项的子项。现在,快速提问,也许我误解了您在下面键入的内容,但您要添加的第3级到底是什么?它是另一个DB值,还是来自php中与子项或父项相关的其他内容?仅供参考,我确定,举例来说,它将是前端的一个菜单,如下所示:关于我们>公司>我们当前的历史parentid与pageid相关。输入类型只是为了帮助前端了解我们显示的内容。虽然我提到了前端,但所有这些都只是后端(菜单生成器)目前,正如它本质上所显示的一样。部分问题只是对if-else模型的理解有一半。我觉得在else中,我应该说如果entry\u type=4,然后以某种方式传回子级和子级,其他只是子级,并在视图中捕捉到这一点,但它正在融化我的非程序员大脑。我要开始一个a由于你不是一个全职程序员,我将从打破这一点开始
<?php if(!empty($menu_items)): ?>
<?php foreach($menu_items as $item): ?>
<?php $link_target = $item['details']['target']==0 ? "Current" : "New"; ?>
<tr class="parent">
<td>
<span class="anchor" data-id="<?php echo $item['details']['id']; ?>">
<i class="fa fa-chevron-down <?php echo count($item['children']) ? '' : 'off'; ?>"></i> <?php echo $item['details']['title']; ?>
</span>
</td>
<td><?php echo $item['details']['url']; ?></td>
<td><?php echo $item['details']['order']; ?></td>
<td><?php echo $link_target; ?> Tab</td>
</tr>
<?php if(count($item['children'])): ?>
<?php foreach($item['children'] as $child): ?>
<?php $link_target = $child['target']==0 ? "Current" : "New"; ?>
<tr class="child">
<?php if($child['entry_type'] == 3): ?>
<td colspan>-- DIVIDER --</td>
<?php else: ?>
<td><?php echo $child['title']; ?></td>
<?php endif; ?>
<td><?php echo $child['url']; ?></td>
<td><?php echo $child['order']; ?></td>
<td><?php echo $link_target; ?> Tab</td>
</tr>
<!-- ¡¡¡NEW LEVEL!!! -->
<!-- Not really sure where you want it, so I just created a new row,
with a colored background so you can "see" the change -->
<? php if (!empty($child['level3'])): ?>
<?php foreach($child['level3'] as $lvl3): ?>
<?php $link_target = $lvl3['target']==0 ? "Current" : "New"; ?>
<tr style="background: yellow;">
<td><?php echo $lvl3['title']; ?></td>
<td><?php echo $lvl3['url']; ?></td>
<td><?php echo $lvl3['order']; ?></td>
<td><?php echo $link_target; ?> Tab</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="6">No navigation entries</td>
</tr>
<?php endif; ?>