如何在父评论下显示回复评论

时间:2014-09-23 07:15:11

标签: php mysql

我是PHPMYSQL的新手。 我在评论系统中使用最后评论show top。 当我给出任何回复时,它显示为父评论的顶部。它没有归结为父评论。

如何在家长评论下显示回复(如果),我该怎么做。

我阅读了许多不同的解决方案,但无法在我的代码中应用它们。

这是我的代码(这里parent_id是回复评论)

function getComments($tutid){
$results = mysql_query("SELECT * FROM comments WHERE qazi_id='$tutid' ORDER BY id DESC") or die(mysql_error());
$commentNum = mysql_num_rows($results);
echo' <div class="post-bottom-section">
<h2> '.$commentNum.' comments so far (<a href="#post" title="post your own">post your  own</a>)</h2>
<div class="primary">
<ol class="commentlist">';
$count = mysql_numrows($results);
$i = 0;
while ($i < $count){
    $id = mysql_result($results,$i,"id");
    $qazi_id = mysql_result($results,$i,"qazi_id");
    $username = mysql_result($results,$i,"username");
    $email = mysql_result($results,$i,"email");
    $description = mysql_result($results,$i,"description");     
    $url = mysql_result($results,$i,"url");
    $parent_id = mysql_result($results,$i,"parent_id");
    $date = mysql_result($results,$i,"date");

    echo'<ul> 
    <div class="comment-info">
    <img alt="" class="avatar" height="42" width="42" src="'.$grav_url.'"/>
    <cite><a href="'.$url.'" title="'.$username.'">'.$username.'</a> Says: <br />
    <span class="comment-data"><a href="'.$url.'" title="'.$date.'">'.$date.'</a></span>
    </cite></div>
    <div class="comment-text"><p>'.BBCode($description).'</p></div>
    <a href="#post" class="reply" id="'.$id.'">Reply</a>
    </ul>'; 
   ++$i;
    }
    echo'</ol></div></div>';
    }
?>

<?php
function submitComments($tutid2,$tuturl){
echo'
<a name="post">
<div class="post-bottom-section">
<h2>Leave a Reply</h2>
<div class="primary">
<form action="comments/submit.php" method="post" id="commentform">
<div>
<label for="username">User Name <span>*</span></label>
<input id="username" name="username" value="" type="text" tabindex="1" />
</div><div>
<label for="email">Email Address <span>*</span></label>
<input id="email" name="email" value="" type="text" tabindex="2" />
</div><div>
<label for="website">Website</label>
<input id="website" name="website" value="" type="text" tabindex="3" />
</div><div>
<label for="message">Your Message <span>*</span></label>
<textarea id="message" name="message" rows="10" cols="18" tabindex="4"></textarea>
</div><div class="no-border">
<input class="button" type="submit" name="post" value="Submit Comment" tabindex="5" title="Submit Comment" /></div>
<input type="hidden" name="parent_id" id="parent_id" value="0" />
<input type="hidden" name="tuturl" value="'.$tuturl.'" />
<input type="hidden" name="tutid2" value="'.$tutid2.'" />
</form></div></div>';}
?>

现在在下面,通过这种方式我可以得到我的主要评论

$q = "SELECT * FROM comments WHERE id ='$parent_id'";
$r = mysql_query($q);
if(mysql_num_rows($r)==0) // Only main comment
{ echo' <ul><div class="comment-info">
 <img alt="" class="avatar" height="42" width="42" src="'.$grav_url.'"/>
 <cite> <a href="'.$url.'" title="'.$username.'">'.$username.'</a> Says: <br />
 <span class="comment-data"><a href="'.$url.'" title="'.$date.'">'.$date.'</a></span>
 </cite> </div>
 <div class="comment-text"> <p>'.BBCode($description).'</p> </div>
 <a href="#post" class="reply" id="'.$id.'">Reply</a> </ul>';       
}

如果替换if(mysql_num_rows($ r)&gt; 0得到我的回复。

我的Submit.php代码

include("db.php");
if(isset($_REQUEST['post'])){
$tuturl = $_POST['tuturl'];
$qazi_id = $_POST['tutid2'];
$username = $_POST['username'];
$url = $_POST['website'];
$email = $_POST['email'];
$description = $_POST['message'];
$parent_id = $_POST['parent_id'];
$date = date("F j, Y; g:i a");
if ($parent_id == 0){
//Submit data
$query = "INSERT INTO comments         VALUES('','$qazi_id','$username','$email','$description','$url','$parent_id','$date')";
mysql_query($query);
echo "<h1></h1>";
echo "Your comment has been submitted.  You will now be redirected back to the last page you visited.  Thanks!";
echo "<meta http-equiv='refresh' content='2;URL=$tuturl'>";
} else {
if ($parent_id >= 1){
//Submit data
$query = "INSERT INTO comments     VALUES('','$qazi_id','$username','$email','$description','$url','$parent_id','$date')";
mysql_query($query);
echo "<h1></h1>";
echo "Your reply has been submitted. Thanks!";
echo "<meta http-equiv='refresh' content='2;URL=$tuturl'>";
}}}

0 个答案:

没有答案