Kushi1up Posted June 1, 2010 Share Posted June 1, 2010 I'm trying to use a code that will allow me to make a menu file that can be included on all pages, but still have active links for each page. This would be the menu file: <?php $active[$current] = "class=active"; ?> <ul> <li <?php echo $active[1] ?>><a href=”index.php”>Home</a></li> <li <?php echo $active[2] ?>><a href=”services.php”>services</a></li> <li <?php echo $active[3] ?>><a href=”print.php”>print</a></li> <li <?php echo $active[4] ?>><a href=”web.php”>web</a></li> <li <?php echo $active[5] ?>><a href=”marketing.php”>marketing</a></li> <li <?php echo $active[6] ?>><a href=”portfolio.php”>Portfolio</a></li> <li <?php echo $active[7] ?>><a href=”contact.php”>Contact</a></li> </ul> Then include this in the page, changing the number to whatever link should be active: <?php $current = 3; include(”menu.php”); ?> The problem with this code is that when I include it, I get the following error: Parse error: syntax error, unexpected '=' in (location of the menu.php I used) on line 2 Link to comment https://forums.phpfreaks.com/topic/203575-php-menu-active-class-help/ Share on other sites More sharing options...
Andy-H Posted June 1, 2010 Share Posted June 1, 2010 Try: <?php $active = array_fill(1, 7, ''); $active[$current] = "class=active"; ?> <ul> <li <?php echo $active[1] ?>><a href=”index.php”>Home</a></li> <li <?php echo $active[2] ?>><a href=”services.php”>services</a></li> <li <?php echo $active[3] ?>><a href=”print.php”>print</a></li> <li <?php echo $active[4] ?>><a href=”web.php”>web</a></li> <li <?php echo $active[5] ?>><a href=”marketing.php”>marketing</a></li> <li <?php echo $active[6] ?>><a href=”portfolio.php”>Portfolio</a></li> <li <?php echo $active[7] ?>><a href=”contact.php”>Contact</a></li> </ul> Link to comment https://forums.phpfreaks.com/topic/203575-php-menu-active-class-help/#findComment-1066377 Share on other sites More sharing options...
Kushi1up Posted June 1, 2010 Author Share Posted June 1, 2010 Try: <?php $active = array_fill(1, 7, ''); $active[$current] = "class=active"; ?> <ul> <li <?php echo $active[1] ?>><a href=”index.php”>Home</a></li> <li <?php echo $active[2] ?>><a href=”services.php”>services</a></li> <li <?php echo $active[3] ?>><a href=”print.php”>print</a></li> <li <?php echo $active[4] ?>><a href=”web.php”>web</a></li> <li <?php echo $active[5] ?>><a href=”marketing.php”>marketing</a></li> <li <?php echo $active[6] ?>><a href=”portfolio.php”>Portfolio</a></li> <li <?php echo $active[7] ?>><a href=”contact.php”>Contact</a></li> </ul> No error with this code, but the link does not show as active. Link to comment https://forums.phpfreaks.com/topic/203575-php-menu-active-class-help/#findComment-1066386 Share on other sites More sharing options...
marcus Posted June 1, 2010 Share Posted June 1, 2010 print_r($active); post your results Link to comment https://forums.phpfreaks.com/topic/203575-php-menu-active-class-help/#findComment-1066389 Share on other sites More sharing options...
Kushi1up Posted June 1, 2010 Author Share Posted June 1, 2010 The actual links have been changed so the code looks like this: <?php $active = array_fill(1, 7, ''); $active[$current] = "class=active"; ?> <ul class="artmenu"> <li <?php echo $active[1] ?>><a href=home.php><span><span>Home</span></span></a></li> <li><a href="#"><span><span>About</span></span></a> <ul> <li><a href="#">On the Offline</a></li> <li><a href="#">Freelancive</a></li> </ul> </li> <li <?php echo $active[2] ?>><a href="#"><span><span>Comics</span></span></a> <ul> <li><a href="#">All</a></li> <li><a href="#">Story</a></li> <li><a href="#">PSA</a></li> <li><a href="#">Ads</a></li> <li><a href="#">Random</a></li> </ul> </li> <li <?php echo $active[3] ?>><a href="#"><span><span>Gallery</span></span></a> <ul> <li><a href="#">Wallpapers</a></li> <li><a href="#">Concept Art</a></li> <li><a href="#">Fan Art</a></li> </ul> </li> <li <?php echo $active[4] ?>><a href="#"><span><span>Forums</span></span></a></li> <li <?php echo $active[5] ?>><a href="#"><span><span>Store</span></span></a></li> <li <?php echo $active[6] ?>><a href="#"><span><span>extras</span></span></a> <ul> <li><a href="#">Contests</a> <ul> <li><a href="#">Writing</a> </li> <li><a href="#">Art</a> </li> <li><a href="#">Other</a> </li> </ul> </li> <li><a href="#">Downloads</a></li> <li><a href="#">Guides and Tips</a></li> </ul> </li> <li <?php echo $active[7] ?>><a href="#"><span><span>Contact</span></span></a></li> </ul> But the basics are the same. Here is what the page looks like only using html. Here is what the page looks like using the php include. I'm trying to get the include to make "home" active like in the html-only version of the page by using <?php $current = 3; include(”includes/menu.php”); ?> Link to comment https://forums.phpfreaks.com/topic/203575-php-menu-active-class-help/#findComment-1066409 Share on other sites More sharing options...
Andy-H Posted June 1, 2010 Share Posted June 1, 2010 <?php $active = array_fill(1, 7, ''); $active[$current] = 'class="active"'; echo '<pre>' . print_r($active, true) . '</pre>'; exit(0); ?> <ul class="artmenu"> <li <?php echo $active[1] ?>><a href=home.php><span><span>Home</span></span></a></li> <li><a href="#"><span><span>About</span></span></a> <ul> <li><a href="#">On the Offline</a></li> <li><a href="#">Freelancive</a></li> </ul> </li> <li <?php echo $active[2] ?>><a href="#"><span><span>Comics</span></span></a> <ul> <li><a href="#">All</a></li> <li><a href="#">Story</a></li> <li><a href="#">PSA</a></li> <li><a href="#">Ads</a></li> <li><a href="#">Random</a></li> </ul> </li> <li <?php echo $active[3] ?>><a href="#"><span><span>Gallery</span></span></a> <ul> <li><a href="#">Wallpapers</a></li> <li><a href="#">Concept Art</a></li> <li><a href="#">Fan Art</a></li> </ul> </li> <li <?php echo $active[4] ?>><a href="#"><span><span>Forums</span></span></a></li> <li <?php echo $active[5] ?>><a href="#"><span><span>Store</span></span></a></li> <li <?php echo $active[6] ?>><a href="#"><span><span>extras</span></span></a> <ul> <li><a href="#">Contests</a> <ul> <li><a href="#">Writing</a> </li> <li><a href="#">Art</a> </li> <li><a href="#">Other</a> </li> </ul> </li> <li><a href="#">Downloads</a></li> <li><a href="#">Guides and Tips</a></li> </ul> </li> <li <?php echo $active[7] ?>><a href="#"><span><span>Contact</span></span></a></li> </ul> Please modify the code and post back the output from print_r. Nice looking site so far BTW. Link to comment https://forums.phpfreaks.com/topic/203575-php-menu-active-class-help/#findComment-1066413 Share on other sites More sharing options...
Kushi1up Posted June 2, 2010 Author Share Posted June 2, 2010 <?php $active = array_fill(1, 7, ''); $active[$current] = 'class="active"'; echo '<pre>' . print_r($active, true) . '</pre>'; exit(0); ?> <ul class="artmenu"> <li <?php echo $active[1] ?>><a href=home.php><span><span>Home</span></span></a></li> <li><a href="#"><span><span>About</span></span></a> <ul> <li><a href="#">On the Offline</a></li> <li><a href="#">Freelancive</a></li> </ul> </li> <li <?php echo $active[2] ?>><a href="#"><span><span>Comics</span></span></a> <ul> <li><a href="#">All</a></li> <li><a href="#">Story</a></li> <li><a href="#">PSA</a></li> <li><a href="#">Ads</a></li> <li><a href="#">Random</a></li> </ul> </li> <li <?php echo $active[3] ?>><a href="#"><span><span>Gallery</span></span></a> <ul> <li><a href="#">Wallpapers</a></li> <li><a href="#">Concept Art</a></li> <li><a href="#">Fan Art</a></li> </ul> </li> <li <?php echo $active[4] ?>><a href="#"><span><span>Forums</span></span></a></li> <li <?php echo $active[5] ?>><a href="#"><span><span>Store</span></span></a></li> <li <?php echo $active[6] ?>><a href="#"><span><span>extras</span></span></a> <ul> <li><a href="#">Contests</a> <ul> <li><a href="#">Writing</a> </li> <li><a href="#">Art</a> </li> <li><a href="#">Other</a> </li> </ul> </li> <li><a href="#">Downloads</a></li> <li><a href="#">Guides and Tips</a></li> </ul> </li> <li <?php echo $active[7] ?>><a href="#"><span><span>Contact</span></span></a></li> </ul> Please modify the code and post back the output from print_r. Nice looking site so far BTW. Here is the result. Array ( [1] => class=active [2] => [3] => [4] => [5] => [6] => [7] => ) Link to comment https://forums.phpfreaks.com/topic/203575-php-menu-active-class-help/#findComment-1066429 Share on other sites More sharing options...
Kushi1up Posted June 2, 2010 Author Share Posted June 2, 2010 Got it to work! Just had to move the <?php echo $active[1] ?> to after the <a href instead of the <li> Thanks for the help, guys. Link to comment https://forums.phpfreaks.com/topic/203575-php-menu-active-class-help/#findComment-1066439 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.