【发布时间】:2015-03-11 22:17:24
【问题描述】:
我正在 Codeiginter 上创建一个从 MYSQL DB 检索的动态导航菜单 这是导航的说明
父表
|---parent_id---link---title-----|
|---1-----------#------Parent1---|
|---2-----------#------Parent2---|
|---3-----------#------Parent3---|
儿童桌
|---children_id---fk_parent_id---title-------|
|--------1---------2-------------children1---|
|--------2---------2-------------children2---|
|--------3---------2-------------children3---|
子子表(子子表会在子子被选中时显示)
|---sub_child_id--- fk_children_d ------title-------|
|------1--------------2-------------Sub Children1---|
|------2--------------2-------------Sub Children2---|
|------3--------------2-------------Sub Children3---|
导航菜单
|---------|---------|--------|
| Parent1 |Parent2 |Parent3 |
|---------|===++====|--------|
|=============++======================================|
|---|Children1|-----|Children2|-----|Childrent3|------|
|-===================++===============================|
|-----------------|Sub Childrent1|--------------------|
|-----------------|Sub Childrent2|--------------------|
|-----------------|Sub Childrent3|--------------------|
注意:作为我下面的代码,它只适用于儿童,但它不显示在子 儿童
这是我的模型函数,它为孩子和父母创建一些条件
function menu() {
$this->db->select("*");
$this->db->from("menu_parents");
$q = $this->db->get();
$final = array();
if ($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$this->db->select("*");
$this->db->from("menu_childrent");
$this->db->where("fk_p_id", $row->parents_id);
$q = $this->db->get();
if ($q->num_rows() > 0) {
$row->children = $q->result();
foreach ($q->result() as $srow){
//This is the session problem which I want to select for my sub children from menus_children
$this->db->select("*");
$this->db->from("menu_sub_childrent");
$this->db->where("fk_m_child_id", $srow->m_child_id);
$q = $this->db->get();
if($q->num_rows()>0){
$row->sub_children = $q->result();
}
}
}
array_push($final, $row);
}
}
return $final;
}
这是我的看法
<ul class="nav navbar-nav">
<?PHP foreach($menus as $menu): ?>
<li class="dropdown megamenu-fullwidth">
<a data-toggle="dropdown" class="dropdown-toggle" href="#"> <?PHP echo $menu->title;?> <?PHP if(isset($menu->children)):?><b class="caret"></b><?PHP endif;?></a>
<?PHP if(isset($menu->children)):?>
<ul class="dropdown-menu">
<?PHP foreach($menu->children as $child): ?>
<li class="megamenu-content">
<ul class="col-lg-2 col-sm-2 col-md-2">
<li class="no-border"><p><strong><?PHP echo $child->title;?></strong></p></li>
<?PHP if(isset($menu->sub_children)):?>
<?PHP foreach($menu->sub_children as $sub_children): ?>
<li><a href="#"> <?PHP echo $sub_children->title; ?><br></a></li>
<?PHP endforeach; ?>
<?PHP endif;?>
</ul>
</li>
<?PHP endforeach; ?>
</ul>
<?PHP endif; ?>
</li>
<?PHP endforeach; ?>
</ul>
这是我使用 Var_dump()
array (size=3)
0 =>
object(stdClass)[22]
public 'parents_id' => string '1' (length=1)
public 'linke' => string '' (length=0)
public 'title' => string 'parent1' (length=7)
1 =>
object(stdClass)[23]
public 'parents_id' => string '2' (length=1)
public 'linke' => string '' (length=0)
public 'title' => string 'parent2' (length=7)
public 'children' =>
array (size=6)
0 =>
object(stdClass)[26]
...
1 =>
object(stdClass)[25]
...
2 =>
object(stdClass)[27]
...
3 =>
object(stdClass)[28]
...
4 =>
object(stdClass)[29]
...
5 =>
object(stdClass)[30]
...
public 'sub_children' =>
array (size=1)
0 =>
object(stdClass)[33]
...
2 =>
object(stdClass)[24]
public 'parents_id' => string '3' (length=1)
public 'linke' => string '' (length=0)
public 'title' => string 'parent1' (length=7)
感谢楼主的帮助
【问题讨论】:
-
第一点:让您的生活更轻松,并为整个导航使用一个查询,而不是循环中的嵌套查询。第二个是:在你的循环中,你正在覆盖 $q。
-
我尝试创建不同的 @q 和 $row 它仍然无法正常工作。我对此不太了解
-
请显示一个 print_r($final) 来查看,该数组已正确填满。
-
哦,当我使用 var_dump() 函数时,它没有显示我的数组的任何值。作为我的嵌套循环和 mysql 子句,它显示了为什么它不显示
-
好好看看导航,试一试,结果更容易了。
标签: php codeigniter