-1

我正在创建一个具有回复功能的评论系统,但我发现遇到了问题。

评论表

id post_id 评论回复

0 17 废话 2

1 17 你好 2

2 17 再见         

3 17 很棒 1

4 17 坏

我的代码

$comments = mysql_query("SELECT * FROM comment WHERE post_id='$post_id' ORDER BY id DESC");

while($comment = mysql_fetch_assoc($comments))
{
    $id = $comment['id'];

    $comment = $comment['comment'];

    $reply = $comment['replyTo'];

    echo $comment;

    echo "<br />";


    $replyQuery = mysql_query("SELECT * FROM comment WHERE replyTo='$id' ORDER BY id DESC");

    while($comment = mysql_fetch_assoc($comments))

    {

        $id = $comment['id'];

        $comment = $comment['comment'];

        $reply = $comment['replyTo'];

        echo $comment;

        echo "<br />";

     }

}

我的结果

废话

伟大的

你好

再见

      废话

      你好

伟大的

坏的

预期结果

再见

      废话

      你好

      伟大的

坏的

谢谢!!!!!

4

4 回答 4

0

第一行应为:

$comments = mysql_query("SELECT * FROM comment WHERE post_id='$post_id' AND replyTo IS NULL ORDER BY id DESC");

以便只包含顶级评论。

于 2012-09-29T05:53:15.013 回答
0

试试这个查询,它将在一个查询中包含所有结果。

SELECT GROUP_CONCAT(id) AS id,GROUP_CONCAT(post_id) AS post_id,GROUP_CONCAT(comment) AS comment,GROUP_CONCAT(replyTo) AS replyTo FROM comment WHERE post_id=17 GROUP BY replyTo ORDER BY id DESC

结果看起来像

id    post_id   comment      replyTo

3      17           great           1

2,4    17,17        goodbye,bad    0,0

0,1    17,17        Blah,hello     2,2

我不清楚你的要求。所以这只是一个建议......

于 2012-09-29T06:16:32.630 回答
0
<?php
$comments = mysql_query("SELECT GROUP_CONCAT(id) AS id,GROUP_CONCAT(post_id) AS     post_id,GROUP_CONCAT(comment) AS comment,GROUP_CONCAT(date) AS date,GROUP_CONCAT(submittedBy)  AS submittedBy,GROUP_CONCAT(replyTo) AS replyTo FROM comment WHERE post_id='$post_id' GROUP BY replyTo ORDER BY id DESC");
while($comment = mysql_fetch_assoc($comments))
{
$cid = $comment['id'];
$comment_submit = $comment['submittedBy'];
$submitted_comment = $comment['comment'];
$comment_date = $comment['date'];
$replyTo = $comment['replyTo'];

echo'<div id="c'.$id.'" class="comment_container">';
    echo'<div class="comment_header">';
        echo'<a href="#" class="report">Flag</a>';
        echo'<a href="#" class="reply">Reply</a>';
        echo'<span>';
        echo $comment_submit.' - '.$comment_date.'</span>';
    echo'</div>';
    echo'<p>';
        echo $submitted_comment;
    echo'</p>';
    echo'
    <form method="post" class="replyForm">
        <textarea name="post_data_comment" class="replyTextArea" placeholder="@'.$comment_submit.'"></textarea>
        <input type="hidden" name="post_id" value="'.$post_id.'" />
        <input type="hidden" name="replyTo" value="'.$cid.'" />
        <div class="row_three">
            <span class="char_left_reply">Characters left: <span>500</span></span>
            <input type="button" value="Reply" name="share_reply" class="reply_comment" />
        </div>
    </form>
    ';
echo'</div>';

$reply = mysql_query("SELECT * FROM comment WHERE replyTo='$cid' ORDER BY id DESC");
while($replies = mysql_fetch_assoc($reply))
{
    $id = $replies['id'];
    $comment_submit = $replies['submittedBy'];
    $submitted_comment = $replies['comment'];
    $comment_date = $replies['date'];
    $replyTo = $replies['replyTo'];

    echo'<div id="c'.$id.'" class="comment_container" style="width:90%; float:right" >';
        echo'<div class="comment_header">';
            echo'<a href="#" class="report">Flag</a>';
            echo'<a href="#" class="reply">Reply</a>';
            echo'<span>';
            echo $comment_submit.' - '.$comment_date.'</span>';
        echo'</div>';
        echo'<p>';
            echo $submitted_comment;
        echo'</p>';
        echo'
        <form method="post" class="replyForm">
            <textarea name="post_data_comment" class="replyTextArea" placeholder="@'.$comment_submit.'"></textarea>
            <input type="hidden" name="post_id" value="'.$post_id.'" />
            <input type="hidden" name="replyTo" value="'.$cid.'" />
            <div class="row_three">
                <span class="char_left_reply">Characters left: <span>500</span></span>
                <input type="button" value="Reply" name="share_reply" class="reply_comment" />
            </div>
        </form>
        ';
    echo'</div>';
}

} ?>

于 2012-09-29T06:27:49.277 回答
0

请试试

$comments = mysql_query("SELECT distinct(comment),id,replyTo FROM comment WHERE post_id='$post_id' ORDER BY id DESC");

while($comment = mysql_fetch_assoc($comments))
{
    $id = $comment['id'];

    $comment = $comment['comment'];

    $reply = $comment['replyTo'];

    echo $comment;

    echo "<br />";


    $replyQuery = mysql_query("SELECT * FROM comment WHERE replyTo='$id' and comment <>  '$comment' ORDER BY id DESC");

    while($comment = mysql_fetch_assoc($comments))

    {

        $id = $comment['id'];

        $comment = $comment['comment'];

        $reply = $comment['replyTo'];

        echo $comment;

        echo "<br />";

     }

}
于 2012-09-29T07:11:38.643 回答