/
home
/
techb158
/
workloadmatch.com
/
api
/
chat
/
/home/techb158/workloadmatch.com/api/chat
mkdir
upload
Name
Size
Mode
Actions
fetch.php
2034
0644
edit
dl
rm
send.php
1319
0644
edit
dl
rm
test.php
908
0644
edit
dl
rm
unread_count.php
745
0644
edit
dl
rm
Edit:
/home/techb158/workloadmatch.com/api/chat/fetch.php
(2034B)
<?php include_once '../../includes/db_connect.php'; include_once '../../includes/functions.php'; sec_session_start(); header('Content-Type: application/json'); $userId = isset($_SESSION['user_id']) ? (int)$_SESSION['user_id'] : 0; if (!$userId) { echo json_encode(['success' => false, 'messages' => [], 'error' => 'Not logged in']); exit; } $user_type = $_GET['user_type'] ?? 'manager'; $partner_id = (int)($_GET['partner_id'] ?? 0); $last_id = (int)($_GET['last_id'] ?? 0); $limit = min((int)($_GET['limit'] ?? 50), 200); $partner_type = $_GET['partner_type'] ?? ($user_type === 'manager' ? 'teacher' : 'manager'); if (!$partner_id) { echo json_encode(['success' => false, 'messages' => []]); exit; } $stmt = $mysqli->prepare("SELECT msg_id, sender_id, sender_type, receiver_id, receiver_type, message, is_read, created_at FROM chat_messages WHERE (sender_id = ? AND sender_type = ? AND receiver_id = ? AND receiver_type = ?) OR (sender_id = ? AND sender_type = ? AND receiver_id = ? AND receiver_type = ?) ORDER BY created_at ASC LIMIT ?"); if (!$stmt) { echo json_encode(['success' => false, 'messages' => [], 'error' => $mysqli->error]); exit; } $stmt->bind_param("isisisisi", $userId, $user_type, $partner_id, $partner_type, $partner_id, $partner_type, $userId, $user_type, $limit ); $stmt->execute(); $res = $stmt->get_result(); $messages = []; $new_last_id = $last_id; while ($row = $res->fetch_assoc()) { if ($row['msg_id'] > $new_last_id) $new_last_id = $row['msg_id']; $messages[] = $row; } $stmt->close(); // Mark incoming messages as read $mark = $mysqli->prepare("UPDATE chat_messages SET is_read = 1 WHERE receiver_id = ? AND receiver_type = ? AND sender_id = ? AND sender_type = ? AND is_read = 0"); if ($mark) { $mark->bind_param("isis", $userId, $user_type, $partner_id, $partner_type); $mark->execute(); $mark->close(); } echo json_encode(['success' => true, 'messages' => $messages, 'new_last_id' => $new_last_id]); $mysqli->close();
Save
cmd:
run