mysql - creating class active dynamically in php -
i creating menu dynamically based on categories registered on database, need is: when click in link, option should have css class 'active', showing in page user , pages created dynamically. have no idea how because php student , couldn't find information in google "dynamically menus" thank much.
<nav class="menu"> <ul class="list"> <li class="line"><a id="item" href="index.php">home</a></li> <?php $categories = $db->select("select * tbl_category"); if($categories){ while ($result = $categories->fetch_assoc()){ echo <<<html <li id="line" ><a class="item" href="categories.php?category=$result[id]">$result[name]</a></li> html; } } ?> </ul> <nav>
first of all, you're looping through while
loop , adding id of "line" each , every <li>
element. suggest create unique id's each list item. don't necessary advocate using code way have it, example, here's code edited that:
if($categories){ $i = 1; while ($result = $categories->fetch_assoc()){ $i++; echo <<<html <li id="line$i" ><a class="item" href="categories.php?category=$result[id]">$result[name]</a></li> html; } }
then when clicks on link, use jquery this:
$(".item").click(function() { $(this).parent('li').addclass('active'); });
Comments
Post a Comment