-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.php
138 lines (114 loc) · 4.43 KB
/
chat.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
<?php
session_start();
?>
<?php
include ('connect.php');
include ('authenticationUser.php');
global $con;
global $user;
global $userid;
//get all chets
$chatqueryresult = $con->query("SELECT * FROM chatmessage WHERE receiverUserID = '$userid' or senderUserID = '$userid' order by id desc");
$userchatcreated = array();
if ($chatqueryresult->num_rows > 0) {
while ($r = $chatqueryresult->fetch_assoc()) {
if($r['senderUserID'] != $userid){
$otheruserofchatid = null;
if(!array_key_exists($r['senderUserID'], $userchatcreated)){
$otheruserofchatid=$r['senderUserID'];
}
}else{
if(!array_key_exists($r['receiverUserID'], $userchatcreated)) {
$otheruserofchatid=$r['receiverUserID'];
}
}
if($otheruserofchatid != null){
$otheruserqueryresult = $con->query("SELECT * FROM user WHERE id = '$otheruserofchatid'");
if($otheruserqueryresult->num_rows == 1){
$otheruserofchat = $otheruserqueryresult->fetch_assoc();
$userchatcreated[$otheruserofchatid]= array('name'=>$otheruserofchat['name'], 'date'=>$r['date']);
}
}
}
}
//get all chatmessages
$chatmsg = array();
$chatwithusername = null;
if(isset($_GET['id'])){
$chatwithuserid = $_GET['id'];
//get name of user we chat with
$userofchatquery = $con->query("SELECT * FROM user WHERE id = '$chatwithuserid'");
if($userofchatquery->num_rows == 1){
$userofchat = $userofchatquery->fetch_assoc();
$chatwithusername = $userofchat['name'];
}
//get all messages with this user
$chatqueryresult = $con->query("SELECT * FROM chatmessage WHERE (receiverUserID = '$userid' and senderUserID = '$chatwithuserid') or (receiverUserID = '$chatwithuserid' and senderUserID = '$userid')");
if ($chatqueryresult->num_rows > 0) {
while ($r = $chatqueryresult->fetch_assoc()) {
if($r['senderUserID'] == $userid){
$class = 'ownwrittenmsg';
}else{
$class = 'receivedmsg';
}
array_push($chatmsg, array('text'=>$r['text'], 'date'=>$r['date'], 'class'=>$class));
}
}
//if button is clicked
if(isset($_POST['sendmsg'])){
$message = input($_POST['msgtext']);
$date = date('Y-m-d H:i:s');
$result = $con->query("INSERT INTO chatmessage(`text`, `date`, `receiverUserID`, `senderUserID`) VALUES ('$message','$date','$chatwithuserid','$userid')");
header("Refresh:0");
}
}
?>
<!--HTML CODE-->
<!DOCTYPE html>
<html lang="en-us">
<?php include('head.php') ?>
<body>
<div>
<?php include('menu.php') ?>
<div style="display: flex; padding-top: 70px; padding-left: 20px;">
<div style="width: 200px; background-color: #cecece; height: calc(100vh - 230px); overflow-y: scroll; overflow-x: hidden;">
<?php
foreach ($userchatcreated as $key=>$userch){
?>
<a href="chat.php?id=<?php echo $key ?>">
<div style="padding: 10px; border-bottom: 1px solid black">
<h3><?php echo $userch['name'] ?></h3>
<p><?php echo $userch['date'] ?></p>
</div>
</a>
<?php
}
?>
</div>
<div style="width: calc(100% - 300px); background-color: #cecece; height: calc(100vh - 230px); overflow-y: scroll; overflow-x: hidden;">
<?php
if($chatwithusername != null){
?>
<div style="padding: 20px; background-color: #333333; color: #ffffff;"><?php echo $chatwithusername ?></div>
<?php
foreach ($chatmsg as $msg){
?>
<div class="<?php echo $msg['class'] ?>" style="padding: 10px; margin-top: 10px; max-width: 250px; ">
<h3><?php echo $msg['text'] ?></h3>
<p><?php echo $msg['date'] ?></p>
</div>
<?php
}
?>
<form action="" method="post" style=" display: flex;">
<input style="width: 100%" type="text" name="msgtext">
<input type="submit" value="send" class="button" name="sendmsg">
</form>
<?php
}
?>
</div>
</div>
</div>
</body>
</html>