Horse Posted November 7, 2008 Share Posted November 7, 2008 Hi All, Hopefully someone can help me out with this. Here is a simplified version of the way I currently have my main menu on my site. The "current" link has a different background colour so the user knows where they are. <ul> <li id="current"><a href="index.php">Home</a></li> <li><a href="about.php">About Us</a></li> <li><a href="register.php">Register</a></li> <li><a href="contactus.php">Contact</a></li> </ul> Now any time I want to add anything to the menu I need to update it on every page so i wanted to pull it from the database using mysql_fetch_assoc. I have it working with the following code: $query="select linkname, id, url from tbl ORDER BY id"; if($result = mysql_query($query)) while($row = mysql_fetch_assoc($result)) { echo ' <ul> <li><a href=' .$row['url']. '>' .$row['linkname']. '</a></li> </ul>';} BUT obviously I have now lost the ability to specify the "current" page. Does anyone know how i can use this new method to pull the menu from the database but still specify the current page? TIA for any help! Link to comment https://forums.phpfreaks.com/topic/131801-pull-menu-items-from-mysql-db-and-still-specify-current-page/ Share on other sites More sharing options...
ashton321 Posted November 7, 2008 Share Posted November 7, 2008 Not exactly sure what you want to do but fro my site i just make the navigation a separate file then use include() to add that file to the top of every page. Then when i change the navigation i only change the one file and the changes are included in the other pages. Hope that helps. Link to comment https://forums.phpfreaks.com/topic/131801-pull-menu-items-from-mysql-db-and-still-specify-current-page/#findComment-684801 Share on other sites More sharing options...
n3ightjay Posted November 7, 2008 Share Posted November 7, 2008 Although I write my navigation the same way as ashton ... the way to achieve what your looking for is... <?php $current = explode("/"$_SERVER['PHP_SELF']); $query="select linkname, id, url from tbl ORDER BY id"; if($result = mysql_query($query)){ echo '<ul>'; while($row = mysql_fetch_assoc($result)) { ?> <li<?=($current[1] == $row['url']?" id=\"current\"":"")?>><a href=' .$row['url']. '>' .$row['linkname']. '</a></li> <? } // while echo '<ul>'; } ?> assuming your pages are all at root level if not you have to play with the $current variable and such Link to comment https://forums.phpfreaks.com/topic/131801-pull-menu-items-from-mysql-db-and-still-specify-current-page/#findComment-684811 Share on other sites More sharing options...
Horse Posted November 11, 2008 Author Share Posted November 11, 2008 Hi, thanks for the replies. The code above doesn't work though, fixed all the problems I could see with it but still couldn't get it to do what I needed. I also include the menu but my issue was that I have to create a separate menu file for each page in order to get the effect I need (a different background colour on the "current" link). I'm convinced this is not necessary. The logic of your code looks like the right direction to me - get the current url and if url in the db for that row matches then add the =current to the <li> ...i just think the syntax is wrong... Any ideas? Link to comment https://forums.phpfreaks.com/topic/131801-pull-menu-items-from-mysql-db-and-still-specify-current-page/#findComment-687754 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.