Naprawdę bardzo łatwo jest coś takiego znaleść w Internecie... Poszukać trochę. Jest pełno przykładów oraz gotowych skryptów.
https://www.google.pl/?gws_rd=ssl#q=mysql+menuMySQL:
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);
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);
Plik:
<?php
// connect to mysql
// select the database
// create an array to hold the references
// create and array to hold the list
// 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
// loop over the results
{
// 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;
}
}
Plik ciąg dalszy:
function create_list( $arr )
{
$html = "\n<ul>\n";
foreach ($arr as $key=>$v)
{
$html .= '<li>'.$v['menu_item_name']."</li>\n";
{
$html .= "<li>";
$html .= create_list($v['children']);
$html .= "</li>\n";
}
else{}
}
$html .= "</ul>\n";
return $html;
}
echo create_list
( $list );
?>