I want to display categories and subcategories in a select list (drop-down list) as shown below.

This is how I tried this in PHP:
while ($stmt->fetch()) {
$cats[$parent][$id] = $name;
}
function displayList(&$cats, $parent, $level=0) {
if ($parent==0) {
foreach ($cats[$parent] as $id=>$nm) {
displayList($cats, $id);
}
}
else {
foreach ($cats[$parent] as $id=>$nm) {
echo "<option>$nm</option>\n";
if (isset($cats[$id])) {
displayList($cats, $id, $level+1);
}
}
}
}
echo '<select>';
displayList($cats, 0);
echo '</select>';
This code displays all my categories in my drop-down list. But I need to add some indentation to my subcategories.
Can anyone tell how to do this.
Thanks.
source
share