From 1e770f1fb0722ac83418ed447cf41e5a62a67569 Mon Sep 17 00:00:00 2001 From: frederik Date: Sat, 24 May 2025 15:15:54 +0200 Subject: [PATCH] common fetch_species_array() --- scripts/common.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/scripts/common.php b/scripts/common.php index 26f92c5..b716685 100644 --- a/scripts/common.php +++ b/scripts/common.php @@ -117,6 +117,26 @@ function get_sci_name($com_name) { return $sciname; } +function fetch_species_array($sort_by, $date=null) { + if (!isset($_db)) { + $_db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_READONLY); + $_db->busyTimeout(1000); + } + $where = (isset($date)) ? "WHERE Date == \"$date\"" : ""; + if ($sort_by === "occurrences") { + $statement = $_db->prepare("SELECT Date, Time, File_Name, Com_Name, Sci_Name, COUNT(*) as Count, MAX(Confidence) as MaxConfidence FROM detections $where GROUP BY Sci_Name ORDER BY COUNT(*) DESC"); + } elseif ($sort_by === "confidence") { + $statement = $_db->prepare("SELECT Date, Time, File_Name, Com_Name, Sci_Name, COUNT(*) as Count, MAX(Confidence) as MaxConfidence FROM detections $where GROUP BY Sci_Name ORDER BY MAX(Confidence) DESC"); + } elseif ($sort_by === "date") { + $statement = $_db->prepare("SELECT Date, Time, File_Name, Com_Name, Sci_Name, COUNT(*) as Count, MAX(Confidence) as MaxConfidence FROM detections $where GROUP BY Sci_Name ORDER BY MIN(Date) DESC, Time DESC"); + } else { + $statement = $_db->prepare("SELECT Date, Time, File_Name, Com_Name, Sci_Name, COUNT(*) as Count, MAX(Confidence) as MaxConfidence FROM detections $where GROUP BY Sci_Name ORDER BY Com_Name ASC"); + } + ensure_db_ok($statement); + $result = $statement->execute(); + return $result; +} + define('DB', './scripts/flickr.db'); class Flickr {