I have a table where i am saving category and product name :
+-----------------------+ | products | +-----------------------+ | id name cat_name | | 1 lily flower | | 2 hibiscus flower | | 3 cat animal | | 4 dog animal | +-----------------------+
I want to have a select list with first cat_name and inside that product 'name'
**flower**
-lily
-hibiscus
**animal**
-cat
-dog
how this can be achieved in select list. please help.
In html body it is the optgroup tag that groups options.
<select>
<optgroup label="flower">
<option value="whatever">lily</option>
<option value="whatever">hibiscus</option>
</optgroup>
...
</select>
Hope you get the idea. Note that these optgroups look pretty poor. Maybe you need some jquery plugin in additon to get what you want.
if you can get array like this
array(
array(
'id' => 1
'name' => 'lily',
'cat_name' =>'flower'
),
array(
'id' => 2
'name' => 'hibiscus',
'cat_name' =>'flower'
),
.....
);
then group data by cat
$groups = array();
foreach ($results as $rs) {
$key = $rs['cat_name'];
if (!isset($groups[$key])){
$groups[$key] = array();
}
$groups[$key][] = $rs;
}
then loop $groups , echo html option , like
//this not php code
loop groups {
echo optgroup opentag
loop groups[cat_name]{
echo option tag
}
echo optgroup close tag
}