-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathquestionSearch.php
More file actions
57 lines (43 loc) · 1.22 KB
/
questionSearch.php
File metadata and controls
57 lines (43 loc) · 1.22 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
<?php
require_once __DIR__ . '/db_connect.php';
header('Content-Type: application/json; charset=utf-8');
$keyword = isset($_GET['keyword']) ? trim($_GET['keyword']) : '';
$isSolved = isset($_GET['isSolved']) ? $_GET['isSolved'] : 'all';
// 기본 WHERE
$where = "WHERE 1 = 1";
if ($keyword !== '') {
$safeKeyword = mysqli_real_escape_string($conn, $keyword);
$where .= " AND title LIKE '%$safeKeyword%'";
}
if ($isSolved === 'true') {
$where .= " AND is_solved = 1";
} else if ($isSolved === 'false') {
$where .= " AND is_solved = 0";
}
$sort = isset($_GET['sort']) ? $_GET['sort'] : 'date';
switch ($sort) {
case 'like':
$orderBy = "ORDER BY like_count DESC";
break;
case 'date':
default:
$orderBy = "ORDER BY created_at DESC";
break;
}
$sql = "SELECT * FROM questions $where $orderBy";
$result = mysqli_query($conn, $sql);
$rows = [];
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = array_map(function($v) {
return is_numeric($v) ? (int)$v : $v;
}, $row);
}
echo json_encode([
'success' => true,
'sort' => $sort,
'isSolved' => $isSolved,
'keyword' => $keyword,
'data' => $rows
], JSON_UNESCAPED_UNICODE);
mysqli_close($conn);
?>