SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict Shantra's Avatar
    Join Date
    Feb 2001
    Location
    Norway
    Posts
    224
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 2-Level Menu - How to?

    For a new project, I need to use a 2-level menu (vertical). I can't figure out how to make it.

    First, it must not use javascript or mysql, and second, not be of the "dynamic" type (like CSS pop-ups).

    Here is my "old" 1-level code, with added hardcoded 2'nd level links (the li's). Also, only one 1'st level link/menubar is included (About Us):

    history.php:
    PHP Code:
    <?php
    $page 
    "History";
    include 
    'include/header.php'
    ?>
    <h1>History</h1>
    <?php include 'include/footer.php'?>
    header.php:
    PHP Code:
    <head> 
    <title>Site - <?php echo $page?></title>
    </head>

    <?php>
    if (
    $page=="About Us")
    echo 
    "<p><a class=\"navselect\" href=\"../about.php\">About Us</a></p>
    <ul>
    <li><a class=\"nav2\" href=\"\">Introduction</a></li>
    <li><a class=\"nav2select\" href=\"\">History</a></li>
    <li><a class=\"nav2\" href=\"\">Goals</a></li>
    </ul>"
    ;
    else
    echo 
    "<p><a class=\"nav\" href=\"../about.php\">About Us</a></p>";
    ?>

    <?php echo "<small>You are here: Home > About Us (hardcoded) > " $page "</small>"?>
    As you can see, there is no code for the 2'nd level links (the li's). When pressing the "About Us" menubar, I want to be taken to about.php (CSS highlighting the menubar). The 2'nd level links is now showing. Pressing "History" should take me to history.php and and "History" should be highlighted by CSS (in addition to "About Us").

    Also, I would like to have a working "You are here:" bar, like the one at end of header.php

    How do I do this? Is my "old" way to simple for this 2-level menu. Or, could a little adaption be enough?

    Thanks!
    Last edited by Shantra; Apr 29, 2009 at 21:35.

  2. #2
    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)
    If Introduction, History and Goals are also seperate files, which I will assume they are, then you are really dealing with a hierarchy of files.

    You could store that hierarchy as an array.

    Arrays are good because you can loop through them.

    Arrays are good because you can create them from databases or text files.

    Lets say you shift from making conditional tests on the title of the page, e.g.

    ( if $page == "About us" )

    What happens if you accidentally type "About Us" or the client want you to change it to "About <client name>" - you have to go hunting around.

    Are you able to change it to:

    ( if $page == "about" )

    That is, use the filename (minus the filetype) as a unique and possibly less likely to change token.

    PHP Code:
    //  Eventually comment this line out, testing only
    $file 'description' ;

    // uncomment the line below, it detects which page it is on. 
    // eg description.php becomes description

    // $file = basename($_SERVER['PHP_SELF'], ".php"); 


    // bring in your navigation as an array

    $nav['about']['title'] = "About us" // magic entry, title.
    $nav['about']['description'] = "What we do";
    $nav['about']['history'] = "A bit of history";
    $nav['about']['goals'] = "Our Goals";

    foreach( 
    $nav['about'] as $k=>$v ){

    if( 
    $k === 'title') {  // detect this is a title only
    echo '<p>' $v '</p><ul>' PHP_EOL;
    continue ;
    }
    // otherwise, is this the current page?

    if( $file === $k ){
    echo 
    '<li class="navselect">';
    }else{
    echo 
    '<li class="nav">';
    }
    // display the rest of the link
    echo '<a href="' $k '.php" >' $v '</a></li>' PHP_EOL ;

    }

    echo 
    '</ul>' PHP_EOL 
    If you are looking at description.php should then give you;

    Code:
    <p>About us</p><ul>
    <li class="navselect"><a href="description.php" >What we do</a></li>
    <li class="nav"><a href="history.php" >A bit of history</a></li>
    <li class="nav"><a href="goals.php" >Our Goals</a></li>
    </ul>

    Does that give you some ideas maybe?

    The term you are looking for is a "breadcrumb trail", searching that may find you some solutions too.


Tags for this Thread

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
  •