1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Include Navigation Menu - Updating Current Tab

Discussion in 'PHP' started by matthewbeckman, Mar 25, 2008.

  1. #1
    Ok so here is my problem. I currently have approximately 15 pages, all with a navigational menu. I have recently been needing to change the tabs for the menu, but have to go in to every page individually and update it. For certain pages I will make the <li> tag use the class "active" to show the tab selected. How would I go about doing a 'php include' for this? I remember at one time I had a site that included the navigation in a php file, but could tell what page it was on due to the main div class(or something like that) and actually give one of the items the "active" class to make it appear selected.

    Below is my code for the navigation. The first <li> would be the selected tab. Thanks ahead of time.

    
    <div id="navigation">
      <ul>
        <li class="active"><a href="../../index.php"><b>About</b></a></li>
        <li><a href="../../services/index.php"><b>Services</b></a></li>
        <li><a href="../../profile/index.php"><b>Profile</b></a></li>
        <li><a href="../../portfolio/index.php"><b>Our Portfolio</b></a></li>
        <li><a href="../../bidding/index.php"><b>Bidding</b></a></li>
        <li><a href="../../contact/index.html"><b>Contact Us</b></a></li>
      </ul>
    </div>
    
    Code (markup):
    My site is currently being tested on a random site of mine, so check it out if you need more information. www.lamefilms.com
     
    matthewbeckman, Mar 25, 2008 IP
    Weirfire likes this.
  2. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #2
    Create a page called nav.php, stick all your navigation in there and put it in a folder called "includes".

    Then on the pages you want the navigation to appear insert the following code;

    
    <?php
    
    $page_a = explode("/",$_SERVER['SCRIPT_URL']);
    $page = $page_a[$count($page_a)-1] . "/" . $page_a[count($page_a)];
    
    switch($page){
     
     case 'services/index.php' :
    
        $services_active = ' class = "active" ';
    
    
     break;
    
    // etc ....
    
    }
    
    include 'includes/nav.php';
    ?>
    
    
    PHP:
    and for each navigation item put in the relevant variable to state whether the navigation is active or not.

    Let me know if this helps.

     
    Weirfire, Mar 25, 2008 IP
  3. matthewbeckman

    matthewbeckman Peon

    Messages:
    140
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Weirfire,

    Seems to be a no go at this time. I will try and walk you through the steps I took. First off I created a folder named "includes" in my site directory. It can be found at lamefilms.com/includes. In that folder I put a php file called "nav.php" & inside the "nav.php" file I put the following code:

    
    <div id="navigation">
    <ul>
    <li><a href="index.php"><b>About Us</b></a></li>
    <li><a href="services/index.php"><b>Services</b></a></li>
    <li><a href="profile/index.php"><b>Company Profile</b></a></li>
    <li><a href="portfolio/index.php"><b>Our Portfolio</b></a></li>
    <li><a href="customers/index.php"><b>Bidding </b></a></li>
    <li><a href="contact/index.html"><b>Contact Us</b></a></li>
    </ul>
    </div>
    
    Code (markup):
    I then created a page called "navigation.php" also in my home root folder. The page can be found at lamefilms.com/navigation.php I just duplicated the main "index.php" page and took out the navigation and in its place I put this code:

    
    <?php
    
    $page_a = explode("/",$_SERVER['SCRIPT_URL']);
    $page = $page_a[$count($page_a)-1] . "/" . $page_a[count($page_a)];
    
    switch($page){
     
    case 'index.php' :
    
    $services_active = ' class = "active" ';
    
    break;
    
    }
    
    include 'includes/nav.php';
    ?>
    
    Code (markup):
    I then uploaded everything to my server and got the error message:
    "Fatal error: Function name must be a string in /mnt/w0714/d14/s11/b02c5d23/www/lamefilms.com/navigation.php on line 29"

    Line 29 is:
    "$page = $page_a[$count($page_a)-1] . "/" . $page_a[count($page_a)];"

    It was probably just something stupid I did :D

    www.lamefilms.com/includes/nav.php
    www.lamefilms.com/navigation.php

    Thanks for helping!
     
    matthewbeckman, Mar 25, 2008 IP
  4. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #4
    $count replace by count


    Regards

    Alex
     
    kmap, Mar 25, 2008 IP
    matthewbeckman likes this.
  5. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #5
    oops. sorry about that.

    Need a job Alex? lol
     
    Weirfire, Mar 25, 2008 IP
  6. matthewbeckman

    matthewbeckman Peon

    Messages:
    140
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The navigation shows up... but I can't get any tabs to appear selected.

    Say I am currently at www.lamefilms.com/customers/index.php what needs to go into the line that says "case 'customers/index.php'" because that definitely doesn't work. I also tried a absolute URL with no such luck.
     
    matthewbeckman, Mar 25, 2008 IP
  7. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #7
    You see where I put $services_active....


    You need to put that in here;

    <div id="navigation">
      <ul>
        <li> <a href="../../index.php"><b>About</b></a></li>
        <li <?=$services_active?>><a href="../../services/index.php"><b>Services</b></a></li>
        <li><a href="../../profile/index.php"><b>Profile</b></a></li>
        <li><a href="../../portfolio/index.php"><b>Our Portfolio</b></a></li>
        <li><a href="../../bidding/index.php"><b>Bidding</b></a></li>
        <li><a href="../../contact/index.html"><b>Contact Us</b></a></li>
      </ul>
    </div>
    PHP:
     
    Weirfire, Mar 25, 2008 IP
  8. matthewbeckman

    matthewbeckman Peon

    Messages:
    140
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hmm now I have no idea what is going on...

    I am not even sure if we are on the same page haha. I want a navigation menu that extends to all of my pages on my site. I want that menu to figure out what page it is on, and choose the appropriate tab to give the class active... It appears as if yours script requires me to tell it what page it's on? Even then I still can't get any of this to work. What is my nav.php file supposed to like like?

    
    <div id="navigation">
      <ul>
        <li> <a href="../../index.php"><b>About</b></a></li>
        <li <?=$services_active?>><a href="../../services/index.php"><b>Services</b></a></li>
        <li><a href="../../profile/index.php"><b>Profile</b></a></li>
        <li><a href="../../portfolio/index.php"><b>Our Portfolio</b></a></li>
        <li><a href="../../bidding/index.php"><b>Bidding</b></a></li>
        <li><a href="../../contact/index.html"><b>Contact Us</b></a></li>
      </ul>
    </div>
    
    Code (markup):
    This is what you gave me...? Did you want me to do something like this?

    
    <div id="navigation">
      <ul>
        <li <?=$index_active?>> <a href="../../index.php"><b>About</b></a></li>
        <li <?=$services_active?>><a href="../../services/index.php"><b>Services</b></a></li>
        <li <?=$profile_active?>><a href="../../profile/index.php"><b>Profile</b></a></li>
        <li <?=$portfolio_active?>><a href="../../portfolio/index.php"><b>Our Portfolio</b></a></li>
        <li <?=$customers_active?>><a href="../../customers/index.php"><b>Bidding</b></a></li>
        <li <?=$contact_active?>><a href="../../contact/index.html"><b>Contact Us</b></a></li>
      </ul>
    </div>
    
    Code (markup):
    Excuse me for not knowing a whole lot about PHP. I am not sure if services is just part of the language, or is how it figures out when to add the "class=active" to the HTML. Then you told me to add this to each of my HTML pages that require a navigation. For our sake we will use an example. Say I want the tab to pop up on my portfolio page, what should the code look like? It would be located at www.lamefilms.com/portfolio/index.php

    
    <?php
    
    $page_a = explode("/",$_SERVER['SCRIPT_URL']);
    $page = $page_a[count($page_a)-1] . "/" . $page_a[count($page_a)];
    
    switch($page){
     
    case 'portfolio/index.php' :
    
    $portfolio_active = ' class = "active" ';
    
    break;
    
    }
    
    include '../includes/nav.php';
    ?>
    
    Code (markup):
    I changed the case line to where the file is which is portfolio/index.php ? Then I change the place where it did say $services_active to $portfolio_active or no?

    Ha sorry about not knowing anything, but I really thought I knew what I was doing at first because I have had done it before, but it wasn't anything like this, but I don't remember much about how I did it.
     
    matthewbeckman, Mar 25, 2008 IP
  9. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #9
    One way of doing this would be to have a variable before you call the includes line;

    $page = "portfolio";
    PHP:
    then in the includes file just do

    switch($page){
    PHP:


    Also the switch case statement should include all pages and should appear in the navigation file.

    switch($page){
     
     case 'portfolio/index.php' :
      $portfolio_active = ' class = "active" ';
     break;
    
     case 'services/index.php' :
      $services_active = ' class = "active" ';
     break;
    
    // etc
    
    }
    PHP:
    The navigation code was perfect.



     
    Weirfire, Mar 25, 2008 IP
    matthewbeckman likes this.
  10. matthewbeckman

    matthewbeckman Peon

    Messages:
    140
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thanks I got everything working beautifully! +Rep
     
    matthewbeckman, Mar 25, 2008 IP
  11. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #11
    You're welcome. If you have any "difficult" functionalities you'd like on your site that you'd be willing to pay for give me a shout :)
     
    Weirfire, Mar 25, 2008 IP
  12. matthewbeckman

    matthewbeckman Peon

    Messages:
    140
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Ya we are planning on expanding the site with more features in the next few weeks. I think these will probably be out of my skill level, so feel free to email me your contact information so I don't lose you in this post!

    syncdaily at gmail dot com
     
    matthewbeckman, Mar 26, 2008 IP
  13. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #13
    Email sent :)
     
    Weirfire, Mar 26, 2008 IP