Jump to content

trying to compare url to one i predetermine to change a links class


rincewind007j

Recommended Posts

i am trying to make a menu which suits the purpose of my website and this is the code i have come up with so far to change a links class as i need each link when active to have the class = current and when inactive to not use a class, i have put 1 link below for your help:

<?php print $_SERVER['REQUEST_URI']; ?>

<a href="Index.php"

<?php if ("Test/Index.php"==$_SERVER['REQUEST_URI'];)

{

class("current");

}

else

(

class("");

    }

?>

>Home</a>

You should have a variable containing the name or ID of the current page. Then e.g. build the menu with a loop:

 

<?php
//$page could be set via the URL, e.g. http://example.com/home/
$page = 'home';
$menu = array(
   'home' => 'Home link text',
   'about' => 'About link text'
);
foreach ($menu as $item => $text) {
   $current = ($page == $item) ? ' class="current"' : '';
   echo "<a href=\"/$item/\"$current>$text</a>\n";
}
//<a href="/home/" class="current">Home link text</a>
//<a href="/about/">About link text</a>
?>

so by what you put as a reply this will lork like this or would i need to do something else to is as i will be using it as an include on each page?

Sorry if it seems like i am asking dumb questions it is just that i am new to php and programming.

<?php

$page = 'home';

$menu = array(

  'home' => 'Home link text',

  'login' => 'Login link text',

  'forum' => 'Forum link text',

  'place 4 kids' => 'Home link text',

  'a-z' => 'A-Z link text',

  'links' => 'Links link text',

  'about' => 'About link text'

);

foreach ($menu as $item => $text) {

  $current = ($page == $item) ? ' class="current"' : '';

  echo "<a href=\"/$item/\"$current>$text</a>\n";

}

?>

    <div class="indentmenu">

<ul>

<li>

                <a href="Index.php">Home</a>

</li>

<li>

                <a href="Forgotten Password & Login.php">Login</a>

</li>

<li>

                <a href="Forum.php">Forum</a>

</li>

<li>

                <a href="Place 4 Kids.php">Place 4 Kidz</a>

</li>

<li>

                <a href="A-Z.php">A-Z</a>

</li>

<li>

                <a href="Sponsored Links.php">Links</a>

</li>

                <li>

                <a hred="About.php"></a>

                </li>

</ul>

</div>

Archived

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

×
×
  • Create New...

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.