如何解决 codeigniter 中的动态多个下拉菜单问题?
我有一个通过从数据库中获取标题动态生成的菜单。我通过添加列 p_id 为子页面设置了相同的表。
我的代码看起来像这样。
private $Section = array();
function _menu($root){
$this->db->select('id,title,p_id');
$this->db->Where('p_id',$root);
$this->db->Where('status','active');
$this->db->Where('type','page');
$this->db->order_by('sort');
$query = $this->db->get('pages');
foreach ($query->result() as $row)
{
$this->Section[] = '<ul>';
$this->Section[] = '<li>';
$this->Section[] = anchor('site/page/'.$row->id.'#cont_main',$row->title);
$this->_menu($row->id);
$this->Section[] = '</li>';
$this->Section[] = '</ul>';
}
$query->free_result();
return $this->Section;
}
//get menu tree
function MenuTree(){
return $this->_menu(0);
}
在上面的 _menu() 中,如果导航有两个或更多子项,那么它不会显示,因为它填充的源提供了额外的 和
。来源看起来像这样<ul>
<li><a href="#">menu 1</a>
<ul>
<li><a>menu 1 sub1</a></li>
</ul>
<ul>
<li><a>menu 1 sub2</a></li>
</ul>
</li>
While I am assuming to get:<ul>
<li><a href="#">menu 1</a>
<ul>
<li><a>menu 1 sub1</a></li>
<li><a>menu 1 sub2</a></li>
</ul>
</li>
**edited** I have to add a menu according to the child element. Image comes in all menus which don't have child too due to extra ```<ul>
<li><a href="#">menu 1</a>
<ul>
<li><a>menu 1 sub1</a>
<ul></ul>
</li>
<li><a>menu 1 sub2</a></li>
</ul>
</li>
任何想法将不胜感激。
I have a menu generated dynamically by taking title from the database. I have set the same table for sub pages by adding a column p_id.
And my code looks like this.
private $Section = array();
function _menu($root){
$this->db->select('id,title,p_id');
$this->db->Where('p_id',$root);
$this->db->Where('status','active');
$this->db->Where('type','page');
$this->db->order_by('sort');
$query = $this->db->get('pages');
foreach ($query->result() as $row)
{
$this->Section[] = '<ul>';
$this->Section[] = '<li>';
$this->Section[] = anchor('site/page/'.$row->id.'#cont_main',$row->title);
$this->_menu($row->id);
$this->Section[] = '</li>';
$this->Section[] = '</ul>';
}
$query->free_result();
return $this->Section;
}
//get menu tree
function MenuTree(){
return $this->_menu(0);
}
In above _menu() if a nav has two or more children then it doesn't display because the source populated by it gives extra</ul>
and <ul>
. The source looks like this
<ul>
<li><a href="#">menu 1</a>
<ul>
<li><a>menu 1 sub1</a></li>
</ul>
<ul>
<li><a>menu 1 sub2</a></li>
</ul>
</li>
While I am assuming to get:
<ul>
<li><a href="#">menu 1</a>
<ul>
<li><a>menu 1 sub1</a></li>
<li><a>menu 1 sub2</a></li>
</ul>
</li>
**edited**
I have to add a menu according to the child element. Image comes in all menus which don't have child too due to extra ```
``` if I use ul before the loop. The source looks like this.
<ul>
<li><a href="#">menu 1</a>
<ul>
<li><a>menu 1 sub1</a>
<ul></ul>
</li>
<li><a>menu 1 sub2</a></li>
</ul>
</li>
Any idea would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在 foreach 循环之外添加
ul
并确保您已经有了结果!You need to add your
ul
outside the foreach loop AND make sure that you already have results!您正在使用的
当然总是显示
ul
因为它为每个元素执行此操作。您需要将其关闭块放置在循环之外。如果您希望仅在有多个父级时触发它,则应根据您需要的一切来检查此条件并做出相应反应的 IF 语句。
剩下的问题似乎是
ul
的放置。您可以完全删除上面提到的 ul 以获得内部循环。
You are using
which of course always displays an
ul
since it does it FOR EACH element. You need to placeand it's closing block outside the loop. If you want it only triggered if there are more than one parent, an IF statement that check's this and reacts accordingly should by everything you need.
The problem that remains seems to be the placing of the
ul
. You could doand remove the mentioned ul from above completely to just get the inner loops.