Flexible Database Driven Menu

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Flexible Database Driven Menu

    In this tutorial we'll see how to create a fully database driven menu.
    Our menu will work fine also if we decide to insert some links later in time as we'll use a position index to sort the menu links.
    First we need to create the database table for our menu.
    You can use this script to create the table.

    The fields we'll use are:

    id - we just to have a primary key in our table, it can also be useful if we want to add additional submenues as tables
    name - is what will be displaied as the text of the menu item
    position - is the position of the menu item, we'll go with 10 by 10 steps so other items can be added later
    link - is where the link sill send the visitor when clicked
    Code:
    CREATE TABLE `menu` (
    `id` int(3) NOT NULL auto_increment,
    `name` varchar(20) NOT NULL default '',
    `position` int(3) NOT NULL default '0',
    `link` varchar(100) NOT NULL default '',
    PRIMARY KEY (`id`)) ENGINE=MyISAM;
    Now let's populate our table with some dummy stuff; you can run this script (into your phpmyadmin or whatsoever you are using) to add stuff in your own table.
    Code:
    INSERT INTO `menu` VALUES (1, 'Home', 0, 'index.php?maincat=home');
    INSERT INTO `menu` VALUES (2, 'Forums', 10, 'index.php?maincat=forums');
    INSERT INTO `menu` VALUES (3, 'Tutorials', 20, 'index.php?maincat=tutorials');
    INSERT INTO `menu` VALUES (4, 'About Us', 30, 'index.php?maincat=about');
    INSERT INTO `menu` VALUES (5, 'Contact Us', 40, 'index.php?maincat=contact');
    Now that our stuff is stored into the database we neet to retrieve it and display it on our pages.
    We need to connect to the database and run the query that retrieves the content of the menu table.
    You can find out how to connect to a database in this tutorial.
    Then we run this php lines to run the query and display the results.
    Code:
    <?php
    $count = 0; //This is used to avoid that "|" is printed before the first menu item
    $query = 
    mysql_query("SELECT * FROM menu ORDER BY position ASC") or die(mysql_error());
    while ($row = mysql_fetch_object($query)) 
      { 
        if ($count!=0){echo " | ";}
        echo "<a href=\"$row->link\" alt=\"$row->name\">".$row->name."";
        $count++;
      }
    ?>
    This will display the content of our table as:
    Code:
    Home | Forums | Tutorials | About Us | Contact Us
    and obviously each menu item will have its appropriate link.
    If you decided to add a new menu item and you wanted it to be displayed, for example, between About Us and Contact Us, you should just add it with some position value bigger than 30 and smaller than 40; it will be automatically displayed in the right position.
    ________________
    Jacques
    jacques@gw-designs.co.za
    http://coding.biz.tm
    Come join and lets make it a place to learn all the noobies how to code
    __________________

    NEVER FORGET TO CLICK THE TANX BUTTON IF U LIKE WHAT IM SHARING OR HELPING WITH
Working...
X