Order Private Messages by top/latest bottom/earliest?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Order Private Messages by top/latest bottom/earliest?

Post by JAB Creations »

Please forgive me (and feel free to move the thread to the Database section if applicable) though I'm trying to figure out how to order messages in the private message system I've setup by date. By default I want to display the newest messages at the top and going down the list to earlier messages. I figure this may be a PHP issue to scan the $row['date_0'] (which is the sent date where as date_1 is the read date) though I'm not familiar enough to presume that I could not somehow do this with MySQL.

Since it's a message system the $row is defined inside of the while loop. I'm not looking for a direct answer though it would be nice. I'm actually having difficulty with the MySQL documentation. I eventually got used to W3C's and after a redesign can now figure out Apache. So I'm much more interested in learning how to independently research the topic so I can at least make more of an educated guess versus an empty handed sort of question. Here is the minimal though most applicable code I think is relevant to achieving this goal...

Code: Select all

if (!isset($_GET['folder'])) {$result1 = mysql_query("SELECT id, id_from, date_0, date_1, message, subject FROM `public_private_messages` WHERE id_for = '".$_SESSION['member']."' OR id_for_folder = 'New'");} // AND id_for_folder = 'Read'
 
if (!$result1) {$_SESSION['error'] = mysql_error(); header("location:messages.php?error");}
while($row = mysql_fetch_array($result1))
{
 echo '<tr id="message_'.$row['id'].'"><td><input class="checkbox" type="checkbox" value="" /></td>'."\n";
 echo '<td>'.$row['id_from'].'</td>'."\n";
 echo '<td>'.$row['date_0'].'</td>'."\n";
 echo '<td><a href="messages.php?message='.$row['id'].'">'.$row['subject'].'</a></td></tr>'."\n";
}
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Order Private Messages by top/latest bottom/earliest?

Post by jayshields »

Add an ORDER BY `date_0` DESC clause to the end of your query.

I find Firefox's search engine plug-ins useful for searching manuals, here is a link to some MySQL ones http://mycroft.mozdev.org/search-engine ... name=mysql
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Order Private Messages by top/latest bottom/earliest?

Post by JAB Creations »

Yeah!

YEAH!

YEAH!

Awesome thanks!

I don't even know why I'm asking this! After you mentioned that I switch to the phpMyAdmin tab and there it is...the column header text is anchored...and for the win phpMyAdmin gives you the execution code!

I've added the MYSQL 5.1 manual to Firefox's quick search providers. Thanks a ton!
Post Reply