From 84bf6b2d365ca8a7c89c60f7e336c538ce20e5ab Mon Sep 17 00:00:00 2001 From: Malte Schulze Hobeling Date: Wed, 11 Jan 2023 14:40:12 +0100 Subject: [PATCH] add: select --- BancaDati/BancaDati.php | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/BancaDati/BancaDati.php b/BancaDati/BancaDati.php index c508566..73c056e 100644 --- a/BancaDati/BancaDati.php +++ b/BancaDati/BancaDati.php @@ -100,25 +100,26 @@ class BancaDati { } } - /** - * Einheitliche select Funktion - * @param string $table - * @param array $selects - * @param array $data - * @return void - * @author Malte Schulze Hobeling - */ - public function select(string $table, array $selects, array $data){ - $view = ""; - foreach ($selects as $select){ - $view .= $select . ","; - } - $view = trim($view,","); + + public function select(string $table, array $data, array $order = null){ $where = ""; foreach ($data as $col => $v) { - $where .= $col . "=" . $v . " AND "; + if($where != ""){ + $where .= " AND "; + } + $where .= $col . "=" . $v; + } + $sql = "SELECT * FROM ".$table." WHERE ".$where; + if(isset($order["by"])){ + $sql .= " ORDER BY ".$order; + } + if(isset($order["order"])){ + $sql .= $order["order"]; + } + try { + return $this->pdo->query($sql); + }catch (PDOException $e){ + die; } - $where = trim($where,"AND "); - $sql = "SELECT ".$view." FROM ".$table." WHERE ".$where.";"; } } \ No newline at end of file