I asked a similar question earlier but I'll ask it again in a different way because I re-worked the code a little.
I was wondering how can I indent categories and endless sub categories that I have in a select drop down menu using PHP & CSS?
Here is my PHP code to display the select drop down.
echo '<select name="parent_id">
<option value="0">None</option>';
function make_list ($parent) {
global $option;
foreach ($parent as $id => $cat) {
echo '<option value="' . $cat['id'] . '">' . $cat['category'] . '</option>';
if (isset($option[$id])) {
make_list($option[$id]);
}
}
}
$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT * FROM categories ORDER BY parent_id, category ASC");
if (!$dbc) {
print mysqli_error();
}
$option = array();
while (list($id, $parent_id, $category) = mysqli_fetch_array($dbc, MYSQLI_NUM)) {
$option[$parent_id][$id] = array('category' => $category, 'id' => $id, 'parent_id' => $parent_id);
}
make_list($option[0]);
echo '</select>';
Here is the output.
1. Apple
2. Arts & Entertainment
1. Amusement
2. Art
3. Artists
1. A
1. a1
2. a2
2. B
3. C
4. D
3. Automotive
4. Network
5. Server
6. Web Design
1. CSS
2. HTML
The numbers are just there to see the categories and sub categories easier.