獲取報銷我正在評論系統的顯示帖子,評論(家長評論)和評論repliess(孩子評論)的地方。在評論系統
MySql的意見表看起來如下:
comment_id int primary_key
post_id int // where i save the post id
author varchar
comment text
parent_id int // where i save parent comment id in case this comment is reply of another comment
現在,我的情況是,當我經過的MySQL結果集打印評論信息。正好在試圖打印子註釋(其他註釋的評論)和父註釋(註釋的帖子)時。
我目前的方法來實現這一目標,是選擇使用PHP有沒有PARENT_ID
SELECT * FROM comments WHERE parent_id = null
而遍歷所有結果的所有意見,然後再進行循環的,如果有任何的孩子每個註釋搜索內另一查詢它的評論。
<?php
$sql_result_set;
while($comment = mysql_fetch_array($sql_result_set, MYSQL_ASSOC){
// process comment info
$sql = "SELECT * FROM comments where parent_id = ".$comment['comment_id'];
$result = mysqli_query($sql);
if(mysql_num_rows($result)>0){
while($child_comments = mysql_fetch_array($result, MYSQL_ASSOC)){
// child comments of the parent
}
}
}
?>
現在,我的問題是「有沒有更好的方式做同樣的事情?」
您的幫助表示讚賞
一切順利,
這將有效地s按照特定的順序從'comments'表中選擇所有記錄。這是你真正想做的嗎? – DaveRandom
你可能想看看這個問題:http://stackoverflow.com/q/937042/1627227。這是一種稍微不同的方法,但比每個評論回覆有一個新的數據庫查詢更有效。 – martinczerwi
@DaveRandom不,他們不是所有的生產將返回限制數量的評論 –