所以我有一个表,列是 id、parent-id 和 name。
这是显示的内容。
1 Web Design
- 5 Templates
- 6 Finished Websites
2 Graphic Design
3 Photography
4 Image Manipulation
来自这里:
while($row = mysql_fetch_array( $result )) {
echo $row['id'] . " ";
echo $row['name'] . "<br/ >";
while($row = mysql_fetch_array( $result2 )) {
echo "- " . $row['id'] . " ";
echo $row['name'] . "<br/ >";
}
}
我如何动态地列出他们各自 parent 下的所有 child ,例如......
1 Web Design
- 5 Templates
- 6 Finished Websites
2 Graphic Design
- 7 T-Shirts
- 8 Logos
3 Photography
- 9 Portraits
- 10 Nature
- 11 Animal
- 12 Architecture
4 Image Manipulation
最佳答案
没关系,明白了....
// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM `portfolio-categories` WHERE `parent-id` ='0'")
or die(mysql_error());
// keeps getting the next row until there are no more to get
$parent = 0;
while($row = mysql_fetch_array( $result )) {
echo $row['id'] . " ";
echo $row['name'];
$parent += 1;
echo "#" . $parent . "#";
echo "<br/ >";
$result2 = mysql_query("SELECT * FROM `portfolio-categories` WHERE `parent-id`=$parent")
or die(mysql_error());
while($row = mysql_fetch_array( $result2 )) {
echo "- " . $row['id'] . " ";
echo $row['name'] . "<br/ >";
}
}
关于mysql - 将 MySQL 条目排序为子项和父项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4176707/