EOF; $count_query = new CountGrabage(); // 查询垃圾分类数据量 try { DbUtil::query("select category,count(*) from garbage group by category order by category", $count_query); } catch (Exception $e) { error_res("系统出现严重异常,请联系管理员", "/"); return; } 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("垃圾名不能为空!", $_SERVER["PHP_SELF"]); } else if (empty($_POST["category"])) { error_res("垃圾分类不能为空!", $_SERVER["PHP_SELF"]); } else if (array_search((int)$_POST["category"], array(1, 2, 4, 8, 16)) === false) { error_res("垃圾分类参数不合法", $_SERVER["PHP_SELF"]); } else { try { DbUtil::insert("insert into garbage (name, category, create_at, update_at) values (?,?,?,?)", null, "siss", $_POST["name"], $_POST["category"], date(default_format), date(default_format)); info_res("保存成功", $_SERVER["PHP_SELF"]); } catch (Exception $e) { error($e); error_res("保存操作异常,请联系管理员", $_SERVER["PHP_SELF"]); } } } else if (isset($_GET["action"]) && $_GET["action"] == "list") { if (empty($_GET["category"])) { error_res("垃圾分类不能为空!", $_SERVER["PHP_SELF"]); } else if (array_search((int)$_GET["category"], array(1, 2, 4, 8, 16)) === false) { error_res("垃圾分类参数不合法", $_SERVER["PHP_SELF"]); } else { // 分页查询 $page = (int)($_GET["page"] ?? 1); $category_param = (int)$_GET["category"]; if (isset($_GET["name"])) { $name_param = $_GET["name"]; } else { $name_param = ""; } $count_query = new QueryGarbageCount(); // 获取记录数 try { if (empty($name_param)) { DbUtil::query("select count(*) from garbage where category=?", $count_query, array($category_param)); } else { DbUtil::query("select count(*) from garbage where category=? and name like ?", $count_query, array($category_param, "%" . $name_param . "%")); } } catch (Exception $e) { error_res("系统出现严重异常,请联系管理员", "/"); return; } $count_result = $count_query->getCount(); $garbage_query = new QueryGarbage(); // 分页大小 $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; } try { if (empty($name_param)) { DbUtil::query("select * from garbage where category=? limit ?,?", $garbage_query, array($category_param, ($page - 1) * $page_size, $page_size)); } else { DbUtil::query("select * from garbage where category=? and name like ? limit ?,?", $garbage_query, array($category_param, "%" . $name_param . "%", ($page - 1) * $page_size, $page_size)); } } catch (Exception $e) { error_res("系统出现严重异常,请联系管理员", "/"); return; } if (empty($garbage_query->getGarbageObjArray())) { error("无法查询分类" . get_category($category_param) . "的第" . $page . "数据"); warn_res("查询结果为空", $_SERVER["PHP_SELF"] . "?action=list&category=" . $category_param); return; } else { echo << function doDelete(name){ if(confirm("确认删除"+name+"?")){ EOF; echo 'location.href="' . $_SERVER["PHP_SELF"] . '?action=delete&name="+name'; echo <<
EOF; echo '
'; echo <<
垃圾名
垃圾名不为空
EOF; foreach ($garbage_query->getGarbageObjArray() 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 ''; } } echo <<
垃圾名 垃圾分类 创建时间 操作
$name $category $create_at
'; } } } else if (isset($_GET["action"]) && $_GET["action"] == "delete") { if (empty($_GET["name"])) { error_res("缺少垃圾名,无法删除!", $_SERVER["PHP_SELF"]); } else { try { DbUtil::delete("delete from garbage where name=?", null, "s", $_GET["name"]); info_res("删除成功", $_SERVER["PHP_SELF"]); } catch (Exception $e) { error($e); error_res("删除失败,请联系管理员", $_SERVER["PHP_SELF"]); } } } else { error("非法操作"); error_res("非法操作", "/"); }