SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict Zapppa's Avatar
    Join Date
    Aug 2009
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Main Page Navigation in PHP

    I am relatively new to PHP, trying to learn more every day.

    I have been enjoying using Server Side Includes in my sidebars, footer etc with good results, much handier and more efficient!

    I would like to do the same for the header and main site navigation.

    However, the main problem that I am not sure how to deal with comes from having 'active' links (differently coloured menu tab for the page the user is on) in my menu.

    Here is a small menu example:

    Code:
    <div class="menu">
                     <ul>
                             <li><a href="nav.php" class="active">Home</a></li>
                             <li><a href="nav2.php">About</a></li>
                             <li><a href="products1.php">Products</a></li>
                             <li><a href="products2.php">Products 2</a></li>
                             <li><a href="products3.php">Products 3</a></li>
    
                            
                        </ul>
        </div>
    When the user is on the Home page, the class is set to active - i.e. a different colour.

    If I do a simple server side include with my navigation this will of course be across all the pages. Can anyone please advise on what I need to do to make the menu work correctly across all the pages? (have different tabs selected for each respective page).

    Any help / insight is much appreciated.

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    70
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You could declare a variable before you include the header file.

    HTML Code:
    <div class="menu">
        <ul>
            <li><a href="nav.php" <?php if( $page == 'index') echo 'class="active"'?> >Home</a></li>
            <li><a href="nav2.php" <?php if( $page == 'about') echo 'class="active"'?> >About</a></li>
            <li><a href="products1.php" <?php if( $page == 'products') echo 'class="active"'?> >Products</a></li>
            <li><a href="products2.php" <?php if( $page == 'product2') echo 'class="active"'?> >Products 2</a></li>
            <li><a href="products3.php" <?php if( $page == 'product3') echo 'class="active"'?> >Products 3</a></li>
        </ul>
    </div>
    PHP Code:
    $page 'products';

    include 
    'header.php'
    This will set the desired desire link to active based on the conditional statement in the link elements.

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    How many pages in your site in total?

    Do you have content in directories and do they relate to menu headings?

    eg /products/this.php and so on?

  4. #4
    SitePoint Addict Zapppa's Avatar
    Join Date
    Aug 2009
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the replies so far. I willwill have around ten links, all in the sane directory.

  5. #5
    SitePoint Addict Zapppa's Avatar
    Join Date
    Aug 2009
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JeremyC View Post
    You could declare a variable before you include the header file.

    HTML Code:
    <div class="menu">
        <ul>
            <li><a href="nav.php" <?php if( $page == 'index') echo 'class="active"'?> >Home</a></li>
            <li><a href="nav2.php" <?php if( $page == 'about') echo 'class="active"'?> >About</a></li>
            <li><a href="products1.php" <?php if( $page == 'products') echo 'class="active"'?> >Products</a></li>
            <li><a href="products2.php" <?php if( $page == 'product2') echo 'class="active"'?> >Products 2</a></li>
            <li><a href="products3.php" <?php if( $page == 'product3') echo 'class="active"'?> >Products 3</a></li>
        </ul>
    </div>
    PHP Code:
    $page 'products';

    include 
    'header.php'
    This will set the desired desire link to active based on the conditional statement in the link elements.

    thanks!! I will try this tomorrow when i get into work

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    For such a small nav bar then JeremyC's solution will be fine I'd say, just means hardcoding the file-key "about" in each of your ten files.

    PHP Code:
    <?php

    $this_page 
    "about";

    include 
    'nav_bar.php' ;

    ?>
    If you wanted to do it another way you could detect the filename from your $_SERVER array.

    (just do var_dump($_SERVER); on one of your pages to see what else is available to you).

    Then you could keep your pages in an array and generate your nav bar from that:

    nav_bar.php
    PHP Code:
    <?php 
    // you just maintain this array, which you keep handy at the top of the file

    $pages = array(
      
    '/index.php' => 'Home',
      
    '/about.php' => 'About',
      
    '/contact.php' =>  'Contact us',
      
    // etc
    );

    // grab the var
    $this_page $_SERVER['SCRIPT_NAME'];

    #TODO you could test if $this_page exists in the permitted array first

    echo '<ul>' ;  // start menu

    foreach( $pages as $key=>$val ) {  // loop thru array

    echo '<li><a href="'  $key '"'// start nav link

    if( $key == $this_page) echo ' class="active"';  // if array key '/index.php' matches $this_page 

    echo '>' $val  '</a></li>' PHP_EOL // end nav link

    // end of loop

    echo '</ul>' // end menu
    Sorry, I am unable to test this on my setup today, but that should give you another idea about how you could tackle this problem -- but as I say, JeremyCs idea is just fine.

  7. #7
    SitePoint Member
    Join Date
    May 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I new in php I use script above in my web but only the last menu appear

    PHP Code:

        <?php
        
    include "configuration/connection.php";
        
    $sql=mysql_query("SELECT * from mainmenu where active='Y' ORDER BY id_main");
        while (
    $m=mysql_fetch_array($sql))

        
    $pages = array(
        
    "$m[link]=> "$m[menu_name]",
        
    // etc
        
    );

        
    // grab the var
        
    $this_page "$m[link]";

        
    #TODO you could test if $this_page exists in the permitted array first

        
    echo '<ul>' // start menu

        
    foreach( $pages as $key=>$val // loop thru array

        
    echo '<li><a href="' $key '"'// start nav link

        
    if( $key == $this_page) echo ' class="active"'// if array key '/index.php' matches $this_page

        
    echo '>' $val '</a></li>' PHP_EOL // end nav link

        // end of loop

        
    echo '</ul>' ;?>
    anyone can please help me,.. ??


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •