Creating Smart Navigation Bars

Discussion in 'PHP' started by Anduril66, Dec 31, 2006.

  1. #1
    Hello, I am having trouble designing a navigational bar that will work on all pages of my site. I wanted to use <?php include("navigationbarname.extension")?> on my index page in root directory, where the navigation bar was something like this:

    <ul class="navbar">
    <li><a href="." class="nav">Home</a></li>
    <li><a href="typesofsteak/" class="nav">Types of Steak</a></li>
    <li><a href="steakpreparation/" class="nav">Preparation</a></li>
    <li><a href="steakhouses/" class="nav">Steakhouses</a></li>
    </ul>

    However, I cannot include that same file on the other pages of the site because the relative links will be better. Right now I am using absolute links. Is there a way I could code my navigation bar so it can be included in different subdectories using relative links? Thanks, Daniel
     
    Anduril66, Dec 31, 2006 IP
  2. fsmedia

    fsmedia Prominent Member

    Messages:
    5,163
    Likes Received:
    262
    Best Answers:
    0
    Trophy Points:
    390
    #2
    there shouldn't be a problem with that at all....but you do have some weird referencing with the href's. try using a .php extension for the file name. I do it on a few of my sites and it works without a hitch....
     
    fsmedia, Dec 31, 2006 IP
  3. Anduril66

    Anduril66 Well-Known Member

    Messages:
    390
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    108
    #3
    hmm, those links are to folders, like a href="typesofsteak/" points to domain.com/typesofsteak/index.php , but the users cannot tell it is a php page. If I only used the navigation bar on my domain.com/index.php page, I would not have an issue, but I want to also include the same navigation bar on the other pages of my site, such as /typesofsteak/index.php, but I can't simply include ../navigationbar.html because the relative links would no longer work.
     
    Anduril66, Dec 31, 2006 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    
    <?
    // Root domain path, - trailing slash
    $root_dom = "http://yourdomain.com";
    // incase u ignore the - trailing slash bit
    $root_dom = preg_replace("@/$@", "", $root_dom);
    ?>
    <ul class="navbar">
    <li><a href="<?=$root_dir ?>" class="nav">Home</a></li>
    <li><a href="<?=root_dir ?>/typesofsteak/" class="nav">Types of Steak</a></li>
    <li><a href="<?=root_dir ?>/steakpreparation/" class="nav">Preparation</a></li>
    <li><a href="<?=root_dir ?>/steakhouses/" class="nav">Steakhouses</a></li>
    </ul>
    
    PHP:
    That'll work ....
     
    krakjoe, Jan 1, 2007 IP
    Anduril66 likes this.
  5. Anduril66

    Anduril66 Well-Known Member

    Messages:
    390
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    108
    #5
    Thanks very much for the code.

    I changed <li><a href="<?=$root_dir ?>" class="nav">Home</a></li> to
    <li><a href="<?=$root_dir ?>/" class="nav">Home</a></li>, and added a $ in front of the other root_dirs and it worked great.

    Just curious, is it faster to use the PHP code or absolute links if a domain has many visitors?

    Edit: hmm I think the php processor on my web host is very slow
     
    Anduril66, Jan 1, 2007 IP
  6. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #6
    very little overhead if any, and oops sorry for leaving out characters, in the middle of the day aswell ....
     
    krakjoe, Jan 2, 2007 IP
    robster likes this.