From e666991b608ffdbe6f374ed9c54d44aee67afc42 Mon Sep 17 00:00:00 2001 From: Malte Schulze Hobeling Date: Wed, 18 Jan 2023 10:51:28 +0100 Subject: [PATCH] better select --- BancaDati/BancaDati.php | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/BancaDati/BancaDati.php b/BancaDati/BancaDati.php index 7dc16f4..9690979 100644 --- a/BancaDati/BancaDati.php +++ b/BancaDati/BancaDati.php @@ -102,27 +102,30 @@ class BancaDati { /** * einheitliche Select Funktion + * kann sortiert werden durch ["by"]=>"col" und ["order"]=>"ASC|DESC" * @param string $table * @param array $where ["column"]=>"value" es wird mit LIKE verglichen und mit AND verbunden - * @param array|null $order ["by"]=>"column"; ["order"]=>"ASC|DESC" * @return void * @author Malte Schulze Hobeling */ - public function select(string $table, array $where, array $order = null){ + public function select(string $table, array $where){ $whereString = ""; + $orderString = ""; + if(isset($where["by"])){ + $orderString = " ORDER BY " . $where["by"]; + unset($where["by"]); + if(isset($where["order"])){ + $orderString .= " " . $where["order"]; + unset($where["order"]); + } + } foreach ($where as $col => $v) { if($whereString != ""){ $whereString .= " AND "; } $whereString .= "`" . $col . "` LIKE '" . $v . "'"; } - $sql = "SELECT * FROM ".$table." WHERE ".$whereString.";"; - if(isset($order["by"])){ - $sql .= " ORDER BY ".$order["by"]; - } - if(isset($order["order"])){ - $sql .= $order["order"]; - } + $sql = "SELECT * FROM ".$table." WHERE ".$whereString.$orderString.";"; try { return $this->pdo->query($sql)->fetch(); }catch (PDOException $e){