I have a menu that uses PHP generated HTML to create jQuery dropdown menu. The information used to build the menu is coming from a database table of categories:
cats (db table)
cat_id | cat_name | cat_parent | cat_short __________________________________________ 1 | Home | 0 | home __________________________________________ 2 | Games | 1 | games __________________________________________ 3 | Checkers | 2 | checkers
The code that I have built so far only allows for 2 levels in the menu, and won't add another dropdown for a 3rd level (ie Checkers):
<ul class="dropdown">
<?php
$sql ='SELECT * FROM cats WHERE cat_parent = 0';
$result = $db->sql_query($sql);
while($row = $db->sql_fetchrow($result)){
$cats[] = array(
'id' => $row['cat_id'],
'name' => $row['cat_name'],
'parent' => $row['cat_parent'],
'short' => $row['cat_short'],
'subs' => array()
);
}
foreach($cats as $cat){
$sql = 'SELECT * FROM cats WHERE cat_parent = '.$cat['id'];
$result = $db->sql_query($sql);
while($row = $db->sql_fetchrow($result)){
$cat['subs'][] = array(
'id' => $row['cat_id'],
'name' => $row['cat_name'],
'parent' => $row['cat_parent'],
'short' => $row['cat_short']
);
}
$menu[] = $cat;
}
foreach($menu as $cat){ ?>
<li class="ui-state-default"><a class="transition" href="index.php?mode=<?php echo $cat['short']; ?>"><?php echo $cat['name']; ?></a>
<?php if($cat['subs'] != '0'){ ?>
<ul class="sub_menu">
<?php foreach($cat['subs'] as $sub){ ?>
<li class="ui-state-default"><a class="transition" href="index.php?mode=<?php echo $sub['short']; ?>"><?php echo $sub['name']; ?></a></li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
How can I rewrite this (or write a function) to be able to utilize 3 layers? I only need the PHP loop necessary, as I can easily manipulate the list item elements with jquery to perform the actual drop down.