Page 1 of 1

Newbie: Apply Active Class on menu items

Posted: Fri Dec 16, 2011 10:21 am
by chaoskitten82
Hello. I'm desiging a site that uses includes. I want the navigation to show a marker to the user to indicate what page they are on. But, my nav is an include (nav.php) and I've been struggling to get the code right to have the pages work with the include to show the active state. I've tried this:

Code: Select all

<?php
$active[$current] = “class=active”;
?>

<div id="nav_wrapper">
<div id="nav_container">

	<div id="logo">
    <img src="images/nav/logo.jpg" alt="Mountain Vista Pain Specialists Logo" width="230" height="90" />
    </div>
    
    <ul id="nav">
        <li <?php echo $active[1] ?>><a href=”services.php”>Services</a></li>
        <li <?php echo $active[2] ?>><a href=”physician.php”>Physician</a></li>
        <li <?php echo $active[3] ?>><a href=”patients.php”>Patients</a></li>
        <li <?php echo $active[4] ?>><a href=”contact.php”>Contact</a></li>
	</ul>

Then placing this code on the current page:

<?php $current = 1; include('includes/nav.php'); ?>

But I get a parse error:

Parse error: syntax error, unexpected '=' in E:\inetpub\vhosts\mvpainspecialists.com\httpdocs\devphp\includes\nav.php on line 2

Please don't suggest I use $page_name in the LI because there are only 4 nav elements yet approx 15 pages. (Basically my pages fall under the nav categories, but I am not using dropdowns...once the user lands on one of the category pages, they will get a secondary side nav)

Any hints?

Re: Newbie: Apply Active Class on menu items

Posted: Fri Dec 16, 2011 11:02 am
by social_experiment

Code: Select all

<?php
$active[$current] = "class=active";
?>
Should fix the syntax issue.
chaoskitten82 wrote:Please don't suggest I use $page_name in the LI because there are only 4 nav elements yet approx 15 pages. (Basically my pages fall under the nav categories, but I am not using dropdowns...once the user lands on one of the category pages, they will get a secondary side nav)
What will the links look like for the 15 sub-pages

Re: Newbie: Apply Active Class on menu items

Posted: Fri Dec 16, 2011 2:38 pm
by chaoskitten82
@social_experiment i think that worked! i've got to build out the rest of the pages...but it should work perfectly. Thanks so much for the quick response!

The 15 sub pages have a secondary nav, a left side nav. I will also have to figure out how to get that to work as well. If I can't figure it out...I'll post back okie dokie? I really appreciate the help. =)

Re: Newbie: Apply Active Class on menu items

Posted: Fri Dec 16, 2011 2:40 pm
by chaoskitten82
@social_experiment - One quick question though...I don't really see the difference in what I posted VS what you sent. The words are the same...so why the different action? What did I do wrong?

Was it the quotes?

Re: Newbie: Apply Active Class on menu items

Posted: Sat Dec 17, 2011 3:01 am
by social_experiment

Code: Select all

<?php
$active[$current] = “class=active”;
//
$active[$current] = "class=active";
?>
The quotation marks was the issue yes; it can happen when you copy code from somewhere and then paste it into a IDE or plain-text editor; i'm not sure what causes it though