Private Message System - Showing all messages for each user

Ask about a PHP problem here.
Post Reply
bluebird
Posts: 8
Joined: Fri Mar 09, 2012 3:00 am

Private Message System - Showing all messages for each user

Post by bluebird »

Hi,

My rebuild of the Private Message System works fine. Now I am trying to show, in a table, all messages by their user_name.
<?php

// Obtain the amount of users in users
$query_users = mysql_query("SELECT * FROM users")
$num_users = mysql_num_rows("$query_users");
// Obtain the amount of messages in conversations_messages
$query_messages = mysql_query("SELECT * FROM conversations_messages");
$num_messages = mysql_num_rows($query_messages);

// Create a loop where the limit is the amount of messages stored in the `conversations_messages`table
$i=0;
while ($i<=$num_messages){

// Now, it needs to display all the messages by the user_name. 
//This is the part that I do not understand

}

?>
This should return :

Charles : Message1 (the date the message was sent)
Charles : Message2 (the date the message was sent)
Charles : Message3 (the date the message was sent)
Adam : Message1 (the date the message was sent)
Adam : Message2 (the date the message was sent)
Adam : Message3 (the date the message was sent)
Adam : Message1 (the date the message was sent)
George : Message1 (the date the message was sent)
George : Message2 (the date the message was sent)

and so on.

Thanks for any help! I couldn't figure this out on my own.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Private Message System - Showing all messages for each u

Post by jacek »

Well you don't need this bit
// Obtain the amount of users in users
$query_users = mysql_query("SELECT * FROM users")
$num_users = mysql_num_rows("$query_users");
since it's never actually used.

You don't actually ever need the number of rows since you can just loop until the end of the data is reached using the normal method
$result = mysql_query('SELECT `list`, `of`, `columns`, `that`, `you`, `need` FROM `table`');

while (($row = mysql_fetch_assoc($result)) !== false){
    // Here you can use $row['column_name'];
}
:) ?
Image
bluebird
Posts: 8
Joined: Fri Mar 09, 2012 3:00 am

Re: Private Message System - Showing all messages for each u

Post by bluebird »

That fixed it! Thanks Jacek.
Post Reply