Jump to content

Archived

This topic is now archived and is closed to further replies.

Stripy42

simple nuwbe question, change link depending on curent page

Recommended Posts

Hello, I've ben trying to do something really simple but can't find out how.

I want to use php to insert the wesites links at the top of every page (easy "include" done that bit)

But I want it to not show or gray out the link to the current page.

I figured some sort of

"if curent site = home.php
then "nothing"
else echo [code]<a href whatever>stuff</a>[/code]

But the actual code needed is evading me. ???

Thanks

Share this post


Link to post
Share on other sites
A simple example would be:
[code]<?php
echo (strpos($_SERVER['PHP_SELF'],"home.php")) ? "Home" : "<a href='home.php'>Home</a>";
?>[/code]

Although there are better ways to go around the whole idea.

Share this post


Link to post
Share on other sites
Cool,
So that will be like an if for each and every link, I can see what you mean about better ways.

I was thinking I could get the document name once at the begining, and then compare it for each link to the actual href in the link.
That way I won't add an extra chance to create a mistake by haveing to type the link twice.

Share this post


Link to post
Share on other sites
I'd go for something like this:

index.php
[code]<?php
  include_once('functions.php');
  $page = $_SERVER['PHP_SELF'];
  createnav($page);
?>[/code]

Then functions.php looks like this:
[code]<?php
function createnav($var){
  preg_match("/([^\/]+)$/", $var, $missing_page);
  $pages = array(
  "Home" => "index.php",
  "Contact Me" => "contact.php",
  "Google" => "http://www.google.co.uk"
  );
  foreach ($pages as $page => $link){
      if ($link != $missing_page[0]){
        echo "<a href=\"$link\">$page</a><br>";
      }
  }
}
?>[/code]

This should work.  You just include a list of pages in the array inside the function.  It's not ideal if you have a lot of pages, but could be adapted.

[size=8pt][color=red][b]EDIT: [/b][/color]I've updated the above code and it should work fine.[/size]

Regards
Rich

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.