PDA

View Full Version : Would this work?


Sean Hall
06-16-2012, 02:01 PM
Hello,
I am new to PHP, this is my first script I have made without any help, I am using xammp to test my site, but the basics of the code is this. I am adding an include code into the navigation menu and in the CSS I have defined certain rules for the nav. Now in the code when you click on one of the links, it will change the variable to a corresponding number. The Code is pretty much self explanatory.


<?
$active = 1;
?>
<ul>
<li><a href="#" onclick="<? $active = 1; ?>" class="<? if ($active = 1){?>#navigation ul li a.active<? } ?>">home</a></li>
<li><a href="#" onclick="<? $active = 2; ?>" class="<? if ($active = 2){?>#navigation ul li a.active<? } ?>">about us</a></li>
<li><a href="#" onclick="<? $active = 3; ?>" class="<? if ($active = 3){?>#navigation ul li a.active<? } ?>">services</a></li>
<li><a href="#" onclick="<? $active = 4; ?>" class="<? if ($active = 4){?>#navigation ul li a.active<? } ?>">portfolio</a></li>
<li><a href="#" onclick="<? $active = 5; ?>" class="<? if ($active = 5){?>#navigation ul li a.active<? } ?>">contact</a></li>
</ul>


Now what I want to happen is when you click on one of the links, it will change to active, changing the color of the text to tell the visitor that, that link is active. Now normally I would do it though CSS, changing the texts rule to the active, but in order to be able to change buttons without having to do it though all the pages and also because I want to start using PHP more.

The Include works, as you can see below, but the code above does not work, because the home button should be a light yellow color.

Image will be added soon.....

Corrosive
06-18-2012, 07:10 AM
Not sure about the code as I only dabble in PHP as well but I thought the syntax for opening a line of PHP is <?php and not just <?

davidj
06-18-2012, 09:48 AM
If your implementing this through php alone you will have to pass a flag to tell PHP which one to mark as active.

I suggest looking at JQuery for menus. By all means implement and include using PHP but for the mechanics I would use JQuery.