Site Navigation Code
  Home arrow Site Navigation Code arrow Simple Menu System
IBM developerWorks
Try It Free
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Dedicated Servers  
Download TestComplete 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
SITE NAVIGATION CODE

Simple Menu System
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 6
    2006-12-12

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    Try It Free
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    This file builds a simple vertical menu. It runs off of a database table `tbl_menu` with fields:

    menu_id (int, pk)
    menu_name (varchar)
    menu_id_parent (int)
    menu_link (varchar)
    display_page (varchar)
    sort_order (int)

    menu_id_parent is the menu_id one page above another.
    display_page is the page to show the link on (* for all)
    sort_order dictates in what order the links should appear

    *Note that when inserting new pages into the table, you need to use the location relative to the root (i.e. /index.php, /users/cpanel.php, not ../foo.php or index.php)

    Finally, you can use CSS to change how the menu looks.
    Parent Label ("Back to: ") -> span.backlabel
    Parent Link -> a.backlink
    Menu Title -> span.menutitle
    Menu Link -> a.menulink

    By : foreignis

    <?php

    #First things first, connect to your database
    mysql_connect("foobar.com", "foo", "bar");
    mysql_select_db("foobar");

    #This is the function we will use to "backtrack" to the top
    function getParents($url)
    {
    #Find the parent for the supplied URL, if there is one.
    $sql="SELECT * FROM tbl_menu ".
    "WHERE menu_link='$url' ".
    "AND menu_id_parent IS NOT NULL";

    $result=mysql_query($sql) or die(mysql_error());
    $num=mysql_num_rows($result);

    #If we found a parent, good!
    if ($num > 0)
    {
    #Get the necessary information to construct a link for the parent
    $sql="SELECT * FROM tbl_menu ".
    "WHERE menu_id='".mysql_result($result, 0, "menu_id_parent")."'";
    $result=mysql_query($sql) or die(mysql_error());
    $name=mysql_result($result, 0, "menu_name");
    $url=mysql_result($result, 0, "menu_link");

    #Now that we have our information, we will recursively call the getParents
    #with the new URL. We do this prior to our echo for the purpose of printing
    #the path in reverse order (from home down as opposed to bottom-up)
    getParents($url);

    #Once all the recursive calls above this one have returned, display link
    echo "<span class=\"backlabel\">Back to: </span><a class=\"backlink\" href=\"$url\">$name</a><br>";
    }
    }

    #find the current page
    $current_page=$_SERVER['PHP_SELF'];

    #get the path to this page, create links.
    getParents($current_page);

    #pull out information for this page from the database
    $sql="SELECT * FROM tbl_menu WHERE menu_link='$current_page'";
    $result=mysql_query($sql) or die(mysql_error());

    #if we found anything, display the name of the page as the menu's title
    if (mysql_num_rows($result) > 0)
    {
    $menu_title=mysql_result($result, 0, "menu_name");
    echo "<span class=\"menutitle\">$menu_title</span>";
    }

    #now, get everything that isn't the current page that is set either to display on this page or on all pages (*).
    $sql="SELECT * FROM tbl_menu ".
    "WHERE menu_link != '$current_page' ".
    "AND (display_page='$current_page' OR display_page='*') ".
    "ORDER BY sort_order";
    $result=mysql_query($sql) or die(mysql_error());
    $num=mysql_num_rows($result);

    #display a link for each row returned
    for($i=0; $i<$num; $i++)
    {
    $url=mysql_result($result, $i, "menu_link");
    $name=mysql_result($result, $i, "menu_name");
    echo "<br><a class=\"menulink\" href=\"$url\">$name</a>";
    }
    ?>
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Site Navigation Code Articles
    More By Codewalkers

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! A Layered approach to delivering security-rich Web applications

    As businesses grow increasingly dependent upon Web applications to provide services to customers, employees and partners, these complex applications become more difficult to secure. Although traditional security solutions protect Internet infrastructure layers, they do not guard against HTTP and HTML attacks. Many organizations that conduct security testing still deploy applications that allow attackers to manipulate their logic and wreak havoc on their business. To mitigate this risk, development and delivery teams must address Web application security throughout the lifecycle, addressing the many layers detailed in this paper.
    FREE! Go There Now!


    NEW! Don't wait! Try the Rational Application Developer (RAD) v7.5 open beta code today

    Download the Rational Application Developer (RAD) v7.5 open beta code and start developing applications for the JEE5 standard which features EJB3.0, JPA, JSF 1.2, JSP 2.1 and Servlet 2.5 standards. When you use this beta you will see how you can increase developer productivity for already existing applications with improved support for refactoring, as well as adding new features to existing applications. In addition, the beta provides tooling for JD Edwards, Oracle, SAP, Siebel and PeopleSoft to improve the developer productivity with these enterprise systems.
    FREE! Go There Now!


    NEW! Harnessing the power of SQL and Java for high performance data access

    Join this webcast to see how IBM Data Studio Developer and pureQuery can take the pain out of Java data access. uApplications developed using both Java and SQL have become a common requirement. Database connectivity using Java Database Connectivity (JDBC) to create an application is a multi-step tedious process, and tooling that covers both SQL and Java has been unavailable, until now. IBM Data Studio introduces the pureQuery platform: a high-performance, Java data access platform focused on simplifying the tasks of developing, managing, and optimizing database applications and services.
    FREE! Go There Now!


    NEW! Improve your build process with IBM Rational Build Forge, Part 2: Automate builds for a real-world Tomcat project

    Learn how Rational Build Forge can extend a simple compile and package build process by adding customization and deployment capability. Go from a manual method to automating: checking for code changes; getting the latest source; compiling and packaging; customizing; copying to and restarting a deployment server; and sending e-mail notification that a new version is available.
    FREE! Go There Now!


    NEW! Rational Talks to You:Per Kroll on Rational Method Composer Plug-in customization

    Join this Rational Talks to You teleconference on December 11 at 1:00 pm ET to get tips on building your own plugins with Rational Method Composer. Get your questions answered!
    FREE! Go There Now!


    NEW! Rational Talks to You: Grady Booch on Architecture

    Join this Rational Talks to You teleconference on November 29 at 1:00 pm ET to participate in an interactive discusssion with Grady Booch around architecture and reuse. Get your questions answered!
    FREE! Go There Now!


    NEW! Trial download: IBM Informix Dynamic Server Express Edition V11.0

    Informix Dynamic Server (IDS) Express Edition offers outstanding online transaction processing (OLTP) database performance, while helping to simplify and automate many of the tasks associated with deploying databases for small business applications. IDS 11 further extends the ease of management and applications integration with the Admin API and Scheduler, high availability with Continuous Log Restore for backup server recovery in case of a primary server failure, and column level encryption to protect personal and company private data.
    FREE! Go There Now!


    NEW! Try IBM Rational Asset Manager V7.0 online!

    You can now evaluate IBM Rational Asset Manager V7.0 online without installing or configuring it on your own system! Rational Asset Manager helps create, modify, govern, find, and reuse any type of development assets, including SOA and systems development assets. Rational Asset Manager helps you reduce software development costs and improve quality by facilitating the reuse of all types of software development-related assets. Visit developerWorks to learn more about this product and register to explore its capabilities online.
    FREE! Go There Now!


    NEW! Webcast: Application security testing and Web compliance

    Join the IBM Watchfire team for an informative discussion on techniques and best practices to proactively manage Web application security and how to effectively build application security testing into the software development lifecycle (SDLC). In this Software Delivery Platform webcast you will learn: How to better understand potential web application security vulnerabilities, best practices and how to effectively integrate application security testing into the software development lifecycle, the importance of detecting and removing software vulnerabilities during application development.
    FREE! Go There Now!


    NEW! Webcast: Eclipse: Empowering the universal platform

    The Eclipse community is constantly working to extend Eclipse's functionality. In this webcast, learn about some of the most important and feature-rich projects under development. From multi-language support to plug-in development, tune in to see what Eclipse is capable of now.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    SITE NAVIGATION CODE ARTICLES

    - Simple Menu System
    - Simply image viewer script
    - Simple File Lister
    - Dynamic Error Pages
    - BSoftEditor
    - Yahoo Status
    - Page numbers
    - PHP Search Navigator 1.0
    - Simple Page Navigation
    - An easy page browser ( prev 6 7 8 9 10 next )
    - AutoIndex PHP Script (Directory Indexer)
    - Bs_HtmlNavigation (Navigation and Sitemap cl...
    - Another Paging with Stage
    - Website Navigation via PHP
    - MySQL Paging Class





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway