Você está na página 1de 6

Simple Mysql PHP Menu 26/09/2014

tutorials / Simple Mysql PHP Menu

Simple Mysql PHP Menu

08 Simple Mysql PHP Menu


May By Kevin W aterson 10006872 12 Comments

There are many ways in which a menu, and menu structure can be created. Here we show how to create simple, multiple level menu system with
just a single database table.

Create the Tables


CREATE THE TABLES
The table structure is quite simple, and key to manipulating the data is in the parent child relationship created with the menu_item_id
and the menu_parent_id.

CREATE table menu_items(


menu_item_id int not null auto_increment,
menu_item_name tinytext,
menu_description tinytext,
menu_url text,
menu_parent_id int not null default 0,
PRIMARY KEY(menu_item_id)
) ENGINE=INNODB;

INSERT INTO menu_items


(menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
VALUES
(1, 'Cars', 'Stuff about cars', '#', 0);

INSERT INTO menu_items


(menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
VALUES
(2, 'Sedans', 'Sedan cars', '#', 1);

INSERT INTO menu_items


(menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
VALUES
(3, 'Station Wagons', 'Station Wagon cars', '#', 1);

INSERT INTO menu_items


(menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
VALUES
(4, 'Utes', 'You Beaut', '#', 1);

INSERT INTO menu_items


(menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
VALUES
(5, 'Big Utes', 'You Beaut', '#', 4);

INSERT INTO menu_items


(menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
VALUES
(6, 'Little Utes', 'You Beaut', '#', 4);

INSERT INTO menu_items


(menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
VALUES
(7, 'Trucks', 'Stuff about Trucks', '#', 0);

http://phpro.org/tutorials/Simple-Mysql-PHP-Menu.html 1/6
Simple Mysql PHP Menu 26/09/2014

INSERT INTO menu_items


(menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
VALUES
(8, 'Tip Trucks', 'Tippers', '#', 7);

INSERT INTO menu_items


(menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
VALUES
(9, 'Lorries', 'Bigger trucks', '#', 8);

INSERT INTO menu_items


(menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
VALUES
(10, 'Road Trains', 'LOOONG Trucks', '#', 8);

INSERT INTO menu_items


(menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
VALUES
(11, 'Boats', 'LOOONG Trucks', '#', 0);

INSERT INTO menu_items


(menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
VALUES
(12, 'Yachts', 'Sail Boats', '#', 11);

INSERT INTO menu_items


(menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
VALUES
(13, 'Power Boats', 'Fast Boats', '#', 11);

INSERT INTO menu_items


(menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
VALUES
(14, 'Dinghy', 'Small Power Boats', '#', 13);

INSERT INTO menu_items


(menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
VALUES
(15, 'Half Cabin', 'Bigger Boats', '#', 13);

INSERT INTO menu_items


(menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
VALUES
(16, 'Full Cabin', 'Even Bigger Boats', '#', 13);

INSERT INTO menu_items


(menu_item_id, menu_item_name, menu_description, menu_url, menu_parent_id)
VALUES
(17, 'Cruisers', 'Really Big Boats', '#', 13);

THE CODE
With the database in place, the code to fetch the menu is rather simple and relies on references to do the hard work.

<?php

// connect to mysql
$link = mysql_connect( 'localhost', 'username', 'passw ord' );

// select the database


mysql_select_db('my_database', $link);

// create an array to hold the references


$refs = array();

http://phpro.org/tutorials/Simple-Mysql-PHP-Menu.html 2/6
Simple Mysql PHP Menu 26/09/2014

// create and array to hold the list


$list = array();

// the query to fetch the menu data


$sql = "SELECT menu_item_id, menu_parent_id, menu_item_name FROM menu_items ORDER BY menu_item_name";

// get the results of the query


$result = mysql_query($sql);

// loop over the results


w hile($data = @mysql_fetch_assoc($result))
{
// Assign by reference
$thisref = &$refs[ $data['menu_item_id'] ];

// add the the menu parent


$thisref['menu_parent_id'] = $data['menu_parent_id'];
$thisref['menu_item_name'] = $data['menu_item_name'];

// if there is no parent id
if ($data['menu_parent_id'] == 0)
{
$list[ $data['menu_item_id'] ] = &$thisref;
}
else
{
$refs[ $data['menu_parent_id'] ]['children'][ $data['menu_item_id'] ] = &$thisref;
}
}

/**
*
* Create a HTML list from an array
*
* @param array $arr
* @param string $list_type
* @return string
*
*/
function create_list( $arr )
{
$html = "\n<ul>\n";
foreach ($arr as $key=>$v)
{
$html .= '<li>'.$v['menu_item_name']."</li>\n";
if (array_key_exists('children', $v))
{
$html .= "<li>";
$html .= create_list($v['children']);
$html .= "</li>\n";
}
else{}
}
$html .= "</ul>\n";
return $html;
}

echo create_list( $list );

?>

http://phpro.org/tutorials/Simple-Mysql-PHP-Menu.html 3/6
Simple Mysql PHP Menu 26/09/2014

DEMONSTRATION

Boats

Power Boats

Cruisers

Dinghy

Full Cabin

Half Cabin

Yachts

Cars

Sedans

Station Wagons

Utes

Big Utes

Little Utes

Trucks

Tip Trucks

Lorries

http://phpro.org/tutorials/Simple-Mysql-PHP-Menu.html 4/6
Simple Mysql PHP Menu 26/09/2014

Road Trains

Search... 

Categories

Testing

Services

Tutorials

Articles

Examples

Classes

Contact

Popular Recent

SPL Autoload
25 Sep, 2014

Easter Egg
25 Sep, 2014

Simple Mysql PHP Menu


25 Sep, 2014

Simple Mysql PHP Menu


25 Sep, 2014

Xajax-In An Object Oriented Environment


09 Sep, 2014

About Us

http://phpro.org/tutorials/Simple-Mysql-PHP-Menu.html 5/6
Simple Mysql PHP Menu 26/09/2014

Providing the best and most up to date tutorials and examples on the web. Our candidate testing service provides the best method of screen candidates to be
assured of securing the right resource for your business.

Get in Touch

Newsletter
Keep up on our alw ays evolving product features and technology. Enter your e-mail and subscribe to our new sletter.

Latest Tweet

Contact Us
Address: 1234 Street Name, City Name, United States

Phone: (123) 456-7890

Email: mail@example.com

Follow Us

I w ould rather see a sermon than hear one any day. I'd rather have you w alk w ith me, than merely point the w ay. The eye is a more ready pupil than ever w as the
ear, good advice is often confusing, but example is alw ays clear.
© Copyright 2014. All Rights Reserved.

FAQ's Sitemap Contact

http://phpro.org/tutorials/Simple-Mysql-PHP-Menu.html 6/6

Você também pode gostar