Jump to content

Reverse Ordering While Loop


Tibblez

Recommended Posts

I have a comments page and I wanted the results to be taken in reverse order out of the mysql database so that the newest would be at the top, problem is that the way I'm doing it right now leaves an empty table row before actually printing anything from the database, my code is:

 

<table align="center">
<th>Comments</th>

<?php

  $username="username";
  $password="password";
  $database="database";

  mysql_connect(localhost,$username,$password);
  @mysql_select_db($database) or die ("Could not connect: " . mysql_error());

  $query="SELECT * FROM comments";
  $result=mysql_query($query);
  $num=mysql_numrows($result);
  $i=$num;

  while ($i >= 0) {
  $name=mysql_result($result,$i,"name");
  $comment=mysql_result($result,$i,"comment");

  print "<tr>";
  print "<td><b>$name</b> <br /><br />$comment</td>";
  print "</tr>";
  
  $i--;
  
};

?>
</table>

 

Any help would be appreciated!

Link to comment
Share on other sites

Add an ID element to your table, primary key, auto_increment, not NULL, etc.. You probably have one already

 

so change your query to 

SELECT * FROM `comments` ORDER BY `id` DESC LIMIT 20

Link to comment
Share on other sites

I agree with Naez, but to explain why you have this problem...

 

If a record set has 10 records then mysql_numrows() will return 10, obviously. But, the records go from 0 to 9! I wouldn't suggest using the code you have above, but the correct thing to do would be this:

 

$num=mysql_numrows($result) - 1;

Link to comment
Share on other sites

Good suggestion on the timestamp.

 

Make a PHP file and run this code:

<?php

$sql = "ALTER TABLE `comments` ADD `post_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP";

 

mysql_query($sql);

?>

 

Do that, then change your code in that page to:

 

 

<th>Comments</th>

<?php

  $username="username";
  $password="password";
  $database="database";

  mysql_connect(localhost,$username,$password);
  @mysql_select_db($database) or die ("Could not connect: " . mysql_error());

  $query="SELECT * FROM comments ORDER BY post_time DESC";
  $result=mysql_query($query);

  while ($row = mysql_fetch_array($result))
{
	echo "<tr>";
  		echo "<td><b>" . $row['name'] . " </b> <br /><br />" . $row['comment'] . "</td>";
  		echo "</tr>";
}
?>

 

 

 

Note: the results might be a little off unless you update the timestamps of older posts within the DB (as they will default their timestamps to "0000-00-00 00:00:00", but newer posts will be in the correct order.)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.