tapeter Posted June 10, 2009 Share Posted June 10, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/161744-how-to-format-the-included-text-of-a-dynamic-menu/ Share on other sites More sharing options...
RussellReal Posted June 10, 2009 Share Posted June 10, 2009 how do you get '$pagename'? Quote Link to comment https://forums.phpfreaks.com/topic/161744-how-to-format-the-included-text-of-a-dynamic-menu/#findComment-853391 Share on other sites More sharing options...
elis Posted June 10, 2009 Share Posted June 10, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/161744-how-to-format-the-included-text-of-a-dynamic-menu/#findComment-853392 Share on other sites More sharing options...
RussellReal Posted June 10, 2009 Share Posted June 10, 2009 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/161744-how-to-format-the-included-text-of-a-dynamic-menu/#findComment-853394 Share on other sites More sharing options...
tapeter Posted June 11, 2009 Author Share Posted June 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/161744-how-to-format-the-included-text-of-a-dynamic-menu/#findComment-853775 Share on other sites More sharing options...
tapeter Posted June 11, 2009 Author Share Posted June 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/161744-how-to-format-the-included-text-of-a-dynamic-menu/#findComment-853779 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.