-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_connection.php
165 lines (152 loc) · 3.71 KB
/
database_connection.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
$connect = new PDO("mysql:host=localhost;dbname=pchat;charset=utf8mb4", "root", "");
date_default_timezone_set('Asia/Kolkata');
function fetch_user_last_activity($user_id, $connect)
{
$query = "
SELECT * FROM login_details
WHERE user_id = '$user_id'
ORDER BY last_activity DESC
LIMIT 1
";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row)
{
return $row['last_activity'];
}
}
function fetch_user_chat_history($from_user_id, $to_user_id, $connect)
{
$query = "
SELECT * FROM chat_message
WHERE (from_user_id = '".$from_user_id."'
AND to_user_id = '".$to_user_id."')
OR (from_user_id = '".$to_user_id."'
AND to_user_id = '".$from_user_id."')
ORDER BY timestamp DESC
";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$output = '<ul class="list-unstyled">';
foreach($result as $row)
{
$user_name = '';
if($row["from_user_id"] == $from_user_id)
{
$user_name = '<b class="text-success">You</b>';
}
else
{
$user_name = '<b class="text-danger">'.get_user_name($row['from_user_id'], $connect).'</b>';
}
$output .= '
<li style="border-bottom:1px dotted #ccc">
<p>'.$user_name.' - '.$row["chat_message"].'
<div align="right">
- <small><em>'.$row['timestamp'].'</em></small>
</div>
</p>
</li>
';
}
$output .= '</ul>';
$query = "
UPDATE chat_message
SET status = '0'
WHERE from_user_id = '".$to_user_id."'
AND to_user_id = '".$from_user_id."'
AND status = '1'
";
$statement = $connect->prepare($query);
$statement->execute();
return $output;
}
function get_user_name($user_id, $connect)
{
$query = "SELECT username FROM login WHERE user_id = '$user_id'";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row)
{
return $row['username'];
}
}
function count_unseen_message($from_user_id, $to_user_id, $connect)
{
$query = "
SELECT * FROM chat_message
WHERE from_user_id = '$from_user_id'
AND to_user_id = '$to_user_id'
AND status = '1'
";
$statement = $connect->prepare($query);
$statement->execute();
$count = $statement->rowCount();
$output = '';
if($count > 0)
{
$output = '<span class="label label-success">'.$count.'</span>';
}
return $output;
}
function fetch_is_type_status($user_id, $connect)
{
$query = "
SELECT is_type FROM login_details
WHERE user_id = '".$user_id."'
ORDER BY last_activity DESC
LIMIT 1
";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$output = '';
foreach($result as $row)
{
if($row["is_type"] == 'yes')
{
$output = ' - <small><em><span class="text-muted">Typing...</span></em></small>';
}
}
return $output;
}
function fetch_group_chat_history($connect)
{
$query = "
SELECT * FROM chat_message
WHERE to_user_id = '0'
ORDER BY timestamp DESC
";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$output = '<ul class="list-unstyled">';
foreach($result as $row)
{
$user_name = '';
if($row["from_user_id"] == $_SESSION["user_id"])
{
$user_name = '<b class="text-success">You</b>';
}
else
{
$user_name = '<b class="text-danger">'.get_user_name($row['from_user_id'], $connect).'</b>';
}
$output .= '
<li style="border-bottom:1px dotted #ccc">
<p>'.$user_name.' - '.$row['chat_message'].'
<div align="right">
- <small><em>'.$row['timestamp'].'</em></small>
</div>
</p>
</li>
';
}
$output .= '</ul>';
return $output;
}
?>