您好,我今天整天都在工作,实际上我真的迷路了,我现在有一张这样的桌子。
id |类别| parent_id
id = 5类别=笔记本电脑parent_id = 0
id = 7类别=房屋parent_id = 0
id = 8类别= HP parent_id = 5
id = 9类别=联想parent_id = 5
所以我想做的就是把它作为category sub category样式之类的东西;
笔记本电脑
生命值
联想
房屋
直到知道我有什么
<?php
include_once("dir/db.php");
$result = $db->query("SELECT * FROM categories WHERE parent_id = 0");
?>
<ul>
<?php
while($row = $result->fetch(PDO::FETCH_ASSOC)):
?>
<a href="gallerycato.php?id=<?php echo $row['id']?>"><li><?php echo $row['category'] ?></li></a>
<?php endwhile;?>
</ul>
输出;
<ul>
<a href="gallerycato.php?id=5"><li>Laptops</li></a>
<a href="gallerycato.php?id=7"><li>Houses</li></a>
</ul>
但是我想让我的联想和惠普笔记本电脑使用,如果您能帮助我,我将非常高兴!谢谢。
最佳答案
<?php
include_once("dir/db.php");
$result = $db->query("SELECT * FROM categories WHERE parent_id = 0");
echo "<ul>";
while($row = $result->fetch(PDO::FETCH_ASSOC)):
echo "<a href=\"gallerycato.php?id=" . $row['id'] ."\"><li>" . $row['category'] . "</li></a>";
$sub_result = $db->query("SELECT * FROM categories WHERE parent_id = " . $row['id']);
echo "<ul>";
while($sub_row = $sub_result->fetch(PDO::FETCH_ASSOC)):
echo "<a href=\"gallerycato.php?id=" . $sub_row['id'] ."\"><li>" . $sub_row['category'] . "</li></a>";
endwhile;
echo "</ul>";
endwhile;
echo "</ul>";
?>