Newbie: Apply Active Class on menu items

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chaoskitten82
Forum Newbie
Posts: 3
Joined: Fri Dec 16, 2011 10:04 am

Newbie: Apply Active Class on menu items

Post 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?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Newbie: Apply Active Class on menu items

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
chaoskitten82
Forum Newbie
Posts: 3
Joined: Fri Dec 16, 2011 10:04 am

Re: Newbie: Apply Active Class on menu items

Post 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. =)
chaoskitten82
Forum Newbie
Posts: 3
Joined: Fri Dec 16, 2011 10:04 am

Re: Newbie: Apply Active Class on menu items

Post 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?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Newbie: Apply Active Class on menu items

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply