Page 1 of 1

Private Message System - Showing all messages for each user

Posted: Sun May 06, 2012 3:51 am
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.

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

Posted: Sun May 06, 2012 5:21 am
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'];
}
:) ?

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

Posted: Sun May 06, 2012 6:13 am
by bluebird
That fixed it! Thanks Jacek.