From 77cc24bd2926cb403a7fc4e9f54da4e8c77317af Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Fri, 22 Feb 2019 00:02:28 +0100 Subject: [PATCH] refactor table names --- ajax.php | 4 ++-- index.php | 20 ++++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/ajax.php b/ajax.php index 817f837..c12817e 100644 --- a/ajax.php +++ b/ajax.php @@ -32,7 +32,7 @@ switch($_GET["action"]) { case "add_found": if (hasval($_POST["was"]) && hasval($_POST["wann"]) && hasval($_POST["wo"])) { /* Prepared statement, stage 1: prepare */ - if (!($stmt = $mysqli->prepare("INSERT INTO items(was, wann, wo) VALUES (?, ?, ?)"))) { + if (!($stmt = $mysqli->prepare("INSERT INTO found_items(was, wann, wo) VALUES (?, ?, ?)"))) { $errormsg = "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error; } if (!$stmt->bind_param("sss", $_POST["was"], $_POST["wann"], $_POST["wo"])) { @@ -49,7 +49,7 @@ switch($_GET["action"]) { case "add_lost": if (hasval($_POST["was"]) && hasval($_POST["wann"]) && hasval($_POST["wo"]) && hasval($_POST["contact"])) { /* Prepared statement, stage 1: prepare */ - if (!($stmt = $mysqli->prepare("INSERT INTO lost(was, wann, wo, contact) VALUES (?, ?, ?, ?)"))) { + if (!($stmt = $mysqli->prepare("INSERT INTO lost_items(was, wann, wo, contact) VALUES (?, ?, ?, ?)"))) { $errormsg = "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error; } if (!$stmt->bind_param("ssss", $_POST["was"], $_POST["wann"], $_POST["wo"], $_POST["contact"])) { diff --git a/index.php b/index.php index a62d7d6..6bdf37d 100644 --- a/index.php +++ b/index.php @@ -23,9 +23,9 @@ function auth(){ function get_founditems($onlyUnmatched = false){ global $mysqli; if ($onlyUnmatched) { - $res = $mysqli->query("SELECT items.* FROM items LEFT JOIN matches ON items.id = matches.f_id WHERE matches.f_id IS NULL ORDER BY items.id DESC"); + $res = $mysqli->query("SELECT found_items.*, files.hash FROM files, found_items LEFT JOIN matches ON found_items.id = matches.f_id WHERE files.item_id = found_items.id AND found_items.del = 0 AND matches.f_id IS NULL ORDER BY found_items.id DESC"); } else { - $res = $mysqli->query("SELECT * FROM items ORDER BY id ASC"); + $res = $mysqli->query("SELECT found_items.*, files.hash FROM files, found_items WHERE files.item_id = found_items.id AND found_items.del = 0 ORDER BY found_items.id ASC"); } $ret = array(); while ($row = $res->fetch_assoc()) { @@ -37,9 +37,9 @@ function get_founditems($onlyUnmatched = false){ function get_lostitems($onlyUnmatched = false){ global $mysqli; if ($onlyUnmatched) { - $res = $mysqli->query("SELECT lost.* FROM lost LEFT JOIN matches ON lost.id = matches.l_id WHERE matches.l_id IS NULL ORDER BY lost.id DESC"); + $res = $mysqli->query("SELECT lost_items.* FROM lost_items LEFT JOIN matches ON lost_items.id = matches.l_id WHERE lost_items.del = 0 AND matches.l_id IS NULL ORDER BY lost_items.id DESC"); } else { - $res = $mysqli->query("SELECT * FROM lost ORDER BY id ASC"); + $res = $mysqli->query("SELECT * FROM lost_items WHERE lost_items.del = 0 ORDER BY id ASC"); } $ret = array(); while ($row = $res->fetch_assoc()) { @@ -64,11 +64,19 @@ function get_stats(){ $ret = array(); $ret["lost"]=0; $ret["found"]=0; - $res = $mysqli->query("SELECT COUNT(*) AS c FROM lost ORDER BY id ASC"); + + + + $res = $mysqli->query("select hour(date) as h, day(date) as d, count(date) as c from items group by h, d order by d, h"); + $ret["graph"] = array(); + while ($row = $res->fetch_assoc()) { + $ret["graph"][] = $row; + } + $res = $mysqli->query("SELECT COUNT(*) AS c FROM lost_items WHERE lost_items.del = 0 ORDER BY id ASC"); if ($row = $res->fetch_assoc()) { $ret["lost"] = $row["c"]; } - $res = $mysqli->query("SELECT COUNT(*) AS c FROM items ORDER BY id ASC"); + $res = $mysqli->query("SELECT COUNT(*) AS c FROM found_items WHERE found_items.del = 0 ORDER BY id ASC"); if ($row = $res->fetch_assoc()) { $ret["found"] = $row["c"]; }