[英]PHP: Comment reply system with quotes. How can I display the parent (quoted) message to the child (reply) message
Database structure:
post_id
comment_id
message
parent_id
SQL data:
post_id: 1
comment_id: 10
message: boohooo
parent_id: 0
------------------
post_id: 1
comment_id: 20
message: another reply
parent_id: 0
------------------
post_id: 1
comment_id: 30
messsage: you are scary
parent_id: 10
這是下面的代碼(從mysql結果轉換為php)到目前為止我所擁有的
<?php
$first_comment = new stdClass;
$first_comment->cid = '1';
$first_comment->message = 'this is the first message';
$first_comment->parent = '0';
$second_comment = new stdClass;
$second_comment->cid = '20';
$second_comment->message = 'this is the second message';
$second_comment->parent = '0';
$third_comment = new stdClass;
$third_comment->cid = '30';
$third_comment->message = 'this is a reply to the first message';
$third_comment->parent = '10';
$comments = array ($first_comment, $second_comment, $third_comment);
//print_r($comments);
foreach ( $comments as $c ) {
echo "#". $c->cid . " ". $c->message . "\n\n";
}
?>
在此php沙箱網站上可以擺弄相同的代碼: http : //ideone.com/fwHUwv
我希望這樣輸出:
#10。 噓聲
#20。 另一個答復
#30。 你很嚇人
<?php
$first_comment = new stdClass;
$first_comment->cid = '1';
$first_comment->message = 'this is the first message';
$first_comment->parent = '0';
$second_comment = new stdClass;
$second_comment->cid = '2';
$second_comment->message = 'this is the second message';
$second_comment->parent = '0';
$third_comment = new stdClass;
$third_comment->cid = '3';
$third_comment->message = 'this is a reply to the first message';
$third_comment->parent = '1';
$comments = array ($first_comment, $second_comment, $third_comment);
//print_r($comments);
foreach ( $comments as $c ) {
if($c->parent != 0) { echo "#". $c->cid . " ". $comments[$c->parent-1]->message . "\n"; echo $c->message; }
else {
echo "#". $c->cid . " ". $c->message . "\n\n";
}
}
?>
輸出:
#1 this is the first message
#2 this is the second message
#3 this is the first message
this is a reply to the first message
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.