I am currently making an product page. I need a selectbox displaying multilevels
of categories where to add the product in. Need something like (php/mysql dynamic):
<option>Workstations</option>
<option>--Macs</option>
<option>--Windows</option>
<option>Software</option>
<option>--Image editing</option>
<option>----Pro</option>
<option>----Semi pro</option>
My mysql fields are, cat_id, cat_name, cat_parent.
I have the following code, so is there any way to customize it to work with a selectbox insted of ul/li?
function display_children($parent, $level) {
$result = mysql_query("SELECT a.id, a.label, a.link, Deriv1.Count FROM `menu` a LEFT OUTER JOIN (SELECT parent, COUNT(*) AS Count FROM `menu` GROUP BY parent) Deriv1 ON a.id = Deriv1.parent WHERE a.parent=" . $parent);
echo "<ul>";
while ($row = mysql_fetch_assoc($result)) {
if ($row['Count'] > 0) {
echo "<li><a href='" . $row['link'] . "'>" . $row['label'] . "</a>";
display_children($row['id'], $level + 1);
echo "</li>";
} elseif ($row['Count']==0) {
echo "<li><a href='" . $row['link'] . "'>" . $row['label'] . "</a></li>";
} else;
}
echo "</ul>";
}
display_children(0, 1);
Regards, Simon
Something like this would do the trick:
I think you could also simplify your query slightly
Reworked example
The following would allow you to complete the whole process with 1 query. It uses PHP 5.3 due to closures.