Forum Moderators: coopster

Message Too Old, No Replies

Highlighting Active Link in Navigation Problem

         

joelwsmith

5:14 pm on Apr 15, 2010 (gmt 0)

10+ Year Member



I'm new to PHP (about 12 hours of total experience) and new here. I have been searching for hours for an answer to my question, but nothing has worked.

I have a header.php file that includes all my navigation elements. I then call that file with a PHP include on each page. Works great, no problems.

I'm using the code below to define a page "name" above the <html> tag on every page. Also works great, no problems.

<?php $thisPage="About"; ?>


I'm using the code below in an unordered list for my main navigation. The links either point to a page or show a "selected" <li> element if you're on the current page. All of this is in header.php.
<div id="navigation">

<a id="logo" href="http://www.example.com"><img src="http://www.example.com/img/logo.gif" alt="Home" /></a>

<ul>
<?php if ($thisPage == "About")
{echo "<li class=\"selected\">about</li>";}
else
{echo "<li><a href=\"http://www.example.com/about.php\">about</a></li>";}
?>
<?php if ($thisPage == "Resellers")
{echo "<li class=\"selected\">resellers</li>";}
else
{echo "<li><a href=\"http://www.example.com/resellers.php\">resellers</a></li>";}
?>
</ul>

</div>


The HTML works by itself perfectly without the PHP, and the "selected" classes of the <li> work without the PHP.

When the PHP codes above are in place, the links show up fine on all pages but the "selected" class does not. It still shows the regular link.

I've tried everything I can think of. Any help would be appreciated.

[edited by: eelixduppy at 11:47 pm (utc) on Apr 15, 2010]
[edit reason] exemplified [/edit]

nickCR

5:30 pm on Apr 15, 2010 (gmt 0)

10+ Year Member



Personally i'm not a big fan of mixing HTML and PHP code because it gets messy and "VERY" confusing.

This code here will do what you want with very little cross over.

The only difference here is that the link will always remain, which really isn't a big deal if you change the link styling so people know it's the active page.

Basically i'm assigning a dynamic variable. So it will take whatever is in "$thisPage" and turn it into a variable.


<?php
$$thisPage = 'class="selected"';
?>

<div id="navigation">

<a id="logo" href="http://www.example.com"><img src="http://www.example.com/img/logo.gif" alt="Home" /></a>

<ul>
<li <?=$About?>><a href="http://www.example.com/about.php">about</a></li>
<li <?=$Resellers?>><a href="http://www.example.com/resellers.php">resellers</a></li>
</ul>

</div>

[edited by: eelixduppy at 11:48 pm (utc) on Apr 15, 2010]
[edit reason] exemplified [/edit]

Matthew1980

7:33 pm on Apr 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there joelwsmith,

Welcome to the forum ;-p

Firstly

<?php if ($thisPage == "About")


Hopefully you have assigned the $_POST or £_GET to the vars in the clause, because using registered globals is something that is only turned on in a few servers. It is better to use the $_POST/$_GET values, cleanse them with something like strip_tags(); and if you use them in conjunction with a mysql query use mysql_real_escape_string();

Secondly
The mods will probably alter the http address' you gave, try to exemplify them for future posts ;-p

Thirdly
What does the page source show when the php is mixed with the html, I'm curious to see if the link info is affected, as there are no escape slashes - you need them if you are printing html from an echo.

I think as there may be an issue with the way in which the clause is constructed, I suggest putting a debug echo in there to see what part of the clauses are called and if they are as expected.

From what I can see you just want the a href there if the var isn't set? Hopefully I have read that right.

NickRC's right about mix and match too, where ever possible try to dip in and out of php, this makes it less overhead for the CPU on the server, and html editing alot easier.

I hope I make sense :)

Cheers,
MRb

joelwsmith

3:39 pm on Apr 17, 2010 (gmt 0)

10+ Year Member



nickCR and Matthew1980,

Thanks for the help.

Matthew1980, thanks for the reminder on the http addresses, I forgot to edit those. I will do that from now on.

I'm not sure how to assign the $_POST or �_GET to the vars. How would I do that? Sorry, I'm a big noob at this. Probably in over my head for tackling this.

After seeing your answers, my other question is this:

Can I keep the

<?php $thisPage="page_title"; ?>


on every page and still make it work? I'm calling that page title in various places on a page and would have to go through and edit all of those snippets.

Thank you!

Readie

3:43 pm on Apr 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, with $_GET:

http://www.example.com/index.php?action=hello
$some_variable = $_GET['action'];

With $_POST:

<input type="text" name="action" />
$some_variable = $_POST['action'];