creating a dynamic menu using two mysql tables
I have two mysql tables called "parents" and "childs"
in my parents table i have 4 columns (id,link,lable,have_childs)
in my childs table also i have 4 columns (id, c_link, c_lable, parent_id)
and i get the values using a query like this
"SELECT parents.*, childs.* FROM parents, childs WHERE parents.id =
childs.p_id;"
then using a foreach loop i got this result
array(7) { ["id"]=> string(1) "1" ["link"]=> string(3) "veg" ["lable"]=>
string(9) "Vegitable" ["childs"]=> string(1) "1" ["c_link"]=> string(6)
"carrot" ["c_lable"]=> string(6) "carrot" ["p_id"]=> string(1) "1" }
array(7) { ["id"]=> string(1) "2" ["link"]=> string(3) "Fru" ["lable"]=>
string(6) "Fruits" ["childs"]=> string(1) "1" ["c_link"]=> string(6)
"grapes" ["c_lable"]=> string(6) "grapes" ["p_id"]=> string(1) "2" }
array(7) { ["id"]=> string(1) "3" ["link"]=> string(3) "veg" ["lable"]=>
string(9) "Vegitable" ["childs"]=> string(1) "1" ["c_link"]=> string(5)
"beeat" ["c_lable"]=> string(5) "beeat" ["p_id"]=> string(1) "1" }
then i did this
<?php
foreach($result as $myresult){ ?>
<ul>
<li><a href="<?php echo $myresult['link']; ?>"><?php echo
$myresult['lable']; ?></a>
<?php
if($myresult['childs'] == 1){
echo '<div><ul>';
echo '<li><a
href="'.$myresult['c_link'].'">'.$myresult['c_lable'].'</a></li>';
echo '</div></ul>';
}
?>
<?php
}
?>
then i got this result
.Vegitable
carrot
.Fruits
grapes
.Vegitable
beet
but this is not the result i looking for i need both carrot and beet items
go under vegetable.
is there any way to do this?
No comments:
Post a Comment