-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetNews.php
39 lines (32 loc) · 1.48 KB
/
getNews.php
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
<?php
require_once("../header.php");
header("Access-Control-Allow-Headers: Content-Type");
header("Content-Type: application/json");
// ini_set("display_errors", "On"); //上線測試用
try {
require_once("../connectGridIsland.php");
$sql = "SELECT * FROM news";
$news = $pdo->query($sql);
$newsRows = $news->fetchAll(PDO::FETCH_ASSOC);
// 全部消息的數量
$sql = "SELECT COUNT(*) as newsCount FROM news";
$allNews = $pdo->query($sql);
$allNewsRows = $allNews->fetch(PDO::FETCH_ASSOC)["newsCount"];
// 優惠消息的數量
$sql = "SELECT COUNT(*) as promotion FROM news WHERE news_category = '優惠'";
$promotion = $pdo->query($sql);
$promotionRows = $promotion->fetch(PDO::FETCH_ASSOC)["promotion"];
// 活動消息的數量
$sql = "SELECT COUNT(*) as activity FROM news WHERE news_category = '活動'";
$activity = $pdo->query($sql);
$activityRows = $activity->fetch(PDO::FETCH_ASSOC)["activity"];
// 桌遊消息的數量
$sql = "SELECT COUNT(*) as boardGame FROM news WHERE news_category = '桌遊'";
$boardGame = $pdo->query($sql);
$boardGameRows = $boardGame->fetch(PDO::FETCH_ASSOC)["boardGame"];
$result = ["error" => false, "msg" => "", "news" => $newsRows , "allNewsCount"=>$allNewsRows,"promotionCount"=>$promotionRows,"activityCount"=>$activityRows,"boardGameCount"=>$boardGameRows];
} catch (PDOException $e) {
$result = ["error" => true, "msg" => $e->getMessage()];
}
echo json_encode($result);
?>