Reputation: 261
I am working on a module where a part of my navigation menu has to come dynamically with associated dropdowns/submenus. It is like I have a table in my database tbl_levels
where two LEVELS are kept. Under each LEVEL there are associated Units whose details are kept in another table tbl_question_module
with id
from tbl_levels
as column level
. I want these two levels to list one by one, which is I am getting. Now I want to list units associated with each level as drop down when LEVEL 1 or LEVEL 2 is clicked from navigation menu. I have implemented the below code but what I am obtaining is only submenus for LEVEL1 and when LEVEL 2 is clicked then it again opens up the LEVEL 1 list. Can I get some insight where I am getting wrong. This will be very helpful.
<?php
$sq_lev = $db->query("SELECT id,level_title from tbl_levels WHERE level_status = 1 ");
while($row_lev=mysql_fetch_array($sq_lev))
{
$level_id=$row_lev['id'];
$level_title=$row_lev['level_title '];
?>
<li data-toggle="collapse" data-target="#products3" class="collapsed"> <a href="#"><span class="glyphicon glyphicon-book"></span><?php echo $level_title; ?><span class="arrow"></span></a>
<ul class="sub-menu collapse" id="products3">
<?php
$sq = $db->query("SELECT id, title from tbl_question_module WHERE status = 1 AND level = $level_id ");
while($rowM=mysql_fetch_array($sq))
{ ?>
<li><a href="studyMaterial.php?unit_id=<?php echo $fnc->encode($rowM['id']) ; ?>"><?php echo $rowM['title'] ; ?></a></li>
<?php } ?>
</ul>
</li> <?php } ?>
Upvotes: 1
Views: 75
Reputation: 1364
You have to update data-target
dynamically inside the loop
<li data-toggle="collapse" data-target="#products<?= $level_id ?>" class="collapsed"> <!-- **Changes here** -->
<a href="#">
<span class="glyphicon glyphicon-book"></span>
<?php echo $level_title; ?>
<span class="arrow"></span>
</a>
<ul class="sub-menu collapse" id="products<?= $level_id ?>"> <!-- **Changes here** -->
ID attribute of html is always unique in the current page. and according to jquery
or data-target
if more then two element contain the same id then it's always point to first element in the body
Upvotes: 2