I have this mysql table "categories":
id category parent
1 category1 NULL
2 subcategory1 1
3 category2 NULL
4 subcategory2 2
and I want to get this result:
category1
subcategory1
subcategory2
category2
to get this result I use this code, but is very slow:
foreach($db->query("SELECT * FROM categories WHERE parent IS NULL") as $parent)
{
$t=0;
categories($db,$t,$parent['id'],$parent['category']);
}
function categories($db,$t,$id,$category)
{
echo"<option value=".$id.">";//
for($i=0;$i<$t;$i++) {echo" "; $GLOBALS['cat'].=" ";}
echo $category."</option>";//" ".$id.
$GLOBALS['cat'].=$category."<br>";
$t++;
if($db->query("SELECT * FROM categories WHERE parent=".$id)->rowCount()>0)
{
foreach($db->query("SELECT * FROM categories WHERE parent=".$id) as $child)
categories($db,$t,$child['id'],$child['category']);
}
}
Do you have a faster solution?
Thanks