Forum Moderators: coopster

Message Too Old, No Replies

Hiding hyperlinks for page displayed

         

Kysmiley

9:42 pm on Mar 10, 2005 (gmt 0)

10+ Year Member



Can anyone point me in a direction to find a tutorial on writing a script that will hide, (not show) The text link for the home page if my vistor is on the home page. i have read a book and they have on th4ere but it only deals with changing the type of image not disabling it and hiding it from veiw.
Pat

coopster

11:36 pm on Mar 10, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Just remove the href attribute from the <a> element.

Kysmiley

12:26 am on Mar 11, 2005 (gmt 0)

10+ Year Member



I have a common header so I was looking for away to do this thru php that would recongize the page and disable the link or hide it all together. My header has the left nav container that is generic for all pages.
Pat

StupidScript

12:38 am on Mar 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I use a little variable on each page, and then test for that when writing the menu code:

<?

$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.

ironik

1:16 am on Mar 11, 2005 (gmt 0)

10+ Year Member



Or if you define the page names in your php script when you display your navigation you can test the URI:

$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.

coopster

1:34 am on Mar 11, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



OK, I recently had a very difficult menu structure to re-design and the situation called for something very similar to what you want to do here.

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 ...

<?php  
// +----------------------------------+
// ¦ 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];?>
Take the concept and run with it.

Kysmiley

2:03 am on Mar 11, 2005 (gmt 0)

10+ Year Member



Thank you everyone now I will read more on the links provided and try to ru withit while learning more on PHP
Pat

StupidScript

3:49 am on Mar 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Wow. That IS cool, coopster. Thanks.

Now I'll be up ALL NIGHT! :) (And loving every minute of it!)

StupidScript

10:21 pm on Mar 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Quick note to a cool function:

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.

StupidScript

11:21 pm on Mar 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, that is NOT the solution to the original post, which requires actually removing the link from the list ... not just disabling or highlighting it.

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.

Kysmiley

11:51 pm on Mar 11, 2005 (gmt 0)

10+ Year Member



I was wondering why It was not working for me. Thanks for the update
Pat

StupidScript

12:28 am on Mar 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Boy, this is messing me up! :)

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! :)

coopster

2:43 am on Mar 12, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Keep going, it is worth the effort to understand how it works. The example given leaves the current link active. If you don't want it active, you just don't print the "href" portion again.

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();
?>

tkroll

5:54 am on Mar 12, 2005 (gmt 0)

10+ Year Member



Hey SS,

In your regex, your \w* match includes the "-" character, which, if I remember correctly, is not included in the \w character set.

Good luck!

---

A "word" character is any letter or digit or the underscore character, that is, any character which can be part of a Perl "word".

StupidScript

5:08 pm on Mar 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



coopster, the thing I'm in the midst of mucking up is removing the link and it's trigger text/image entirely, rather than just disabling it. Your first example already works great for disabling it (but I am grateful for the more complex illustration, as well).

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.

coopster

8:28 pm on Mar 12, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Don't forget that $_SERVER['REQUEST_URI'] is probably going to have a slash character in it, for example:

/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));
}

tkroll

2:42 am on Mar 13, 2005 (gmt 0)

10+ Year Member



How about:

[^<]*?

Usually works best in these situations. Cheers!

StupidScript

7:19 pm on Mar 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okey dokey!

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!

kilonox

6:53 pm on Mar 15, 2005 (gmt 0)

10+ Year Member



heres a simple navigation script that I use in some simple web sites.


$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 .= '&nbsp;&nbsp;-<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 .= '&nbsp;&nbsp;-&nbsp;&nbsp;<font class=currentnav>'.$base.'</font>';

}


$base is the current script without any extensions or paths. hth.

Kysmiley

1:19 am on Mar 17, 2005 (gmt 0)

10+ Year Member



thanks everyone Now i will decide which is best to show the text and just disable the liunk which seems to be the prefered choice. I am on the road so will see how it works this weekend when i get home
Thanks again
pat