我试图在我循环的答案下方发表评论(就像stackoverflow上的答案一样)但我不知道如何将正确的评论放在正确的答案下方。我正在使用模板系统。就我目前拥有的而言,它只显示最后一个 $row['id'] 的查询注释
PHP代码:
<?php
//Query setup for answers
$answerquery = "SELECT a.id, a.post_id, a.username, a.date, a.text FROM answers_tbl AS a, posts_tbl AS p WHERE a.post_id = p.id";
$result = $mysqli->query($answerquery);
//Replace answers
$layout_a = file_get_contents("tpl/question_answer_layout.html");
$search_a = array("%username%", "%date_a%", "%text_a%");
$answer_r = NULL;
while($row = $result->fetch_array()){
$replace_a = array($row['username'], $row['date'], $row['text']);
$answer_r .= str_replace($search_a, $replace_a, $layout_a);
//Query setup for comments of the question
$commentquery = "SELECT c.username, c.comment
FROM answer_comments_tbl AS c
INNER JOIN answers_tbl AS a ON a.id = c.answer_id
WHERE answer_id =".$row['id'];
$result2 = $mysqli->query($commentquery);
//Replace comments
$layout_c = file_get_contents("tpl/question_comment_layout.html");
$search_c = array("%comment_c%", "%username_c%");
$answer_c = NULL;
while($row2 = $result2->fetch_assoc()){
$replace_c = array($row2['comment'], $row2['username']);
$answer_c = str_replace($search_c, $replace_c, $layout_c);
$answer_r = str_replace("%answer_comment%", $answer_c, $answer_r);
}
}
$template = str_replace("%answer_post%", $answer_r, $template);
?>
question_answer_layout.html:
<div id="AnswerCarTop">
</div><!--Question-->
<div id="QuestionBottom">
<div id="QuestionTitle">%username%</div><!--QuestionTitle-->
<div id="Paragraph">%text_a%</div><!--Paragraph-->
%answer_comment%
question_comment_layout.html:
<div id="CommentPlaced">
<div id="Paragraph1">%comment% - <span class="bold">%username%</span></div>
<!--Paragraph--></div>