我有两个名为“父母”和“孩子”的mysql表
在我父母的表中,我有 4 列(id、link、lable、have_childs)
在我的孩子表中,我也有 4 列(id、c_link、c_lable、parent_id)
我使用这样的查询获取值
"SELECT parents.*, childs.* FROM parents, childs WHERE parents.id = childs.p_id;"
然后使用 foreach 循环我得到了这个结果
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" }
然后我做了这个
<?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
}
?>
然后我得到了这个结果
.Vegitable
carrot
.Fruits
grapes
.Vegitable
beet
但这不是我要找的结果,我需要胡萝卜和甜菜都放在蔬菜下面。
有什么办法吗?