Skorpio Posted October 20, 2015 Share Posted October 20, 2015 I followed a tutorial sometime back and now I want to use this script unfortunately the tutorial at the time did not cover what I am looking for but I did end up with the code below. I am wanting to have an active class dependant on users selection. I would appreciate any help. <?php echo '<ul id="nav">'; $cat_sql="SELECT * FROM administration"; $cat_query=mysqli_query($con, $cat_sql); // calling data put into associative array $cat_rs=mysqli_fetch_assoc($cat_query); do{ ?> <li><a <?php if ($current_page == "name") { ?><?php } ?>href="index.php?page=<?php echo $cat_rs['link_name']; ?>"><?php echo $cat_rs['name']; ?></a></li> <?php } while ($cat_rs=mysqli_fetch_assoc($cat_query)); echo "</ul>"; ?> <!-- need to add - class='active' --> The code above gets the field names for the buttons but I am unable to work out how to have an active class. The code is messy but have been trying all sorts to sort this. Thanks again for any pointers/help. Quote Link to comment https://forums.phpfreaks.com/topic/298727-php-active-class-for-nav-buttons/ Share on other sites More sharing options...
maxxd Posted October 24, 2015 Share Posted October 24, 2015 $curPg = $_SERVER['PHP_SELF']; // OR $curPg = $_SERVER['REQUEST_URI']; while($cat_rs = mysqli_fetch_assoc($cat_query)){ print("<li><a href='index.php?page={$cat_rs['link_name']}'"); if($cat_rs['link_name'] == $curPg){ print(" class='active'"); } print(">{$cat_rs['name']}</a></li>"); } You can use PHP_SELF or REQUEST_URI depending on what's stored in the 'link_name' column of your administration table. Or, you may have to do a bit more finessing on the data before the comparison. The important thing is that you get the current page and compare it to the value of link_name. If they match, you're on the current page and append the active class to that link. Quote Link to comment https://forums.phpfreaks.com/topic/298727-php-active-class-for-nav-buttons/#findComment-1524224 Share on other sites More sharing options...
benanamen Posted October 24, 2015 Share Posted October 24, 2015 (edited) You can use PHP_SELF No, PHP_SELF is vulnerable to SQL injection. You can use $_SERVER['SCRIPT_NAME'] Edited October 24, 2015 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/298727-php-active-class-for-nav-buttons/#findComment-1524227 Share on other sites More sharing options...
maxxd Posted October 24, 2015 Share Posted October 24, 2015 No, PHP_SELF is vulnerable to SQL injection. You can use $_SERVER['SCRIPT_NAME'] That's it - thank you! I knew there was something I should've remembered about PHP_SELF... Quote Link to comment https://forums.phpfreaks.com/topic/298727-php-active-class-for-nav-buttons/#findComment-1524234 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.