Jump to content

Recommended Posts

 

I'm working on my first PHP project: importing Menu code across the website using an include() function.

 

I want to use PHP conditional statements (in the included menu code) to style the current page uniquely (using the variable $pagename, named at the top of each page). Yet, it doesn't seem to be evaluated. Do you know why?

 

Thanks for your help!!!

 

Tim

 

The test page: http://visitacp.org/volunteers/

The code:

 

<ul>

<li><a <?php if($pagename=="home") echo 'class="active"'; ?> href="/">Home</a></li>

  <li><a <?php if($pagename=="events") echo 'class="active"'; ?> href="/events/">Events</a></li>

  <li><a <?php if($pagename=="contact") echo 'class="active"'; ?> href="/contact/">Contact</a></li>

<li><a <?php if($pagename=="join") echo 'class="active"'; ?> href="/members/">Join</a></li>

<li><a <?php if($pagename=="volunteer") echo 'class="active"'; ?> href="/volunteers/">Volunteer</a></li>

<li><a <?php if($pagename=="contests") echo 'class="active"'; ?> href="/contest/">Contests</a></li>

<li><a <?php if($pagename=="blog") echo 'class="active"'; ?> href="/blog/">Blog</a></li>

    <li><a <?php if($pagename=="about") echo 'class="active"'; ?> href="/about/">About</a></li>

       

          <?php if($pagename=="about"||"vision"||"people") {

 

  if ($pagename=="vision" ) echo '<li class="submenu"><a class="active" href="#">Vision</a></li>';

 

else echo '<li class="submenu"><a href="/about/vision/">Vision</a></li>';

 

  if ($pagename=="people") echo '<li class="submenu"><a class="active" href="#">People</a></li>';

 

else echo '<li class="submenu"><a href="/about/people/">People</a></li>';

 

} ?>

</ul>

sorry double post, but try this.. it should solve your problems

 

<?php
$pages = array();
$pages[] = array('home','/','Home');
$pages[] = array('events','/events/','Events');
$pages[] = array('Contact','/contact/','Contact');
$pages[] = array('join','/join/','Join');
$pages[] = array('volunteer','/volunteers/','Volunteer');
$pages[] = array('contests','/contest/','Contests');
$pages[] = array('blog','/blog/','Blog');
$pages[] = array('about','/about/','About');
foreach ($pages as $v) {
	if (strpos($_SERVER['REQUEST_URI'],$v[1]) !== false) $addTo = " class='active'";
	else $addTo = '';
	echo "<li><a href='{$v[1]}'{$addTo}>{$v[3]}</a></li>\n";
}
?>

Are you certain it's not working? I looked at your site, and upon clicking the links, they changed to "active" format. I also looked at the HTML and they've changed to <li class="active">

 

Or maybe I've misunderstood your problem?

 

I'm only working with the index page in '/volunteers/' right now (everything else is static), and it lacks the active page styling in the menu that I'm seeking.

 

$pagename is defined at the top of that page, and so menu.php (the include code) - based on the condition, ought to output a 'class="active"' for /volunteers/ -- it doesn't.

 

Is there something I need to do to include the varaible $pagename in menu.php? Is the formatting for PHP in menu.php right?

 

Thanks,

TP

sorry double post, but try this.. it should solve your problems

 

<?php
$pages = array();
$pages[] = array('home','/','Home');
$pages[] = array('events','/events/','Events');
$pages[] = array('Contact','/contact/','Contact');
$pages[] = array('join','/join/','Join');
$pages[] = array('volunteer','/volunteers/','Volunteer');
$pages[] = array('contests','/contest/','Contests');
$pages[] = array('blog','/blog/','Blog');
$pages[] = array('about','/about/','About');
foreach ($pages as $v) {
	if (strpos($_SERVER['REQUEST_URI'],$v[1]) !== false) $addTo = " class='active'";
	else $addTo = '';
	echo "<li><a href='{$v[1]}'{$addTo}>{$v[3]}</a></li>\n";
}
?>

 

This might work, though it's missing the <li> text (see www.visitacp.org/volunteers/index2.php & /menu2.php).

 

Also, I'm wanting to include 'submenu' links just on the 'About Us' pages. Any thoughts?

 

Finally, can someone point me to a good tutorial on integrating PHP/Javascript to have a menu that expands on roll-over (for submenu), and can stylize current page?

 

Thanks all,

TP

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.