Skip to content

Commit ffb80f3

Browse files
committed
準備改用 AJAX 處理
1 parent 5b15026 commit ffb80f3

File tree

6 files changed

+370
-93
lines changed

6 files changed

+370
-93
lines changed

admin/categories.php

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
<link rel="stylesheet" type="text/css" href="../styles/style.css">
1212
<script src="../scripts/jquery-2.2.2.js"></script>
1313
<script src="../semantic/semantic.js"></script>
14+
15+
<style>
16+
td.actions > a {
17+
min-width: 5em;
18+
}
19+
td.actions > a:not(:first-child) {
20+
border-left: 2px solid #a333c8;
21+
padding-left: 4px;
22+
}
23+
</style>
1424
</head>
1525
<body>
1626
<div class="ui container">
@@ -28,6 +38,22 @@
2838
include './../util/connect.php';
2939
?>
3040

41+
<?php
42+
function category_count($categoryID) {
43+
global $mysqli;
44+
$stmt = $mysqli -> prepare('SELECT COUNT(*) FROM group_12.products WHERE categories = ' . intval($categoryID));
45+
$stmt -> bind_result($c);
46+
$stmt -> execute();
47+
$stmt -> fetch();
48+
49+
if (is_null($c) || !isset($c)){
50+
$c = 0;
51+
}
52+
53+
return $c;
54+
}
55+
?>
56+
3157
<?php
3258
$result = $mysqli->query("SELECT * FROM categories WHERE parent IS NULL");
3359
$total = mysqli_num_rows($result);
@@ -45,6 +71,17 @@
4571
));
4672
}
4773

74+
mysqli_free_result($result);
75+
76+
foreach ($categories as &$category) {
77+
echo 'Iterating ' . $category['name'] . '<br/>';
78+
$id = $category['id'];
79+
$category['item'] = category_count($id);
80+
$category['siblingHasItems'] = false;
81+
}
82+
83+
unset($category);
84+
4885
function organize($id)
4986
{
5087
global $mysqli;
@@ -65,6 +102,24 @@ function organize($id)
65102
"children" => array()
66103
));
67104
}
105+
mysqli_free_result($result);
106+
107+
$someHasItems = false;
108+
109+
foreach ($data as &$category) {
110+
$id = $category['id'];
111+
$category['item'] = category_count($id);
112+
113+
if ($category['item'] > 0) {
114+
$someHasItems = true;
115+
}
116+
}
117+
118+
foreach ($data as &$category) {
119+
$category['siblingHasItems'] = $someHasItems;
120+
}
121+
122+
unset($category);
68123

69124
return $data;
70125
}
@@ -91,28 +146,15 @@ function recurseOnCategory(array &$category)
91146
unset($category);
92147

93148
?>
94-
149+
<pre>
95150
<?php
96-
print_r($categories);
151+
print_r(json_encode($categories, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
97152
?>
98-
<?php
99-
function category_count($categoryID) {
100-
include './../util/connect.php';
101-
102-
$stmt = $mysqli -> prepare('SELECT COUNT(*) FROM group_12.products WHERE categories = ' . intval($categoryID));
103-
$stmt -> bind_result($c);
104-
$stmt -> execute();
105-
$stmt -> fetch();
106-
107-
include './../util/close.php';
153+
</pre>
108154

109-
return $c;
110-
}
111-
?>
112155
<?php
113156
// 先自己,再小孩
114157
function render($depth, array $self) {
115-
$catcount = category_count($self['id']);
116158
?>
117159
<tr>
118160
<?php
@@ -122,21 +164,21 @@ function render($depth, array $self) {
122164
<?php
123165
}
124166
?>
125-
<td colspan="<?=(4 - $depth)?>"><?=$self['name']?></td>
126-
<td><?=$catcount?></td>
127-
<td>
167+
<td colspan="<?=(3 - $depth)?>"><?=$self['name']?></td>
168+
<td><?=$self['item']?></td>
169+
<td class="actions">
128170
<?php
129-
if ($depth != 2 && $catcount == 0 || !empty($self['children'])) {
171+
if ($depth != 2 && $self['item'] == 0 || !empty($self['children'])) {
130172
?>
131-
<a href="category_new.php?id=<?$self['id']?>">新增子分類</a>
173+
<a href="category_new.php?id=<?=$self['id']?>">新增子分類</a>
132174
<?php
133175
}
134176
?>
135-
<a href="category_edit.php?id=<?$self['id']?>">編輯名稱</a>
177+
<a href="category_edit.php?id=<?=$self['id']?>">編輯名稱</a>
136178
<?php
137-
if ($catcount == 0 && empty($self['children'])) {
179+
if ($self['item'] == 0 && empty($self['children']) && !$self['siblingHasItems']) {
138180
?>
139-
<a href="category_delete.php?id=<?$self['id']?>">刪除分類</a>
181+
<a href="category_delete.php?id=<?=$self['id']?>">刪除分類</a>
140182
<?php
141183
}
142184
?>
@@ -158,7 +200,7 @@ function render($depth, array $self) {
158200
<table class="ui celled structured table">
159201
<thead>
160202
<tr>
161-
<th colspan="4">名稱</th>
203+
<th colspan="3">名稱</th>
162204
<th style="width: 25%;">產品數量</th>
163205
<th style="width: 25%;">動作</th>
164206
</tr>

admin/category_ajax.php

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Shane
5+
* Date: 2016/6/8
6+
* Time: 上午4:37
7+
*/
8+
9+
// 確認權限
10+
include 'AdminAuthenticationRequired.php';
11+
12+
// header('Content-Type: application/json');
13+
// echo json_encode($result);
14+
15+
switch ($_SERVER['REQUEST_METHOD']) {
16+
case 'GET':
17+
// 取得所有分類
18+
include_once './../util/connect.php';
19+
20+
$select_stmt = $mysqli -> prepare('SELECT id, name, parent from categories');
21+
$select_stmt -> bind_result($id, $name, $parent);
22+
$select_stmt -> execute();
23+
24+
$categories = [];
25+
26+
while ($select_stmt -> fetch()) {
27+
$categories[] = [
28+
'id' => $id,
29+
'name' => $name,
30+
'parent' => $parent
31+
];
32+
}
33+
34+
$select_stmt -> close();
35+
36+
foreach ($categories as &$category) {
37+
$id = $category['id'];
38+
39+
$stmt_aggr = $mysqli -> prepare('SELECT COUNT(*) FROM group_12.products WHERE categories = ?');
40+
$stmt_aggr -> bind_param('d', $id);
41+
$stmt_aggr -> bind_result($count);
42+
$stmt_aggr -> execute();
43+
$stmt_aggr -> fetch();
44+
$stmt_aggr -> close();
45+
46+
if (is_null($count)){
47+
$c = 0;
48+
}
49+
50+
$category['itemCount'] = $count;
51+
}
52+
53+
header('Content-Type: application/json');
54+
echo json_encode($categories);
55+
56+
include_once './../util/close.php';
57+
58+
break;
59+
case 'POST':
60+
// 擷取 JSON
61+
$requestBody = file_get_contents('php://input');
62+
$body = json_decode($requestBody, true);
63+
64+
// 確認有 action
65+
if (!isset($body['action'])) {
66+
throw new Exception('POST 主體缺乏動作種類(action)');
67+
}
68+
69+
$action = $body['action'];
70+
71+
switch ($action) {
72+
case 'insert':
73+
if (!isset($body['name'])) {
74+
throw new Exception('POST 主體缺乏新分類名稱(name)');
75+
}
76+
if (isset($body['parent'])) {
77+
// 是一個子分類
78+
$parent = intval($body['parent']);
79+
$name = $body['name'];
80+
81+
include_once './../util/connect.php';
82+
83+
$insert_stmt = $mysqli -> prepare('INSERT INTO categories (name, parent) VALUES (?, ?)');
84+
$insert_stmt -> bind_param('sd', $name, $parent);
85+
86+
if ($insert_stmt -> execute()) {
87+
header('Content-Type: application/json');
88+
echo json_encode([
89+
'success' => true
90+
]);
91+
} else {
92+
throw new Exception('資料庫插入失敗');
93+
}
94+
95+
include_once './../util/close.php';
96+
} else {
97+
// 是頂層分類
98+
$name = $body['name'];
99+
100+
include_once './../util/connect.php';
101+
102+
$insert_stmt = $mysqli -> prepare('INSERT INTO categories (name, parent) VALUES (?, NULL)');
103+
$insert_stmt -> bind_param('s', $name);
104+
105+
if ($insert_stmt -> execute()) {
106+
header('Content-Type: application/json');
107+
echo json_encode([
108+
'success' => true
109+
]);
110+
} else {
111+
throw new Exception('資料庫插入失敗');
112+
}
113+
114+
include_once './../util/close.php';
115+
}
116+
break;
117+
case 'delete':
118+
if (!isset($body['id'])) {
119+
throw new Exception('POST 主體缺乏要被刪除的分類 ID(id)');
120+
}
121+
122+
$id = intval($body['id']);
123+
124+
include_once './../util/connect.php';
125+
126+
$delete_stmt = $mysqli -> prepare('DELETE FROM categories WHERE id = ?');
127+
$delete_stmt -> bind_param('d', $id);
128+
129+
if ($delete_stmt -> execute()) {
130+
header('Content-Type: application/json');
131+
echo json_encode([
132+
'success' => true
133+
]);
134+
} else {
135+
throw new Exception('資料庫刪除失敗');
136+
}
137+
138+
include_once './../util/close.php';
139+
140+
break;
141+
case 'edit':
142+
if (!isset($body['id'])) {
143+
throw new Exception('POST 主體缺乏新的分類 ID(id)');
144+
}
145+
if (!isset($body['name'])) {
146+
throw new Exception('POST 主體缺乏新的分類名稱(name)');
147+
}
148+
149+
$id = intval($body['id']);
150+
$newName = $body['name'];
151+
152+
include_once './../util/connect.php';
153+
154+
$update_stmt = $mysqli -> prepare('UPDATE categories SET name = ? WHERE id = ?');
155+
$update_stmt -> bind_param('sd', $newName, $id);
156+
157+
if ($update_stmt -> execute()) {
158+
header('Content-Type: application/json');
159+
echo json_encode([
160+
'success' => true
161+
]);
162+
} else {
163+
throw new Exception('資料庫更新失敗');
164+
}
165+
166+
include_once './../util/close.php';
167+
168+
break;
169+
default:
170+
throw new Exception('伺服器不認識的動作');
171+
}
172+
break;
173+
default:
174+
throw new Exception("僅支援 GET 或是 POST");
175+
}

ajax/cart.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<?php
2+
include './../util/UserAuthenticationRequired.php';
3+
?>
4+
15
<?php
26
include './../util/connect.php';
37

0 commit comments

Comments
 (0)