i have this category MySql table:
id , name , parent
Now with this function
i list categories in selectbox
and optgroup
html
tags:
$options = array();
$categories = DataAccess::fetch("
SELECT
* , p.name AS parent
FROM
cats AS child
INNER JOIN
cats AS p ON p.id = child.parent
");
foreach($categories as $category){
$parent = $category['parent'];
if (! isset($options[$parent])) $options[$parent] = array();
$options[$parent][$category['id']] = $category['name'];
}
echo '<select>';
$selid = 13;
foreach($options as $group => $option) {
printf('<optgroup label="%s">', $group);
foreach ($option as $id => $label) {
$selcat = ($selid == $id) ? 'selected' : '';
printf('<option %s value="%s">%s</option>',$selcat, $id, $label);
}
printf('</optgroup>');
}
echo "</select>";
This function ouhput worked for One child
parent categories and output is:
<select class="selectpicker" >
<optgroup label="News">
<option selected value="13">War</option>
</optgroup>
<optgroup label="article">
<option selected value="14">tech</option>
</optgroup>
</select>
Now, I have Three problem:
1- if i create many child This function notwork and print only One Child
. i need to Like This:
News
--Sport
--War
--tech
Article
--tech
--php
--linux
2 - in need to print Multilevel
categories with optgroup
Like This:
News
--Sport
----Sky
----Football
------Football-1
--War
----War1
--tech
Article
--tech
----tech2
------tech3
--php
--linux
3 - Not Print categories if parent = 0 (root), This print only categories with child parent.
how do fix/print this?