PROBLEM: You want infinitely nested categories. Start by building a table for your categories (id, name) have field for parent_id, which would default to 0. I choose to also add the feature of priority ordering capabilities. I recommend ignoring it exists, you could ditch it completely and change the MySQL query to SORT BY name ASC instead.
CREATE TABLE `categories` (
`id` int(10) unsigned NOT NULL auto_increment,
`parent_id` int(10) unsigned NOT NULL default '0',
`name` varchar(100) NOT NULL default '',
`priority` int(11) NOT NULL default '100',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
INSERT INTO `categories` VALUES (1, 0, 'Example 1', 1);
INSERT INTO `categories` VALUES (2, 0, 'Example 2', 2);
INSERT INTO `categories` VALUES (3, 1, 'Child 1', 1);
INSERT INTO `categories` VALUES (4, 1, 'Child 2', 2);
INSERT INTO `categories` VALUES (5, 2, 'Another child', 1);
the backticks probably aren't necessary, it is what PHPMyAdmin defaulted to.
Ok, so now we have data. Lets create a dropdown thats recursive.
function recur_option($cat=0,$sp=0)
{
global $pg_content;
$q = "SELECT id,name FROM prod_cat WHERE parent_id=".$cat." ORDER BY priority ASC";
$r = mysql_query($q) or exit(mysql_error().$q);
if(mysql_num_rows($r)>0)
{
while($a = mysql_fetch_assoc($r))
{
$pg_content .= "<option value=\"".$a['id']."\">";
for($j=0;$j<$sp;$j++)
{
$pg_content .= " ";
}
$pg_content .= $a['name'];
$pg_content .= "</option>\n";
// do i have children?
$q2 = "SELECT id FROM prod_cat WHERE parent_id=".$a['id'];
$r2 = mysql_query($q2) or exit(mysql_error().$q2);
if(mysql_num_rows($r2)>0)
{
recur_option($a['id'],$sp+1);
}
}
}
}
We would then now use this function on a form like this:
$pg_content = "<select name=\"parent\">";
$pg_content .= "<option value=\"0\"> </option>";
recur_option();
$pg_content .= "</select>";
echo $pg_content;