Jump to content


Photo

simple nuwbe question, change link depending on curent page


  • Please log in to reply
4 replies to this topic

#1 Stripy42

Stripy42
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 13 September 2006 - 03:47 PM

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
<a href whatever>stuff</a>

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

Thanks

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 13 September 2006 - 04:02 PM

A simple example would be:
<?php
echo (strpos($_SERVER['PHP_SELF'],"home.php")) ? "Home" : "<a href='home.php'>Home</a>";
?>

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

#3 Stripy42

Stripy42
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 13 September 2006 - 04:20 PM

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.

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 13 September 2006 - 04:23 PM

I'd go for something like this:

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

Then functions.php looks like this:
<?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>";
      }
   }
}
?>

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.

EDIT: I've updated the above code and it should work fine.

Regards
Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#5 Stripy42

Stripy42
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 15 September 2006 - 09:19 AM

Yay! that is excilent, thank you. :D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users