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
EOF; } } } else { error("非法操作"); error_res("非法操作", "/"); }