forked from aorti017/CS180-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessageList.php
More file actions
82 lines (72 loc) · 2.18 KB
/
Copy pathmessageList.php
File metadata and controls
82 lines (72 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
class MessageItem
{
var $sender;
var $receiver;
var $timestamp;
//creates a class of the most recent message by each unique user
function MessageItem($senderr, $receiverr, $timestampp)
{
$this->sender = $senderr;
$this->receiver = $receiverr;
$this->timestamp = $timestampp;
}
function printer()
{
//this prints out the values for debugging
if(isset($this))
{
echo $this->sender;
echo $this->receiver;
echo $this->timestamp;
}
else
{
echo "this is not defined";
}
}
}
//goes to the home page if no one is logged in
session_start();
if(!isset($_COOKIE['username'])) {
header('Location: index.php');
}
// else set the logged in user in the cookies
setcookie('username', $_SESSION['username'], 0);
include './database.php';
$statement = "select distinct sender, receiver, timestamp FROM Messages WHERE sender ='" . $_COOKIE['username'] ."'" . " or receiver ='" . $_COOKIE['username'] ."'" . " ORDER BY timestamp DESC" ;
//SQL statement that gets all of the messages that the user has sent/received and ordres them by timestamp
$results = executeStatement($statement);
$messageList = array();
$newMessage = new MessageItem($results[0][0], $results[0][1], $results[0][2]);
array_push($messageList, $newMessage);
//We check if the sender and receiver are already in our array of messages, and we put them in if they're not
//Since it is ordered by timestamp, the ones at the top of our array will always be the most recent.
foreach($results as $val){
//echo $val[0];
$setflag = 0;
foreach($messageList as $bal)
{
if(($bal->sender ==$val[0] && $bal->receiver == $val[1]) || ($bal->sender == $val[1] && $bal->receiver == $val[0]))
{
$setflag = 1;
}
}
if($setflag == 0)
{
array_push($messageList, new MessageItem($val[0], $val[1], $val[2]));
}
}
$messageSenders = array();
$messageReceiver = array();
$messageTimestamp = array();
foreach($messageList as $resluts)
{
array_push($messageSenders, $resluts->sender);
array_push($messageReceiver, $resluts->receiver);
array_push($messageTimestamp, $resluts->timestamp);
}
$ret = array('sender'=>$messageSenders, 'receiver'=>$messageReceiver,'timestamp'=>$messageTimestamp);
$json = json_encode($ret);
echo $json;
?>