-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathchecknewcomment.php
More file actions
47 lines (40 loc) · 1.3 KB
/
checknewcomment.php
File metadata and controls
47 lines (40 loc) · 1.3 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
<?php
ini_set('display_errors', 0);
session_start();
require_once 'dbconnect.php';
ini_set("display_errors", 1);
error_reporting(E_ALL);
header('Content-Type: application/json');
// セッションにログイン情報がない場合はログインページにリダイレクト
if (!isset($_SESSION['email'])) {
header('Location: login.php');
exit;
}
$threadName = $_POST["threadName"];
$commentCount = $_POST["commentCount"];
$RcommentCount = 0;
//スレッド内部の書き込み総数を取得
$sql = "SELECT COUNT(*) as row_count FROM thread_{$threadName}";
$result = $dbh->query($sql);
if ($result) {
$row = $result->fetch(PDO::FETCH_ASSOC);
$RcommentCount = $row["row_count"];
}
if ($RcommentCount > $commentCount) {
//新規コメントの内容をjsonで古い順に送信
$commentCount += 1;
$sql = "SELECT id, username, comment, created_at FROM thread_{$threadName} WHERE id >= ".$commentCount." AND id <= ".$RcommentCount." ORDER BY id";
$result = $dbh->query($sql);
$response = array();
if ($result) {
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$response[] = $row; // レスポンスに行データを追加
}
}
echo json_encode($response);
} else {
//0を返す
$response = array("no");
echo json_encode($response);
}
?>