Forum Moderators: coopster
<? $thispg="home"; ?> ... then, in the included menu file ...
if ($thispg=='$thisMenuItem') { ... do non-linking code ... } else { ... do linking code ... } When combined with a sub-item variable, it's very useful for highlighting the section of the site one is visiting, for example if the visitor is in the "About Us" section, reading one of several pages of bios, the "About Us" button link could be highlighted and still active for the root "About Us" page, where it would be highlighted but inactive.
I'll typically use the NAME of any link buttons that correspond to the link for this, so it's an easy reference to the link button, too.
<? $thisParent="about"; $thisItem="mybio"; ?> I either set the variable value to empty or something weird like "x" on a page for which I do not want any highlighting or link disabling.
$currentPage = end(explode('/', $_SERVER['REQUEST_URI']))
[... your loop for page navigation items ...]
if ($pageURL!= $currentPage)
{
// Make the page a link
} else {
// Don't make the page a link
}
[... end loop ...]
That way you wouldn't have to explicitely define a variable to find the current variable, you let the script find it for you. Only catch is, you'd have to have a var (in the example I've used $pageURL) that matches whatever your outputting for navigation... not hard though.
Ready for a pretty cool trick? Use PHP's output buffering to handle it. I'll throw the concept out here and you run with it -- meaning if you have to use a regular expression, variables or something fancier then go ahead and get creative. I started with this concept and have taken it to a whole new level. I'm sure you will to. But for now, I'll start out simple. Let's say your menu structure is using relative links and you don't already have a class defined on your <a> elements within your navigation code ...
<?phpTake the concept and run with it.
// +----------------------------------+
// ¦ Show currently active menu link: ¦
// +----------------------------------+
function activeLink($buffer)
{
return (
str_replace [php.net](
'href="' . $_SERVER['REQUEST_URI'] . '"',
'href="' . $_SERVER['REQUEST_URI'] . '" class="activelink"',
$buffer));
}
ob_start [php.net]('activeLink');
?>
<div id="menu">
<h2>Category One</h2>
<ul>
<li><a href="/category1/">Index</a><ul>
<li><a href="/category1/01">Category1-01</a></li>
<li><a href="/category1/02">Category1-02</a></li>
<li><a href="/category1/03">Category1-03</a></li>
<li><a href="/category1/04">Category1-04</a></li>
<li><a href="/category1/05">Category1-05</a></li>
<li><a href="/category1/06">Category1-06</a></li>
<li><a href="/category1/07">Category1-07</a></li>
<li><a href="/category1/08">Category1-08</a></li>
<li><a href="/category1/09">Category1-09</a></li>
<li><a href="/category1/faq/">FAQ</a></li>
</ul>
</li>
</ul>
</div>
<?php
ob_end_flush() [php.net];?>
These types of URLs should be noted with the trailing directory slash. i.e. This:
<li><a href="/category1/01">Category1-01</a></li> should be written as:
<li><a href="/category1/01/">Category1-01</a></li> Also, to accomplish the goals of the original post, I used:
str_replace( 'href="' . $_SERVER['REQUEST_URI'] . '"', 'href="#" class="activelink"', $buffer)); and, at the top, outside of the PHP section (of course):
<style type="text/css"> .activelink { font-weight:bold;text-decoration:none;cursor:default } </style> Very cool, I say again.
You'll need to use
preg_replace() to match the whole line including the label and the closing ANCHOR tag instead of str_replace() to just match the opening ANCHOR tag and its contents. I'm still messing around with it. The directory slashes in $_SERVER['REQUEST_URI'] are a bit problematic for me.
Here's what I have. I'm sure it's my regex syntax, but I'm not seeing it:
Note I changed the list syntax to make a single-line entry for replacement with an empty string. Instead of:
<li><a href="/category1/01/">Category1-01</a></li> I'm using:
<li /><a href="/category1/01/">Category1-01</a> function activeLink($buffer) { return ( preg_replace( '/<li \/><a href="' . $_SERVER['REQUEST_URI'] . '">\w*<\/a>/', '', $buffer)); } I haven't gotten any preg_replace to work, including:
preg_replace('category','sometext',$buffer) I also tried (1) using a
$pattern variable instead of the actual regex and (2) str_replace("/","\/",$_SERVER['REQUEST_URI']) in each attempt, without any difference. It just returns nothing, as if the buffer were "cleaned" instead of "flushed". No errors, just no output to the browser. The regex pattern should not be including everything, even if it got greedy and didn't stop after the first matching
</a>. At least it should have printed out the headline. It's printing the stylesheet, but nothing else, which indicates that the PHP isn't killing the page like it does when there's a PHP error, it's just not outputting anything from the buffer. Whew! I'm taking a break! :)
The trailing slash all depends on how you have your server setup. In this example it is assuming extensionless URI's. OK, here is a more specific example that demonstrates absolute paths and what could be possible. You are on the right track ...
<?php
// +----------------------------------+
// ¦ Show currently active menu link: ¦
// +----------------------------------+
function activeLink($buffer)
{
$protocol = 'http' .
((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')? 's' : '') .
'://';
$server = (isset($_SERVER['HTTP_HOST']))? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
$activeLink = $protocol.$server.$_SERVER['REQUEST_URI'];
$buffer = str_replace('/$server/', "/$server/", $buffer);
return (str_replace("href=\"$activeLink\"", 'class="activelink"', $buffer));
}
ob_start('activeLink');
?>
<div id="menu">
<h2>Category One</h2>
<ul>
<li><a href="http://$server/category1/">Index</a><ul>
<li><a href="http://$server/category1/01">Category1-01</a></li>
<li><a href="http://$server/category1/02">Category1-02</a></li>
<li><a href="https://$server/category1/03">Category1-03</a></li>
<li><a href="http://$server/category1/04">Category1-04</a></li>
<li><a href="http://$server/category1/05">Category1-05</a></li>
<li><a href="https://$server/category1/06">Category1-06</a></li>
<li><a href="http://$server/category1/07">Category1-07</a></li>
<li><a href="http://$server/category1/08">Category1-08</a></li>
<li><a href="http://$server/category1/09">Category1-09</a></li>
<li><a href="https://$server/category1/faq/">FAQ</a></li>
</ul>
</li>
</ul>
</div>
<?php
ob_end_flush();
?>
Kysmiley indicated that the goal was to keep the link to the current page from displaying at all. That's why I went to the preg_replace() function. Removing the link entirely would not be my choice for an intuitive menu, but it seems like an interesting challenge. :)
Thank you for the note, tkroll. I was using
\w because I couldn't predict what the text for the link would be ... although I should include spaces and hyphens, as well. So maybe: [\w\s-]*? (Sorry, I just woke up ... I know there's something about using \w\s within a class ... coffee time. :) My main problem is using preg_replace() within the ob function. I'm having trouble getting ANY implementation of it to return any output, even really simple ones.
/category1/01/
So, guess what is going to happen when you plop that variable into a regex using the slash as your delimiters? It's not going to work correctly ;)
You could use a different delimiter or better yet, run the variable through preg_quote() [php.net] first.
function activeLink($buffer) {
return (
preg_replace(
'/<li \/><a href="' . preg_quote($_SERVER['REQUEST_URI'], "/") . '">\w*<\/a>/',
'',
$buffer));
}
Thanks coopster and tkroll. Here's the working function:
function activeLink($buffer) { return ( preg_replace( '/<li \/><a href="' . preg_quote($_SERVER['REQUEST_URI'], "/") . '">[^<]*?<\/a>/', '', $buffer)); } Kysmiley: If you rearrange you items so they are completely on one line, as I mentioned in a previous post, and use this instead of coopster's original function, the link to the current page will be removed from the display, not just disabled.
Whew!
$index = 'index.php';
$index_no_ext = substr($index, 0, strrpos($index, '.'));if($basext == $index){
$c .= '<font class=currentnav>'.$index_no_ext.'</font><br />';
if ($handle = opendir($doc_root)){
while (false!== ($file = readdir($handle))){
$file_no_ext = substr($file, 0, strrpos($file, '.'));
if (preg_match("/$web_ext/", $file)) {
if($file!= $index){
$c .= ' -<a href="http://'.$domain.'/'.$file.'" class=navlink>'.$file_no_ext.'</a><br />';
}
}
}
} closedir($handle);
}if($basext!= $index){
$c .= '<a href="http://'.$domain.'/'.$index.'" class=navlink>'.$index_no_ext.'</a><br />';
$c .= ' - <font class=currentnav>'.$base.'</font>';}
$base is the current script without any extensions or paths. hth.