将MySQL条目作为子项和父项排序
所以我有一个表,列是id、parent id和name 下面是显示的内容将MySQL条目作为子项和父项排序,mysql,parent-child,Mysql,Parent Child,所以我有一个表,列是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/ >";
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/ >";
}
}
没关系,明白了
// 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/ >";
}
}
没关系,明白了
// 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/ >";
}
}
function items($parent_id)
{
$query = "SELECT * FROM mytable WHERE parent_id = $parent_id"
$result = mysql_fetch_array(mysql_query($query))
foreach ($result as &$res)
{
echo $res['id'] . " ";
echo $res['name'] . "";
echo items($parent_id)
}
}