|
| |
|
|
womble
Posts: 5721 Joined: 3/14/2005 From: Living on the edge Status: offline
|
PHP includes problem - any ideas - 1/15/2007 16:44:37
I've used the following method to include a menu and highlight the current page on a previous web project without any problems, but on this project I can't get it to work. On the current page an id of "active" should get added to the <li> tag, and a "current" class to the <a> tag. The menu gets included, but the current page doesn't highlight - any ideas? Menu section on the current page: <!-- NAVIGATION -->
<div class="navigation">
<div class="curved_corner"></div>
<p class="navtitle">What's here?</p>
<!-- start navigation include -->
<?php include("includes/menu_include.php"); ?>
<!-- end navigation include --> Menu_include.php: <?php
$menu = file_get_contents("menu.html");
$menu = preg_replace("|<li id=\"active\"><a class=\"current\" href=\"" . basename($_SERVER['PHP_SELF']) . "\">(.*)</[^>]+></li>|U", "</a>", $menu);
echo $menu;
?> The menu.html file:
<ul class="navlist">
<li><a href="index.php">Home</a></li>
<li><a href="designs.php">Designs</a>
<ul class="subnavlist">
<li><a href="design1.php">Design 1</a></li>
<li><a href="design2.php">Design 2</a></li>
<li><a href="design3.php">Design 3</a></li>
</ul>
</li>
<li><a href="#">Graphics</a></li>
<li><a href="about.php">About</a></li>
<li><a href="links.php">Links</a></li>
<li><a href="contact.php">Contact Me</a></li>
_____________________________
~~ "A cruel god ain't no god at all" ~~ ~~ Erase hate. Practice love. ~~
|
|
|
|
womble
Posts: 5721 Joined: 3/14/2005 From: Living on the edge Status: offline
|
RE: PHP includes problem - any ideas - 1/15/2007 19:52:40
Yay! Sussed it! First I realised that I didn't need the id and the class, just the id on the <li>. Then I realised that I hadn't got "basename($_SERVER['PHP_SELF']" in the <a> tag that was replacing the orginal - put that in. Right stuff showing up on 'view source', but still not lightly up, which meant I'd also got a problem with the CSS....realised I'd got not got the 'a' on the style...duh! So, finished code incase it's any use to anyone... html... <!-- NAVIGATION -->
<div class="navigation">
<div class="curved_corner"></div>
<p class="navtitle">What's here?</p>
<!-- start navigation include -->
<?php include("includes/menu_include.php"); ?>
<!-- end navigation include --> CSS...
.navlist { width: 199px; color: #467aa7; background-color: #F2F3F5; border-right: 1px solid #c8c8c8; border-bottom: 1px solid #c8c8c8; font: 12px verdana,sans-serif; }
.navlist li { list-style: none; }
.navlist a { color: #467aa7; text-decoration: none; display: block; border-left: 1em solid #c8c8c8; border-top: 1px solid #c8c8c8; padding: 4px 8px; }
.navlist a:hover { border-color: #606B8B; }
ul.navlist li#current a { border-color: #606B8B; }
.subnavlist li a { border: 0; border-left: 0.8em solid #c8c8c8; padding: 3px 4px; margin-left: 20px; } Menu include file (menu.html)
<ul class="navlist">
<li><a href="index.php">Home</a></li>
<li><a href="designs.php">Designs</a>
<ul class="subnavlist">
<li><a href="design1.php">Design 1</a></li>
<li><a href="design2.php">Design 2</a></li>
<li><a href="design3.php">Design 3</a></li>
</ul>
</li>
<li><a href="graphics.php">Graphics</a></li>
<li><a href="about.php">About</a></li>
<li><a href="links.php">Links</a></li>
<li><a href="contact.php">Contact Me</a></li> And the menu_include.php file that does the clever replacing to add in the id on the <li>...
<?php
$menu = file_get_contents("menu.html");
$menu = preg_replace("|<li><a href=\"" . basename($_SERVER['PHP_SELF']) . "\">(.*)</[^>]+></li>|U", "<li id=\"current\"><a href=\"" . basename($_SERVER['PHP_SELF']) . "\">$1</a></li>", $menu);
echo $menu;
?>
The only problem is, I don't think that's gonna work on the ones that have got sub-menu items, because those ones don't have a closing </li> because it's closed after the sub-menu item list...*sigh* I can't see a way round that one at the moment unless I alter the html slightly and put a class on for whether it's a main menu item or a sub-menu item and then somehow do an if/else, but I'm not quite sure how I'd do that... ..unless I could do the preg_replace just on the <li><a href="filename.php">Item</a> portion of it....not sure if that would work....may do... ...then of course, do I want a title on the menu items?...I supoose I could do that on a case/break and add the variable from that into the above menu_include.php preg_replace bit....something like... $currentPage = basename($_SERVER['PHP_SELF']);
$title = '';
switch ($currentPage) {
case 'index.php':
$title = "Home page";
break;
case 'designs.php':
$title = "The company's design page";
break;
etc.
etc. That might just work. Definitely time for bed now. I'll tackle that tomorrow.
_____________________________
~~ "A cruel god ain't no god at all" ~~ ~~ Erase hate. Practice love. ~~
|
|
New Messages |
No New Messages |
Hot Topic w/ New Messages |
Hot Topic w/o New Messages |
Locked w/ New Messages |
Locked w/o New Messages |
|
Post New Thread
Reply to Message
Post New Poll
Submit Vote
Delete My Own Post
Delete My Own Thread
Rate Posts
|
|
|