SugaComa Posted November 1, 2022 Share Posted November 1, 2022 Hi All, Im new here, off work with flu and I thought I would try and get some updates done on my website, its been neglected for so long I have a dynamic page listing so I can send my content via a simple template and I use this to then generate the Menu my issue is setting the class "active" I either have none active or all active, I can never seem to get it to pick the currently selected menu item here is a sample of code - if anyone can help me out it would be appreciated. thank you <?php $pageArray = array( 'Page1', 'Page2', 'Page3', 'Page4' ); function active($selected_page){ $menu_array = explode('/', $_SERVER['REQUEST_URI']); $current = end ($menu_array); if ($selected_page == $current); { echo 'active'; } } ?> <ul> <li><a class="<?php active('$pageArray[0]'); ?>" <?php echo 'href="?page=' . $pageArray[0] . '"'; ?>><?php echo $pageArray[0]; ?></a></li> <li><a class="<?php active('$pageArray[1]'); ?>" <?php echo 'href="?page=' . $pageArray[1] . '"'; ?>><?php echo $pageArray[1]; ?></a></li> <li><a class="<?php active('$pageArray[2]'); ?>" <?php echo 'href="?page=' . $pageArray[2] . '"'; ?>><?php echo $pageArray[2]; ?></a></li> <li><a class="<?php active('$pageArray[3]'); ?>" <?php echo 'href="?page=' . $pageArray[3] . '"'; ?>><?php echo $pageArray[3]; ?></a></li> </ul> Quote Link to comment https://forums.phpfreaks.com/topic/315483-dynamic-menu-active-link-issue/ Share on other sites More sharing options...
ginerjm Posted November 1, 2022 Share Posted November 1, 2022 I have no idea what that line "echo 'active'; is suppose to be doing. I see the word 'active' being output to your page but for what purpose? Then I see a loop that uses some very complex code to output your links to your different menu pages. Perhaps you need to change that function to return the value rather than output it? Even better combine those two operations into a single block the uses the loop to produce your desired output. And what class do you want to use for the items you don't see as being active? How do you like this attempt? $pageArray = array('Page1','Page2','Page3','Page4'); $menu_array = explode('/', $_SERVER['REQUEST_URI']); $current = end($menu_array); echo '<ul>'.PHP_EOL; foreach($pageArray as $pg) { echo "<li><a "; if($pg == $current) echo "classs='active' "; else echo "class='inactive' "; echo "href='(I do not know)?page=\"$pg\"'>$pg</a></li>".PHP_EOL; } echo "</ul>".PHP_EOL; Which gave me this which may be close: <ul> <li><a class='inactive' href='(I do not know)?page="Page1"'>Page1</a></li> <li><a class='inactive' href='(I do not know)?page="Page2"'>Page2</a></li> <li><a class='inactive' href='(I do not know)?page="Page3"'>Page3</a></li> <li><a class='inactive' href='(I do not know)?page="Page4"'>Page4</a></li> </ul> Of course I ended up with no active classes since my uri doesn't match your array. Quote Link to comment https://forums.phpfreaks.com/topic/315483-dynamic-menu-active-link-issue/#findComment-1602158 Share on other sites More sharing options...
Solution Barand Posted November 1, 2022 Solution Share Posted November 1, 2022 No need to mess about exploding the URI - just use $_GET['page']. <style type='text/css'> a { color: gray; text-decoration: none; } a.active { color: red; font-weight: 600; } </style> <?php $selectedPage = $_GET['page'] ?? ''; // get selected page (or empty if there isn't one) $pageArray = array( 'Page1', 'Page2', 'Page3', 'Page4' ); echo "<ul>\n"; foreach ($pageArray as $p) { $act = $selectedPage == $p ? 'active' : ''; // if this is the selected page, add 'active' to class echo "<li><a class='pageitem $act' href='?page=$p'>$p</a></li>"; } echo "</ul>\n"; ?> Result 1 Quote Link to comment https://forums.phpfreaks.com/topic/315483-dynamic-menu-active-link-issue/#findComment-1602159 Share on other sites More sharing options...
SugaComa Posted November 2, 2022 Author Share Posted November 2, 2022 @Barand thank you so much, this absolutely worked perfectly with my existing code @ginerjm thank you also, I haven't looked into your answer as yet, but I will as it will help me on my learning journey Quote Link to comment https://forums.phpfreaks.com/topic/315483-dynamic-menu-active-link-issue/#findComment-1602170 Share on other sites More sharing options...
wees Posted January 19, 2023 Share Posted January 19, 2023 i want active code for cid (category page like formun or ecommerce) https://hitechggg.000webhostapp.com/shop2/category.php?cid=7 for this page how to active this page Quote Link to comment https://forums.phpfreaks.com/topic/315483-dynamic-menu-active-link-issue/#findComment-1604837 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.