Jump to content

Pull menu items from MySQL DB and still specify current page


Horse

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.