Reputation: 116
I am new to php so please I apologize if my question does not come out clear.
I have a message table setup in mysql. I am able to echo out messages sent_by and sent_to a user and the time they were sent and how to order them by time. My question is this: how can I output the messages in a sent_by and sent_to order by time, but like this
user1: sent 2011-02-12 13:34:26
hi, how are you
user2: sent 2011-02-12 13:37:26
who is this?
user1: sent 2011-02-12 13:38:26
its me
user1: sent 2011-02-12 13:38:50
from the bar
user1: sent 2011-02-12 13:40:26
the one in the city
user2: sent 2011-02-12 13:45:26
Oh hi
I want to display everything that user2 sent in red and everything that user1 sent in black. How can I do this?
This is what I did, but the way how I want to do it now is not this way.
$tomsg = mysql_query("SELECT * FROM msg WHERE sent_to = 'user1' ");
$inboxmsg_count = mysql_num_rows($tomsg );
if ($inboxmsg_count>0)
{
echo "messages between You and name<br/>";
echo 'total message ('.$inboxmsg_count.')<hr/><br/>';
while ($msg = mysql_fetch_array($tomsg )){
$cmsg = $msg ['cmsg'];
$time_sent = $msg ['time_sent'];
$subj = $msg ['subj'];
echo '<div style="background-color:gray; width:542px; height:auto; padding-left:10px; padding-right:10px"><span style="float:right;">' .$time_sent.'</span><br/><b>' .$subj.'</b><br/> '.$cmsg.'<hr/></div>';
}
$frommsg = mysql_query("SELECT * FROM msg WHERE sent_by = 'user1' ");
if($inboxmsg_count>0)
{
while ($msg = mysql_fetch_array($frommsg )){
$cmsg = $msg ['cmsg'];
$time_sent = $msg ['time_sent'];
$subj = $msg ['subj'];
echo '<div style="background-color:blue; width:542px; height:auto; padding-left:10px; padding-right:10px"><span style="float:right;">' .$time_sent.'</span><br/><b>' .$subj.'</b><br/> '.$cmsg.'<hr/></div>';
}
}
}
else {echo "you have no messages y}
Upvotes: 0
Views: 303
Reputation: 360742
There's no need to do two queries, simply do something like
SELECT username, the, other, fields, you need
FROM messagestable
ORDER BY timesent
and within your PHP code, you do
while($row = msyql_fetch_assoc(...)) {
... pick a color based on $row['username'] ...
... display the message using that color ...
}
Upvotes: 1