I'm currently trying to implement a messaging functionality in my system. My goal right now is having a jQuery popup dialog which displays the name of users that have messaged the other person and then clicking on that person's name will show the new messages. I've done the popup dialog bit so my next step is getting and sorting the messages from database.
What I've done now is retrieve the new messages from database. I am unsure of how to proceed as my array of unread messages contains the element of the sender's id more than once. I do not want to loop through my unread messages and then get the same sender id again and again and as such retrieve their details from database again and again.
Here's an example of my array:
array(8) {
[6]=>
array(7) {
["id"]=>
string(2) "52"
["useridfrom"]=>
string(3) "330"
["useridto"]=>
string(3) "139"
["message"]=>
string(10) "test"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336561645"
["messagetype"]=>
string(6) "direct"
}
[7]=>
array(7) {
["id"]=>
string(2) "53"
["useridfrom"]=>
string(3) "330"
["useridto"]=>
string(3) "139"
["message"]=>
string(8) "bye"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336561648"
["messagetype"]=>
string(6) "direct"
}
[1]=>
array(7) {
["id"]=>
string(2) "30"
["useridfrom"]=>
string(3) "329"
["useridto"]=>
string(3) "139"
["message"]=>
string(243) "Hi Bob"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1330942044"
["messagetype"]=>
string(6) "direct"
}
[3]=>
array(7) {
["id"]=>
string(2) "42"
["useridfrom"]=>
string(3) "243"
["useridto"]=>
string(3) "139"
["message"]=>
string(4) "test"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1335517653"
["messagetype"]=>
string(6) "direct"
}
[4]=>
array(7) {
["id"]=>
string(2) "46"
["useridfrom"]=>
string(3) "241"
["useridto"]=>
string(3) "139"
["message"]=>
string(8) "sdsdfsdf"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336139572"
["messagetype"]=>
string(6) "direct"
}
[5]=>
array(7) {
["id"]=>
string(2) "47"
["useridfrom"]=>
string(3) "241"
["useridto"]=>
string(3) "139"
["message"]=>
string(13) "8528528285285"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336143958"
["messagetype"]=>
string(6) "direct"
}
array(8) {
[6]=>
array(7) {
["id"]=>
string(2) "52"
["useridfrom"]=>
string(3) "330"
["useridto"]=>
string(3) "139"
["message"]=>
string(10) "test"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336561645"
["messagetype"]=>
string(6) "direct"
}
[7]=>
array(7) {
["id"]=>
string(2) "53"
["useridfrom"]=>
string(3) "330"
["useridto"]=>
string(3) "139"
["message"]=>
string(8) "bye"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336561648"
["messagetype"]=>
string(6) "direct"
}
[1]=>
array(7) {
["id"]=>
string(2) "30"
["useridfrom"]=>
string(3) "329"
["useridto"]=>
string(3) "139"
["message"]=>
string(243) "Hi Bob"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1330942044"
["messagetype"]=>
string(6) "direct"
}
[3]=>
array(7) {
["id"]=>
string(2) "42"
["useridfrom"]=>
string(3) "243"
["useridto"]=>
string(3) "139"
["message"]=>
string(4) "test"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1335517653"
["messagetype"]=>
string(6) "direct"
}
[4]=>
array(7) {
["id"]=>
string(2) "46"
["useridfrom"]=>
string(3) "241"
["useridto"]=>
string(3) "139"
["message"]=>
string(8) "sdsdfsdf"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336139572"
["messagetype"]=>
string(6) "direct"
}
[5]=>
array(7) {
["id"]=>
string(2) "47"
["useridfrom"]=>
string(3) "241"
["useridto"]=>
string(3) "139"
["message"]=>
string(13) "8528528285285"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336143958"
["messagetype"]=>
string(6) "direct"
}
[2]=>
array(7) {
["id"]=>
string(2) "10"
["useridfrom"]=>
string(3) "138"
["useridto"]=>
string(3) "139"
["message"]=>
string(54) "Hi Emma thank you for submitting your homework - Jenny"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1309122193"
["messagetype"]=>
string(6) "direct"
}
}
This is what I've done in PHP so far:
$m = new Messaging();
$json = $m->getUnreadMessages($uid);
/** sort messages by useridfrom **/
uasort($json, array($m, 'messageCompare'));
foreach($json as $j)
{
//do sorting of messages here ??
}
echo json_encode($json);
exit;
I guess ideally, I would want to create a new array out of this where the key is useridfrom (sender) and then their messages and then associated with each message the timecreated. What is the best way to do this?
EDIT
As you can see from the array example there are duplicate useridfrom elements where useridfrom = 241 twice. This means that the user 241 has sent two messages, if there were 3 useridfrom 241 elements then that means he/she sent 3 messages. My goal is two group the useridfrom together as keys in an array so there are no duplicates then associated with that key we have all their messages (message) and the time the message was created (timecreated).
[4]=>
array(7) {
["id"]=>
string(2) "46"
["useridfrom"]=>
string(3) "241"
["useridto"]=>
string(3) "139"
["message"]=>
string(8) "sdsdfsdf"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336139572"
["messagetype"]=>
string(6) "direct"
}
[5]=>
array(7) {
["id"]=>
string(2) "47"
["useridfrom"]=>
string(3) "241"
["useridto"]=>
string(3) "139"
["message"]=>
string(13) "8528528285285"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336143958"
["messagetype"]=>
string(6) "direct"
}
[1]=>
array(7) {
["id"]=>
string(2) "30"
["useridfrom"]=>
string(3) "329"
["useridto"]=>
string(3) "139"
["message"]=>
string(243) "Hi Bob"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1330942044"
["messagetype"]=>
string(6) "direct"
}
So from this example, it should lead to an array of something like what mariomario has suggested:
array{
['241'] => array(
[0] => array(time=>'time1', message => "hello i am 241"),
[1] => array(time=>'time2', message => "nice to meet you"),
),
['330'] => array(
[0] => array(time=>'time1', message => "hello i am 330"),
[1] => array(time=>'time2', message => "goodbye"),
)
}
EDITED:
After a little pondering I'd do this:
$data = array();
foreach ($json as $j) {
if (!array_key_exists($j['useridfrom'], $data)) {
$data[$j['useridfrom']] = array();
}
if (!array_key_exists($j['useridto'], $data[$j['useridfrom']])) {
$data[$j['useridfrom']][$j['useridto']] = array();
}
$data[$j['useridfrom']][$j['useridto']][] = array(
'message' => $j['message'],
'timestamp' => $j['timecreated']
);
}
Keep your data ordered like so ORDER BY timecreated DESC
and that concats everything like you want ordered after the time.
This should net an array like so:
'sender1' => (
'recipient1' = (
(
'message' => 'MESSAGE HERE',
'timecreated' => 'TIME HERE'
),
(
'message' => 'ANOTHER MESSAGE HERE',
'timecreated' => 'ANOTHER TIME HERE'
)
),
'recipient2' = (
(
'message' => 'MESSAGE HERE',
'timecreated' => 'TIME HERE'
)
)
),
'sender2' => (
)