Flexible Database Driven Menu

Started on the old forum in October 2009 · view the original in the archive · old links redirect here

No replies · 674 views · started 17 years ago · 2 min read

riderz Lead · 975 posts #1

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

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.

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.

<?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:

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

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close