0

how to display new comment at the top/first on page?

I used a code before but it,s not work when I add reply system in my comment box.

My used code is:

$count = mysql_numrows($results);
$i = 0;
while ($i < $count){

    $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");
    $date = mysql_result($results,$i,"date");

other code

$i++;}

This is my current comment structure with above code which not work as last comment first also display total comment system continuously again after again for above code:

 $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){

    $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");
    $date = mysql_result($results,$i,"date");

    //heres our alternating hack
    if($css != "depth-1"){ $css = "depth-1"; }else{ $css = "thread-alt depth-1"; }

$q = "SELECT * FROM comments WHERE parent_id = 0";
$r = mysql_query($q);
while($row = mysql_fetch_assoc($r)) // Only main comment
  { $id = $row['id'];
    $qazi_id = $row['qazi_id'];
    $username = $row['username'];
    $email = $row['email'];
    $description = $row['description'];     
    $url = $row['url'];
    $parent_id = $row['parent_id'];
    $date = $row['date'];
            echo' 
<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>
                    ';
$query = "SELECT * FROM comments WHERE parent_id ='$id'";
$res = mysql_query($query);
while($row = mysql_fetch_assoc($res)) // Only main comment
{   $id = $row['id'];
    $qazi_id = $row['qazi_id'];
    $username = $row['username'];
    $email = $row['email'];
    $description = $row['description'];     
    $url = $row['url'];
    $parent_id = $row['parent_id'];
    $date = $row['date'];
echo' <ul><li>
<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>
</li></ul>';
}}
$i++;
}echo'</ol></div></div>'; }
?> 
jainul
  • 13
  • 6
  • Please, [don't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). *They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation)*. See the [red box](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. – Jay Blanchard Sep 23 '14 at 17:29
  • Thank you, i am seeing this now. – jainul Sep 24 '14 at 00:38
  • PDO is disable in my server. I was try before to enable it but failed. So I will transfer at MySQLi soon. Thank you Jay Blanchard. – jainul Sep 24 '14 at 01:41

1 Answers1

0

Change your query to SELECT * FROM comments WHERE parent_id = 0 ORDER BY id DESC.

Jason Swett
  • 43,526
  • 67
  • 220
  • 351
  • Jason Swett, Thank u, it's working now. But if i have 6 comment total its reverse all 6 time also. That mean reverse all by total comment count. How to solved it? – jainul Sep 24 '14 at 01:30
  • I got it, reverse is now stop. Use break; before $i++; Thank u all. – jainul Sep 24 '14 at 02:05