Navigation using PHP - HELP !

Discussion in 'PHP' started by MV07SMV, Jul 14, 2009.

  1. #1
    Hi,

    I've put my navigation in a php script so i only have to update it once when the site is up and running, but I want it to recognise which page its on and show a different background image/font for that page in the Navigation.

    site is at http://www.mattvalentine.co.uk/norcross/index.php

    i know if it wasn't in the php i could just add a class to whichever page it was on at that point but want to keep my code tidy and save time in the future.

    PLEASE HELP ! First attempt at PHP !

    ta
     
    MV07SMV, Jul 14, 2009 IP
  2. gregor171

    gregor171 Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    gregor171, Jul 14, 2009 IP
  3. techbongo

    techbongo Active Member

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Use this piece of code in the php file, where links are saved (and is included inside other site files, ;) I use same trick).

    if($_SERVER['PHP_SELF']=="some_file_name.php")
    {
    $background="img1.jpg";
    }
    elseif(...)
    {
    ...
    }
    ...

    If guess, you got my point. If you need more clarification, PM me.
     
    techbongo, Jul 14, 2009 IP
  4. Goramba

    Goramba Peon

    Messages:
    128
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    A twist on this that will save you coding space if you have a lot of pages is:

    $background = str_replace("php", "jpg", $_SERVER['PHP_SELF']);
    $background = str_replace("/", "", $background);
    PHP:
    The second part may or may not be necessary, depending on how you're doing it.

    Now just save your backgrounds as page_name.jpg and badabing.
     
    Goramba, Jul 14, 2009 IP
  5. garrettheel

    garrettheel Peon

    Messages:
    341
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The easiest way is just to define a page variable at the top of the page. E.g

    
    <?php
    $page = "Home";
    include("header.php");
    ?>
    
    Code (markup):
    Then, in the header file, check if the page is equal to the page variable and add a class if it is. You should put all your menu items in an array to make things easier.

    E.g

    
    $menu_items = array(
        "Home" => "link to home",
        "About" => "link to about"
    )
    
    foreach($menu_items as $name=>$link){
        if($page == $name){
            // this is the item for the current page
        }
        else {
            // regular item
        }
    }
    
    Code (markup):
     
    garrettheel, Jul 14, 2009 IP