diff --git a/admin/Alert.php b/admin/Alert.php new file mode 100644 index 0000000..18a5a5f --- /dev/null +++ b/admin/Alert.php @@ -0,0 +1,48 @@ + + function doClick() { + $script + } + +
+ +
+EOF; +} + +function response_with_href($message, $class, $href) +{ + response_with_click($message, $class, << - function doClick() { - $script - } - -
- -
-EOF; -} - -function response_with_href($message, $class, $href) -{ - reponse_with_click($message, $class, << @@ -75,7 +29,7 @@ if (empty($_POST) && empty($_GET)) { + + + +
+
+
+
+EOF; + $count_query = new CountGrabage(); +// 查询垃圾分类数据量 + DbUtil::query("select category,count(*) from garbage group by category order by category", $count_query); + if (empty($count_query->getResult())) { + warn_res_with_click("暂无垃圾分类数据,请添加", <<getResult() as $item) { + $category = $item["category"]; + $category_name = $item["category_name"]; + $count = $item["count"]; + echo << + $category_name + $count 条 + + EOF; + } + } + echo << +
+
+
+
+ +
+
+ 垃圾名 +
+ +
+ 垃圾名不为空 +
+
+ +
+
+ 垃圾分类 +
+ +
+ 请选择垃圾分类 +
+
+ + +
+
+
+ + + +EOF; + +} else if (isset($_POST["action"]) && $_POST["action"] == "add") { + if (empty($_POST["name"])) { + error_res("垃圾名不能为空!", "/admin/WasteSorting.php"); + } else if (empty($_POST["category"])) { + error_res("垃圾分类不能为空!", "/admin/WasteSorting.php"); + } else if (array_search((int)$_POST["category"], array(1, 2, 4, 8, 16)) === false) { + error_res("垃圾分类参数不合法", "/admin/WasteSorting.php"); + } else { + try { + DbUtil::insert("insert into garbage (name, category, create_at, update_at) values (?,?,?,?)", "siss", $_POST["name"], $_POST["category"], date(default_format), date(default_format)); + info_res("保存成功", "/admin/WasteSorting.php"); + } catch (Exception $e) { + error($e); + error_res("保存操作异常,请联系管理员", "/admin/WasteSorting.php"); + } + + } +} else if (isset($_GET["action"]) && $_GET["action"] == "list") { + if (empty($_GET["category"])) { + error_res("垃圾分类不能为空!", "/admin/WasteSorting.php"); + } else if (array_search((int)$_GET["category"], array(1, 2, 4, 8, 16)) === false) { + error_res("垃圾分类参数不合法", "/admin/WasteSorting.php"); + } else { +// 分页查询 + $page = (int)($_GET["page"] ?? 1); + $category_param = (int)$_GET["category"]; + $count_query = new QueryGarbageCount(); +// 获取记录数 + DbUtil::query("select count(*) from garbage where category=?", $count_query, array($category_param)); + $count_result = $count_query->getCount(); + $garbage_query = new QueryGarbageWithCategory(); +// 分页大小 + $page_size = 10; + // 计算分页数 + if ($count_result < $page_size) { + $max_page = 1; + } else if ($count_result % $page_size == 0) { + $max_page = $count_result / $page_size; + } else { + $max_page = (int)floor($count_result / $page_size) + 1; + } + DbUtil::query("select * from garbage where category=? limit ?,?", $garbage_query, array($category_param, ($page - 1) * $page_size, $page_size)); + if (empty($garbage_query->getResultList())) { + error("无法查询分类" . get_category($category_param) . "的第" . $page . "数据"); + error_res("非法查询,请联系管理员", "/admin/WasteSorting.php"); + } else { + echo << + + + + + + + + + + +EOF; + + foreach ($garbage_query->getResultList() as $garbage) { + if ($garbage instanceof Garbage) { + $name = $garbage->getName(); + $category = get_category($garbage->getCategory()); + $create_at = $garbage->getCreateAt(); + $update_at = $garbage->getUpdateAt(); + echo << + + + + + +EOF; + } + } + echo << +
垃圾名垃圾分类创建时间修改时间
$name$category$create_at$update_at
+ + +
+ 返回 +
+
+ + EOF; + } + } +} else { + error("非法操作"); + error_res("非法操作", "/"); +} \ No newline at end of file diff --git a/api/BaiduImage.php b/api/BaiduImage.php index d7656cc..da079b7 100644 --- a/api/BaiduImage.php +++ b/api/BaiduImage.php @@ -28,7 +28,7 @@ function query_waste_soring($image_res) $sql = "select * from garbage where" . substr($sql, 3); info("查询sql" . $sql); - $garbage_result = getGarbageQuery($sql, $keyword); + $garbage_result = QueryGarbageWithName($sql, $keyword); json_res(json_encode(array("img_parse" => true, "query" => true, "img_res" => $image_res, "result" => $garbage_result->getGarbageObjArray()), JSON_UNESCAPED_UNICODE)); } else { diff --git a/api/QueryText.php b/api/QueryText.php index fe1d4a3..1f9a624 100644 --- a/api/QueryText.php +++ b/api/QueryText.php @@ -36,10 +36,10 @@ class QueryText if (empty($_GET)) { json_res(json_encode(array("status" => false, "error" => "非法请求"), JSON_UNESCAPED_UNICODE)); } else if (isset($_GET["keyword"]) and !empty($_GET["keyword"])) { - $result = getGarbageQuery("select * from garbage where name like ?", array("%" . $_GET["keyword"] . "%")); + $result = QueryGarbageWithName("select * from garbage where name like ?", array("%" . $_GET["keyword"] . "%")); json_res(json_encode(array("status" => true, "result" => $result->getGarbageObjArray(), "keyword" => $_GET["keyword"]), JSON_UNESCAPED_UNICODE)); } else if (isset($_GET["category"]) and is_numeric($_GET["category"])) { - $result = getGarbageQuery("select * from garbage where category = ?", array((int)$_GET["category"])); + $result = QueryGarbageWithName("select * from garbage where category = ?", array((int)$_GET["category"])); json_res(json_encode(array("status" => true, "result" => $result->getGarbageObjArray(), "category" => $_GET["category"]), JSON_UNESCAPED_UNICODE)); } else { json_res(json_encode(array("status" => false, "error" => "非法查询参数"), JSON_UNESCAPED_UNICODE)); diff --git a/database/Query.php b/database/Query.php index f63df62..63a6fb1 100644 --- a/database/Query.php +++ b/database/Query.php @@ -67,8 +67,7 @@ abstract class AbstractGarbageQuery implements DoExcute $stmt->bind_result($col1, $col2, $col3, $col4); while ($stmt->fetch()) { - $g = new Garbage($col1, $col2, $col3, $col4); - array_push($this->garbage_obj_array, $g); + array_push($this->garbage_obj_array, new Garbage($col1, $col2, $col3, $col4)); } } @@ -84,7 +83,8 @@ abstract class AbstractGarbageQuery implements DoExcute } -function getGarbageQuery($sql, $keyword): AbstractGarbageQuery +//根据垃圾名模糊匹配垃圾分类信息 +function QueryGarbageWithName($sql, $keyword): AbstractGarbageQuery { $garbage_result = new class() extends AbstractGarbageQuery { @@ -104,6 +104,7 @@ function getGarbageQuery($sql, $keyword): AbstractGarbageQuery return $garbage_result; } +//查询问题 class QuestionQuery implements DoExcute { @@ -118,8 +119,7 @@ class QuestionQuery implements DoExcute { $stmt->bind_result($col1, $col2); while ($stmt->fetch()) { - $q = new Question($col1, $col2); - array_push($this->question_array, $q); + array_push($this->question_array, new Question($col1, $col2)); } } @@ -151,8 +151,7 @@ class AnswerQuery implements DoExcute { $stmt->bind_result($col1, $col2, $col3, $col4); while ($stmt->fetch()) { - $a = new Answer($col1, $col2, $col3, $col4); - array_push($this->answer_array, $a); + array_push($this->answer_array, new Answer($col1, $col2, $col3, $col4)); } } @@ -167,9 +166,27 @@ class AnswerQuery implements DoExcute } -//查询问题 -class QueryRandQuestion implements DoExcute +function get_category($category) +{ + switch ($category) { + case 1: + return "可回收垃圾"; + case 2: + return "有害垃圾"; + case 4: + return "湿垃圾"; + case 8: + return "干垃圾"; + case 16: + return "大件垃圾"; + } +} + +//统计垃圾分类数据量 +class CountGrabage implements DoExcute { + private $result = array(); + public function bind_param(mysqli_stmt $stmt, array $param = null) { @@ -177,11 +194,79 @@ class QueryRandQuestion implements DoExcute public function doResult(mysqli_stmt $stmt) { - $stmt->bind_result($col1, $col2, $col3, $col4, $col5); + $stmt->bind_result($col1, $col2); while ($stmt->fetch()) { + array_push($this->result, array("category" => $col1, "category_name" => get_category($col1), "count" => $col2)); + } + } + + /** + * @return array + */ + public function getResult(): array + { + return $this->result; + } + +} + +//查询某个垃圾分类下的垃圾数据 +class QueryGarbageWithCategory implements DoExcute +{ + private $result_list = array(); + + public function bind_param(mysqli_stmt $stmt, array $param = null) + { + if (!empty($param)) { + $stmt->bind_param("iii", $param[0], ...array_slice($param, 1)); } } + + public function doResult(mysqli_stmt $stmt) + { + $stmt->bind_result($col1, $col2, $col3, $col4); + while ($stmt->fetch()) { + array_push($this->result_list, new Garbage($col1, $col2, $col3, $col4)); + } + } + + /** + * @return array + */ + public function getResultList(): array + { + return $this->result_list; + } } -; \ No newline at end of file +class QueryGarbageCount implements DoExcute +{ + + private $count; + + public function bind_param(mysqli_stmt $stmt, array $param = null) + { + if (!empty($param)) { + $stmt->bind_param("i", $param[0]); + } + } + + public function doResult(mysqli_stmt $stmt) + { + $stmt->bind_result($col1); + while ($stmt->fetch()) { + $this->count = $col1; + } + } + + /** + * @return mixed + */ + public function getCount() + { + return $this->count; + } + + +} \ No newline at end of file diff --git a/test.php b/test.php index 30df085..d4e3b7b 100644 --- a/test.php +++ b/test.php @@ -3,6 +3,7 @@ require_once "config.php"; require_once __ROOT__ . "/database/DbUtil.php"; require_once __ROOT__ . "/entity/Question.php"; require_once __ROOT__ . "/entity/Answer.php"; +require_once __ROOT__ . "/Log.php"; //echo << @@ -22,8 +23,7 @@ require_once __ROOT__ . "/entity/Answer.php"; // //EOF; -var_dump(array_search(new Answer(1, 2, 3, 4), array(new Answer(4, 5, 6, 7), new Answer(2, 2, 3, 4)))); - - +//var_dump(array_search(new Answer(1, 2, 3, 4), array(new Answer(4, 5, 6, 7), new Answer(2, 2, 3, 4)))); +var_dump(array_search(4, array(2, 5, 1)));