-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_data.php
More file actions
59 lines (49 loc) · 1.49 KB
/
get_data.php
File metadata and controls
59 lines (49 loc) · 1.49 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
48
49
50
51
52
53
54
55
56
57
58
59
<?php
session_start();
header('Content-Type: application/json');
$period = $_GET['period'] ?? 'daily';
include 'db.php';
if ($conn->connect_error) {
echo json_encode(['error' => "接続エラー: " . $conn->connect_error]);
exit;
}
$userId = $_SESSION["user_id"];
if ($period === 'daily') {
$query = "SELECT DATE(a.accessed_at) AS date, COUNT(*) AS count
FROM url_accesses a
JOIN short_urls u ON a.short_url_id = u.id
WHERE u.user_id = ?
GROUP BY DATE(a.accessed_at)
ORDER BY DATE(a.accessed_at)";
} else if ($period === 'monthly') {
$query = "SELECT DATE_FORMAT(a.accessed_at, '%Y-%m') AS month, COUNT(*) AS count
FROM url_accesses a
JOIN short_urls u ON a.short_url_id = u.id
WHERE u.user_id = ?
GROUP BY DATE_FORMAT(a.accessed_at, '%Y-%m')
ORDER BY DATE_FORMAT(a.accessed_at, '%Y-%m')";
} else {
echo json_encode(['error' => "無効な期間: $period"]);
exit;
}
$stmt = $conn->prepare($query);
$stmt->bind_param('i', $userId);
$stmt->execute();
$result = $stmt->get_result();
if ($result) {
$labels = [];
$accessCounts = [];
while ($row = $result->fetch_assoc()) {
$labels[] = $row[$period === 'daily' ? 'date' : 'month'];
$accessCounts[] = $row['count'];
}
$result->free();
echo json_encode([
'labels' => $labels,
'accessCounts' => $accessCounts,
]);
} else {
echo json_encode(['error' => "クエリエラー: " . $conn->error]);
}
$conn->close();
?>