我正在显示category,sub-category1和sub-category2,对于第一个类别,它将正确显示sub-category1和sub-category2。对于其他,它将正确显示sub-category1但不显示subategory2。
在我的控制器中使用此代码
$this->load->helper('url');
$this->load->model('categories_model');
$data['categoryres'] = $this->categories_model->getCategories();
$this->load->view("home", $data);
我的模特页面
function getCategories()
{
$this->db->select("*");
$query=$this->db->get('product_category');
if ($query->num_rows() > 0)
{
$result =$query->result_array();
for($i=0;$i<count($result);$i++)
{
$query1=$this->db->query("select * from product_sub_category1 where category_id_fk='".$result[$i]['id']."'");
if($query1->num_rows() > 0)
{
$result[$i]['sub1']=$query1->result_array();
//$result1=$query1->result_array();
for($j=0;$j<count($result);$j++)
{
$query2=$this->db->query("select * from product_sub_category2 where sub_category1_id_fk='".$result[$j]['id']."'");
if($query2->num_rows() > 0)
{
$result[$j]['sub2']=$query2->result_array();
}
else
{
$result[$j]['sub2']=array();
}
}
}
else
{
$result[$i]['sub1']=array();
}
}
//print_r($result);
return $result;
}
else
{
return $query->result_array();
}
}
在我的视图中使用此代码
<ul class="nav nav-tabs hidden-xs" style="border-color: white;">
<li class="dropdown ">
<a href="#" data-toggle="dropdown" class="dropdown-toggle" style="font-weight: bold; margin-top: 10px; color: #000000;text-decoration: none;background-color: #fff;">
<span style="font-size:16px;cursor:pointer" onclick="openNav()"><i class="fa fa-bars"> Categories </i><i class="fa fa-angle-down"></i></span></a>
<ul class=" amenu triangle-border top ">
<ul class="dropdown-menu">
<?php for($a=0;$a<count($categoryres);$a++)
{ ?>
<li class="category" style=""><?php echo $categoryres[$a]['category_name'];?>
<ul class="sub-menu"style="display: none; background-color: white;">
<?php if(!empty($categoryres[$a]['sub1'])&& count($categoryres[$a]['sub1'])>0)
{
for($b=0;$b<count($categoryres[$a]['sub1']);$b++) {?>
<li class="cat_head sub_category"><?php echo $categoryres[$a]['sub1'][$b]['sub_category_name'];?>
<ul class="sub-sub-menu"style="display: none; background-color: white;">
<?php if(!empty($categoryres[$b]['sub2']) && count($categoryres[$b]['sub2'])>0)
{
for($c=0;$c<count($categoryres[$b]['sub2']);$c++) {?>
<li><a href="#"><?php echo $categoryres[$b]['sub2'][$c]['sub_category2_name'];?></a></li>
<?php } } ?>
</ul>
</li>
<?php }} ?>
</ul>
</li>
<?php } ?>
</ul>
</ul>
</li>
</ul>