Hi ! A question...

Hi, I’m coding a site, and what i’ve done is made a header.php page and inserted it into pages using

[php]<?php
require “header.php”
?>[/php]

But I want to know if something is possible in it.
This particular template has links along the top, and suppose you are on the homepage, the homepage link is suppose to be highlighted.

[php]
<?php
if( $userdata[‘session_logged_in’] )
{
echo ’

';
}
else
{
echo '    <ul id="nav">
  <li id="current"><a href="index.php" class="style26">Home</a></li>
  <li><a href="/forum">Forums</a>
  <li><a href="register.php">Sign Up</a></li>
  <li><a href="login.php">Login</a></li>
  <li><a href="#">Contact Us </a></li>
</ul>';
}
?>

[/php]

My question is, since this is in the header.php, everytime i go to a page with header.php in it, the home link is highlighted

[php]

  • Home
  • [/php]

    I want to know if it is possible to add something in the code so i can make it like IF you are on home, it’s highlighted, and IF you are on signup that’s highlighted, and if you arent on any in the navbar nothings highlighted…

    anyhelp?

    of cause u can do that.

    there is a static way (easyer to understand)
    and a dynamic way (better)

    anyway u need $_SERVER[‘PHP_SELF’], try to put a echo($_SERVER[‘PHP_SELF’]) to see what it does.

    the static way (means links are static incluting a static if):
    [php]
    if( $userdata[‘session_logged_in’] )
    {
    echo ‘

      ’;
      echo ‘<li’;
      if(substr(‘index’,$_SERVER[‘PHP_SELF’])!==false) echo ’ id=“current”’;
      echo '><a href=“index.php” ';
      if(substr(‘index.php’,$_SERVER[‘PHP_SELF’])!==false) echo 'class=“style26” ';
      echo ‘Home’;
    echo '<li';
    if(substr('forum',$_SERVER['PHP_SELF'])!==false) echo ' id="current"';
    echo '><a href="/forum" ';
    if(substr('index',$_SERVER['PHP_SELF'])!==false) echo 'class="style26" ';
    echo 'Forums</a></li>';
    
    echo '<li';
    if(substr('forum',$_SERVER['PHP_SELF'])!==false) echo ' id="current"';
    echo '><a href="contact.php" ';
    if(substr('contact.php',$_SERVER['PHP_SELF'])!==false) echo 'class="style26" ';
    echo 'Contact Us</a></li>';
    echo '</ul>';
    }
    

    [/php]

    the dynamic way is using an array. when changes are done only that array needs to be changed:
    [php]
    $nav=array( ‘Home’=>‘index.php’, ‘Forums’=>‘forum’, ‘Contact Us’=>‘contact.php’);

    if( $userdata[‘session_logged_in’] )
    {
    echo ‘

      ’;
      foreach($nav as $name => $url)
      {
      $active=(substr($url,$_SERVER[‘PHP_SELF’])!==false);
      echo ‘<li’.($active?’ id=“current”’:’’).’><a href="/’.$url.’"’($active?’ class=“style26”’:’’).’>’.$name.’’;
      }
      echo ‘
    ’;
    }
    [/php]
    (the codes are just examples, untested, and ment as a guide)

    P.S.: i don’t get why ‘Contact Us’ has no link, thats why i uses contact.php instead of #

    With my original code:
    [php] <?php
    if( $userdata[‘session_logged_in’] )
    {
    echo ’

    ';
    }
    else
    {
    echo '    <ul id="nav">
      <li id="current"><a href="index.php" class="style26">Home</a></li>
      <li><a href="http://www.za/forum/">Forums</a>
      <li><a href="http://wwel.com/forum/profile.php?mode=register">Sign Up</a></li>
      <li><a href="login.php">Login</a></li>
      <li><a href="#">Contact Us </a></li>
    </ul>';
    }
    ?>[/php]
    

    The result was

    When I implemented this code:

    [php]<?php if( $userdata[‘session_logged_in’] )
    {
    echo ‘

      ’;
      echo ‘<li’;
      if(substr(‘index’,$_SERVER[‘PHP_SELF’])!==false) echo ’ id=“current”’;
      echo '><a href=“index.php” ';
      if(substr(‘index.php’,$_SERVER[‘PHP_SELF’])!==false) echo 'class=“style26” ';
      echo ‘Home’;
    echo '<li';
    if(substr('forum',$_SERVER['PHP_SELF'])!==false) echo ' id="current"';
    echo '><a href="/forum" ';
    if(substr('index',$_SERVER['PHP_SELF'])!==false) echo 'class="style26" ';
    echo 'Forums</a></li>';
    
    echo '<li';
    if(substr('forum',$_SERVER['PHP_SELF'])!==false) echo ' id="current"';
    echo '><a href="contact.php" ';
    if(substr('contact.php',$_SERVER['PHP_SELF'])!==false) echo 'class="style26" ';
    echo 'Contact Us</a></li>';
    echo '</ul>';
    } ?>[/php]
    

    My result was

    It seems all the links are placed in one highlighted box…if i scroll over the link with my mouse it contains all the other links.

    echo ‘>Contact Us’;

    same in the other lines

    Sponsor our Newsletter | Privacy Policy | Terms of Service