-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueryFriends.php
147 lines (118 loc) · 8.87 KB
/
queryFriends.php
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<div id = 'formDiv' style = 'height:390px;padding:10px 50px 30px 40px;border-radius:30px;' class= 'bg-white'>
<div id = 'formScroll' style = 'height:370px'>
<p style = 'margin-left:20%'><strong>Friends</strong></p>
<?php
$callingObjectClass = new Database_object_oriented_index();
$connect = $callingObjectClass->connect_to_database_function();
if ($connect){
$user = $_SESSION['name'];
$sql = "SELECT DISTINCT `receiver` FROM `messages` WHERE `sender` = '$user' AND `receiver` != '$user' ";
$result = $connect->query($sql);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$chatted = $row['receiver'];
$sql = "SELECT `id`,`email` FROM `users` WHERE `username` = '$chatted' ";
$innerResult = $connect->query($sql);
if($innerResult-> num_rows > 0)
{
while ($innerRow = $innerResult->fetch_assoc())
{
//get last chatted id number to get new messages
$sql = "SELECT `id`, `message` from `messages` WHERE `sender` = '$chatted' AND `receiver` = '$user' ORDER BY `id` DESC LIMIT 1";
$idResult = $connect->query($sql);
if($idResult->num_rows > 0){
while ($idRow = $idResult->fetch_assoc()) {
# code...
$sendingId = $idRow['id'];
$getMessage = $idRow['message'];
}
}
$userId = $innerRow['id'];
$email = $innerRow['email'];
$sql = "SELECT `message`, `id` FROM `messages` WHERE `receiver` = '$chatted' AND `sender` = '$user' ORDER BY `id` DESC LIMIT 1";
$innestResult = $connect->query($sql);
if ($innestResult ->num_rows > 0)
{
while ($innestRow = $innestResult->fetch_assoc())
{
$message = $innestRow['message'];
$myLastTextId = $innestRow['id'];
$sql = "SELECT `image` from `profile` WHERE `username` = '$chatted' ";
$innerResult = $connect->query($sql);
if($innerResult->num_rows > 0)
{
while ($innerRow = $innerResult->fetch_assoc())
{
$get_current_user_image = $innerRow['image'];
if ( isset($sendingId ) )
{
# code...
if ($sendingId > $myLastTextId )
{
$newMessage = $sendingId - $myLastTextId;
$lastMessage = $getMessage;
}elseif($sendingId < $myLastTextId)
{
$lastMessage = $message;
$newMessage = null;
$sendingId = null;
}
}
echo "
<div id = 'friendList' >
<div id = 'imgIcon'>
<a href = 'home.php?userId=$userId ' style = 'text-decoration:none;color:black;'>
<img src='$get_current_user_image' alt='icon' style = 'width:30px;height:35px;border-radius:50%;float:left;margin:1px 10px 4px 4px;'> </a>
</div>
<div id = 'sep' >
<strong ><a href = 'home.php?userId=$userId ' style = 'text-decoration:none;color:black;'>$chatted</a></strong>
<p style = 'font-size:10px;'>
</div>
<div id = 'info' style = 'float:right;'> </div>
</div>
<center> <hr></center> ";
}
}
else
{if ( isset($sendingId ) )
{
# code...
if ($sendingId > $myLastTextId )
{
$newMessage = $sendingId - $myLastTextId;
$lastMessage = $getMessage;
}elseif($sendingId < $myLastTextId)
{
$lastMessage = $message;
$newMessage = null;
$sendingId = null;
}
}
echo "
<div id = 'friendList' style = 'margin-top:10px;'>
<div id = 'imgIcon'>
<a href = 'home.php?userId=$userId ' style = 'text-decoration:none;color:black;'>
<img src='images/user.png' alt='icon' style = 'width:30px;height:35px;border-radius:50%;float:left;margin:1px 10px 4px 4px;'> </a>
</div>
<div id = 'sep' >
<strong ><a href = 'home.php?userId=$userId ' style = 'text-decoration:none;color:black;'>$chatted</a></strong>
</div>
</div>
<center> <hr></center> ";
}
}
}
}
}
}
}
else
{
echo 'Your chats would be displayed here';
}
}
?>
</div>
</div>