New post
As a result, I completely avoided this request. I simply could not get the results that I was looking for. To get the desired results, I came up with this ...
Also, if you guys came up with this query, I would really try to delete this work.
Thanks for all the help so far!
function get_converstations($data)
{
$sender_sql = "SELECT DISTINCT `users`.`aid` AS `aid`, `users`.`nickname` AS `nickname`
FROM `users`,`messages`
WHERE `messages`.`sender` = '".$data['aid']."'
AND `messages`.`recipient` = `users`.`aid`";
$query = mysql_query($sender_sql);
if(mysql_num_rows($query) > 0)
{
$sender_data = array();
while ($row = mysql_fetch_array($query)){
$sender_data[$row['aid']] = $row['nickname'];
}
}
$recipient_sql = "SELECT DISTINCT `users`.`aid`, `users`.`nickname`
FROM `users`,`messages`
WHERE `messages`.`recipient` = '".$data['aid']."'
AND `messages`.`sender` = `users`.`aid`";
$query = mysql_query($recipient_sql);
if(mysql_num_rows($query) > 0)
{
while ($row = mysql_fetch_array($query)){
$recipient_data[$row['aid']] = $row['nickname'];
}
}
$no_keys_persons = array_merge($sender_data, $recipient_data);
foreach($no_keys_persons as $aid => $nickname)
{
$persons[] = array(
"aid" => $aid,
"nickname" => $nickname
);
}
foreach($persons as $person)
{
$sql = "SELECT * FROM `messages` WHERE `sender` = '".$data['aid']."' AND `recipient` = '".$person['aid']."' OR `sender` = '".$person['aid']."' AND `recipient` = '".$data['aid']."' ORDER BY `id` DESC LIMIT 1";
$query = mysql_query($sql);
if(mysql_num_rows($query) > 0)
{
while($row = mysql_fetch_array($query))
{
$conversations[] = array(
"person_aid" => $person['aid'],
"person_nickname" => $person['nickname'],
"sender" => $row['sender'],
"recipient" => $row['recipient'],
"body" => $row['body'],
"timestamp" => $row['timestamp'],
"ip" => $row['ip']
);
}
}
}
return $conversations;
}
Then, when I call this function on my controller.
$data = array(
"aid" => $_GET['aid'],
"nickname" => $_GET['nickname'],
"ip" => $_SERVER['REMOTE_HOST'],
);
include 'db.php';
include 'messages_model.php';
$messages = new messages_model();
$coversations = $messages->get_converstations($data);
foreach ($coversations as $conversation)
{
echo '&conversation=';
echo '&sender='.$conversation['sender'];
if($conversation['sender'] === $data['aid']) { echo '&sender_nickname='.$data['nickname']; } else { echo '&sender_nickname='.$conversation['person_nickname']; }
echo '&recipient='.$conversation['recipient'];
if($conversation['recipient'] === $data['aid']) { echo '&recipient_nickname='.$data['nickname']; } else { echo '&recipient_nickname='.$conversation['person_nickname']; }
echo '&body='.$conversation['body'];
echo '×tamp='.$conversation['timestamp'];
}
Original post
I'm in trouble guys. See if you can help me put this request together.
I have a table called messages.
CREATE TABLE `db.app`.`messages` (
`id` INT( 32 ) NOT NULL AUTO_INCREMENT ,
`sender` VARCHAR( 64 ) NOT NULL ,
`recipient` VARCHAR( 64 ) NOT NULL ,
`body` TEXT NOT NULL ,
`timestamp` INT( 32 ) NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM ;
And a table called users.
CREATE TABLE `db.app`.`users` (
`id` INT( 32 ) NOT NULL AUTO_INCREMENT ,
`nickname` VARCHAR( 64 ) NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM ;
, messages. sender, messages. recipient, UNIX. .
. ( iPhone).
, ...
messages table;
id | sender | recipient | body | timestamp
1 | 1234 | 5678 | testing message | 1290233086
2 | 5678 | 1234 | testing reply | 1290233089
users table;
id | nickname
1234 | john
5678 | peter
...
results;
other_person_id | other_person_nickname | last_message | last_message_timestamp
1234 | john | testing reply | 1290233089
....