Page 1 of 1

PHP solution for my nav menu

Posted: Thu Mar 16, 2006 6:06 pm
by bruceg
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello all,

I am trying to accomplish something utlizing PHP. What I have is a menu marked up as such:

Code: Select all

<div id="navcontainer">
<ul id="navlist">
<li id="active"><a href="About_Me.php" title="you know you want to learn more about me" id="current">About Me</a></li> 
<li><a href="Skillset.php" title="I've got skillz">Skill set</a></li>
<li><a href="Hireme.php" title="I can do wonders for your web presence">Hire Me</a></li> 
<li><a href="Portfolio.php" title="web sites, graphics, newsletters">Portfolio</a></li>
<li><a href="Contact.php"  title="how to get in touch with me">Contact</a></li> 
<li><a href="Resume.php"  title="my beautiful resume">R&eacute;sum&eacute;</a></li>
<li><a href=" http://inspiredevolution.blogs.com/inspiredevolutioncom/"  title="the blog for Inspired-Evolution.com" >Blog</a></li>
<li class="last"><a href="RSS.php"  title="Syndication that is really simple" >RSS</a></li> 

</ul>
</div>
and then I have everything styled nicely with CSS to give you the menu you see here:

http://www.inspired-evolution.com/About_Me.php

What I want to do next is to change the menu from being hard coded on all of my pages to being an include which is easily done with PHP. The stumbling block is I want to be able to keep the CSS functionality where you have the active indicator which has different CSS for the link of the page you are on. I have seen this done with PHP before with something like IF on active page use this style ELSE use this style. Can any PHP gurus assist me in coding something like this?

I don't believe it is too difficult, but beyond by scope right at the moment.


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Mar 16, 2006 6:07 pm
by a94060
use the code or php tages around it.

Posted: Fri Mar 17, 2006 1:28 am
by matthijs
There are 2 ways:
1- hardcode a variable which holds the pagename or title of the page and after that include the menu. In the menu, for each list item you check the condition if (this page == pagename) then echo "current"

Code: Select all

<?php 
$thispage = 'homepage';
?>
//include nav.php:
<ul>
<li <?php if ($thispage == 'homepage') { echo 'id="current"; } ?>><a href="">Home</a></li>
etc
</ul>
2- detect automaticly which page is requested and build the menu from that.
Matt does this with http://www.photomatt.net/scripts/intellimenu/

Posted: Fri Mar 17, 2006 2:27 pm
by bruceg
I am trying the photo Matt version, but I get this error:

parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /hsphere/local/home/bruceg/inspired-evolution.com/includes/menu.php on line 12

here is my code

Code: Select all

<?php
$menu = <<<MENU
<ul id="navlist">
<li ><a href="About_Me.php" title="you know you want to learn more about me">About Me</a></li>
<li><a href="Skillset.php" title="I've got skillz">Skill set</a></li>
<li><a href="Hireme.php" title="I can do wonders for your web presence">Hire Me</a></li>
<li><a href="Portfolio.php" title="web sites, graphics, newsletters">Portfolio</a></li>
<li><a href="Contact.php"  title="how to get in touch with me">Contact</a></li>
<li><a href="Resume.php"  title="my beautiful resume">R&eacute;sum&eacute;</a></li>
<li><a href="http://inspiredevolution.blogs.com/inspiredevolutioncom/"  title="the blog for Inspired-Evolution.com" >Blog</a></li>
<li><a href="RSS.php"  title="Syndication that is really simple" >RSS</a></li>
</ul>
MENU;
$lines = split("\n", $menu);
foreach ($lines as $line) {
$current = false;
preg_match('/href="([^"]+)"/', $line, $url);
if (substr($_SERVER["REQUEST_URI"], 0, 5) == substr($url[1], 0, 5)) {
$line = str_replace('<a h', '<a id="current" h', $line);
}
echo $line."\n";
}
?>

any ideas on what the error might be?

Posted: Fri Mar 17, 2006 3:25 pm
by matthijs
I think you have a tiny syntax mistake somewere in your code above or below this code. I tried your code and it works fine for me.