php - 如何使用 PHP 和 MySQL 从数据库检索数据并缩进返回的结果?

标签 php mysql sql

为了正确看待这个计划,我正在尝试创建一个迷你公告。

这是我的 table :

CREATE TABLE Postings
(
`PostID` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`PostDate` DATETIME NOT NULL ,
`PostedBy` VARCHAR( 255 ) NOT NULL ,
`PostSubject` VARCHAR( 255 ) NOT NULL ,
`Content` VARCHAR( 255 ) NOT NULL ,
`ParentPost` INT( 11 ) NULL ,
PRIMARY KEY (  `PostID` )
)

这是该表中的数据:

insert into postings values  
        ('1','11/01/03 10:15','a@abc.com','Welcome','Welcome to the bulletin   board',NULL);
insert into postings values  
        ('2','12/01/03 08:00','b@abc.com','Welcome 2','This is posting 2','1');
insert into postings values  
        ('3','13/01/03 09:00','c@abc.com','Welcome 3','This is posting 3','1');
insert into postings values  
        ('4','14/01/03 10:15','a@abc.com','New Topic','This is posting 4',NULL);
insert into postings values  
        ('5','15/01/03 10:15','a@abc.com','New Topic 2','This is posting 5','4');
insert into postings values  
        ('6','16/01/03 10:15','a@abc.com','New Topic 3','This is posting 6','4');
insert into postings values  
        ('7','17/01/03 10:15','c@abc.com','Welcome 4','This is posting 7','2');
insert into postings values  
        ('8','18/01/03 10:15','d@abc.com','Welcome 5','This is posting 8','2');
insert into postings values  
        ('9','19/01/03 10:15','a@abc.com','Welcome 6','This is posting 9','8');
insert into postings values  
        ('10','20/01/03 10:15','b@abc.com','New Topic 4','This is posting 10','6');

PostID是自动递增的,并且是Primary Key,ParentPost也是INT,并且将有值分配给它。

现在我想做的是编写一个 SQL 语句和 PHP 代码,使我能够从数据库中检索所有值,但缩进具有 ParentPost 值等于 PostID 行下方的 PostID 值的每一行.

我在底部有一张图片,可以更好地解释我所追求的内容。

这是我迄今为止所想出的,但运气不佳:

<?php

$db_host = "localhost";
$db_username = "root";
$db_pass = "";
$db_name = "OnlineBulletinBoardDB";

@mysql_connect("$db_host","$db_username","$db_pass") or die ("Could no connect to    MySQL");
@mysql_select_db("$db_name") or die ("No database");

$query="SELECT PostSubject FROM postings GROUP BY PostID = ParentPost;";

$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

?>

<?php
$i=0;
while ($i < $num) {
$dno=mysql_result($result,$i,"PostSubject");
?>

<?php echo $dno; ?><br/>

<?php
$i++;
}
?>

我正在寻找的最终结果如下图所示:enter image description here

干杯,伙计们。

最佳答案

递归是关键。为了避免执行大量查询,例如“select * from Postings where ParentID = 0”、“select * from Postings where ParentID = 1”等,请执行一个查询“select * from Postings”,该查询会返回所有内容,然后用Parent id 第一个数组的键。下面的示例未经测试,但经过一些整理后应该可以使用:

<?php

$db_host = "localhost";
$db_username = "root";
$db_pass = "";
$db_name = "OnlineBulletinBoardDB";

@mysql_connect("$db_host","$db_username","$db_pass") or die ("Could no connect to    MySQL");
@mysql_select_db("$db_name") or die ("No database");

$query="SELECT PostSubject, PostID, ParentID FROM postings";

$res = mysql_query($query);


while($row = mysql_fetch_assoc($res){
    $parentID = intval($row['ParentID']);
    $posts[$parentID][] = $row;
}

function displayPosts($parentId, $posts){
    echo "<ul>";
    foreach($posts[$parentId] as $post){
       echo "<li>";
           echo $post['PostSubject'];
           if(isset($posts[$post['PostID']])){
               displayPosts($post['PostID'],$posts);
           }
       echo "</li>";
    }
    echo "</ul>";
}

displayPosts($posts['PostID'],$posts);

关于php - 如何使用 PHP 和 MySQL 从数据库检索数据并缩进返回的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6137943/

相关文章:

php - 如何在构造函数中调用实体管理器?

javascript - 使用 PHP 和 Angular.js 进行删除操作时遇到问题

mysql - SQL查询问题

java sql错误列的类型是整数,但表达式的类型是字符变化

sql - Oracle 左外连接查询

php - 连接到 MySql DB (PHP) 时遇到问题

php - 选择最后一个和下一个记录时 Symfony DQL 语法错误

mysql - 如何使用 mysql 查询分割这个字符串?

mysql - Mysql中如何删除某个代码范围

mysql - MySQL 中的动态交叉表查询