Log in

View Full Version : querying 2 tables mixed results



TwitterRooms
02-22-2012, 07:19 PM
hi guys me again doing a simple chat system very basic at the moment but stuck with if user A posts then user B then A then B all works ok but when user A posts twice it still displays A,B,A,B not AA,B,A
here is the script


$result1 = mysql_query("SELECT * FROM sms WHERE touser = '$user' AND fromuser = '$fromuser' ORDER BY time");
$result2 = mysql_query("SELECT * FROM sms WHERE touser = '$fromuser' AND fromuser = '$user' ORDER BY time");
while($row1 = mysql_fetch_array($result1))
{
echo $row1['message'];

while($row2 = mysql_fetch_array($result2))
{
echo $row2['message'];
}
}

crobinson42
03-04-2012, 02:22 AM
hey, I'm trying to rack up some 'thanks' on my profile so hook me up for this one!

you need to do some work with arrays here:


$result1 = mysql_query("SELECT * FROM sms WHERE touser = '$user' AND fromuser = '$fromuser' ORDER BY time");
$result2 = mysql_query("SELECT * FROM sms WHERE touser = '$fromuser' AND fromuser = '$user' ORDER BY time");

$messages1 = mysql_fetch_array($result1);
$messages2 = mysql_fetch_array($result2);

$messages = array_merge($messages1, $messages2);

$sorted_messages = array_orderby($messages, 'time', SORT_DESC, 'touser', SORT_ASC);


echo '<pre>';
print_r($sorted_messages);
echo '</pre>';


Let me know if you don't understand this, or need more help