Ok. As promised this is a different way to do things..
It requires 2 queries but I don't think that will break
the bank :)
It looks like more code than it is because I have fully
commented it. Just mail me if you have any problems.
Cheers,
Justin :)
// ===================================================
// This is the new table structure that you will need..
// (only one table required)..
CREATE TABLE menu
(
my_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
parent_id INT UNSIGNED,
cat_title VARCHAR(32),
cat_prefix VARCHAR(32),
game VARCHAR(100),
description TEXT,
PRIMARY KEY (my_id)
);
// ===================================================
// Some test data for the above table..
//
// 4 top headings with my_id: 1-4 (parent_id set to 0)
INSERT INTO menu (parent_id,cat_title) VALUES (0,'Games');
INSERT INTO menu (parent_id,cat_title) VALUES (0,'Ftp');
INSERT INTO menu (parent_id,cat_title) VALUES (0,'Email');
INSERT INTO menu (parent_id,cat_title) VALUES (0,'Text');
// Sub headings with parent_id set as 1-4
INSERT INTO menu (parent_id,game) VALUES (1,'Grand Theft Auto');
INSERT INTO menu (parent_id,game) VALUES (1,'Ultra Tournament');
INSERT INTO menu (parent_id,game) VALUES (1,'Pokemon');
INSERT INTO menu (parent_id,game) VALUES (2,'Cute FTP');
INSERT INTO menu (parent_id,game) VALUES (2,'Zappy Get');
INSERT INTO menu (parent_id,game) VALUES (3,'Eudora');
INSERT INTO menu (parent_id,game) VALUES (3,'Mikes Hack Mail');
INSERT INTO menu (parent_id,game) VALUES (3,'Get Mail');
INSERT INTO menu (parent_id,game) VALUES (4,'Ultra Edit');
INSERT INTO menu (parent_id,game) VALUES (4,'Text Pad');
Note: This solution requires a database class that I have
written that makes it very easy to work with databases.
It is only one file (ez_sql.php) and can be downloaded
from http://php.justinvincent.com (just add your db settings
to it at the top of the file)
This is the actual php code that prints out the menu you are after...
<?php
// =============================================================
// PHP code required for 2 level drop down menu..
//
// (Using ezSQL DB class available from http://php.justinvincent.com
)
// =============================================================
// include the ez_sql database class making it very easy to
// to work with mySQL databases..
include_once "ez_sql.php";
// Function to print one option..
function print_option($val,$text)
{
echo "<OPTION value=$val>$text</option>\n";
}
// Select top menu level options..
$results1 = $db->get_results("SELECT * FROM menu WHERE parent_id = 0
ORDER BY cat_title");
// Select second level menu options..
$results2 = $db->get_results("SELECT * FROM menu WHERE parent_id !
0 ORDER BY game");
// Open the select and do some formatting..
echo "<select name=blah>";
print_option("","Downloads..");
print_option("","----------------------------");
// loop through the main results array
foreach ( $results1 as $m1 )
{
// Print top level heading..
print_option($m1->my_id,$m1->cat_title);
// for each top level heading loop through second level
headings
foreach ($results2 as $m2 )
{
// if this second level parent_id == top level id
print..
if ( $m2->parent_id == $m1->my_id )
{
print_option($m2->my_id," " . $m2->game);
}
}
// Put a space betwen options..
print_option("","");
}
// Close the select..
echo "</select>";
//
// ==============================================================
?>