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

时间:2011-05-26 11:46:28

标签: php mysql sql

为了正确看待这个程序,我试图创建一个迷你公告。

这是我的表:

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是自动增量,是主键,ParentPost也是INT,并且会为其分配值。

现在我要做的是写一个SQL语句和PHP代码,它允许我从DB中检索所有值,但缩进具有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

干杯。

2 个答案:

答案 0 :(得分:1)

递归是关键。为了避免做大量的查询,例如“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);

答案 1 :(得分:0)

嗯,我无法编辑ejrowley's answer(我是StackOverflow的新手),所以我在递归时发布更整洁,更实用的例子:

<?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");

$posts = array();
$q = mysql_query("SELECT * FROM postings;");
while ($row = mysql_fetch_assoc($q)) {
    $parentId = $row['ParentPost'];
    $posts[$parentId ? $parentId : 0][] = $row; // because you use NULL and array can't have NULL key                                        
}

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

displayPosts(0, $posts); // where 0 means 'no parent'                                                                                        

?>