-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathquestionList.php
More file actions
60 lines (47 loc) · 1.33 KB
/
questionList.php
File metadata and controls
60 lines (47 loc) · 1.33 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
60
<?php
require_once __DIR__ . '/db_connect.php';
header('Content-Type: application/json; charset=utf-8');
$isSolved = isset($_GET['isSolved']) ? $_GET['isSolved'] : 'all';
$where = "WHERE 1 = 1";
$keywordId = isset($_GET['keyword_id']) ? intval($_GET['keyword_id']) : 0;
if ($keywordId > 0) {
$where .= " AND keyword_id = $keywordId";
}
if ($isSolved === 'true') {
$where .= " AND is_solved = 1";
} else if ($isSolved === 'false') {
$where .= " AND is_solved = 0";
}
$sort = isset($_GET['sort']) ? $_GET['sort'] : 'like';
switch ($sort) {
//최신순
case 'date':
$orderBy = "ORDER BY created_at DESC";
break;
//답변 기다리고 있는 (채택이 없는 게시글)
case 'not_solved':
$where .= " AND is_solved = 0";
$orderBy = "ORDER BY created_at DESC";
break;
//인기순
case 'like':
default:
$orderBy = "ORDER BY like_count 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,
'data' => $rows
], JSON_UNESCAPED_UNICODE);
mysqli_close($conn);
?>