From f8fe1a835c0e1c7db740e9bf6cd3143c40d73e78 Mon Sep 17 00:00:00 2001 From: Jordy van Zeeland Date: Thu, 15 Aug 2024 08:54:44 +0200 Subject: [PATCH] changes --- database.php | 62 +++++++++++++++++++++++++++++++++++++++++++++++----- router.php | 2 +- template.php | 2 +- 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/database.php b/database.php index 4fc7972..e3b991d 100644 --- a/database.php +++ b/database.php @@ -3,7 +3,6 @@ use PDO; Class Database{ - global $db_config; private $pdo; private $type; private $table; @@ -12,34 +11,68 @@ Class Database{ private $limit; private $query; + /** + * Constructor method to initialize the PDO instance + */ + public function __construct(){ $this->pdo = new PDO('mysql:host=' . $db_config['host'] . ';dbname=' . $db_config['database'] . ';charset=utf8mb4', $db_config['username'], $db_config['password']); } + /** + * Method to start a SELECT query and specify columns + */ + public function select($columns = "*"){ $this->type = "SELECT"; $this->columns = $columns; } + /** + * Method to specify the table to query + */ + public function from($table){ $this->table = $table; } - public function where($key, value){ + /** + * Method to add a WHERE clause to the query + */ + + public function where($key, $value){ $this->where = array($key, $value); } - public function andWhere($key, value){ + /** + * Method to add additional conditions to the WHERE clause + */ + + public function andWhere($key, $value){ if(!$this->where) $this->where = array(); $this->where[$key] = $value; } + /** + * Method to add a LIMIT clause to the query + */ + public function limit($number){ $this->limit = $number; } + /** + * Method to execute the query and return all results + */ + public function all(){ $this->query = $this->type . ' ' . $this->columns . ' FROM ' . $this->table; + + /** + * Check if a WHERE clause is set, if so, then append it to the query. + * Then construct WHERE conditions using placeholders for prepared statements and + * join the WHERE conditions with AND and append them to the query + */ if(!empty($this->where)){ $whereClauses = []; @@ -54,6 +87,11 @@ Class Database{ } $results = $this->pdo->prepare($Query); + + /** + * Bind the values from the WHERE clause to the prepared statement + */ + foreach($this->where as $condition){ $results->bindValue(':' . $condition[0], $condition[1]); } @@ -62,8 +100,18 @@ Class Database{ return $results->fetchAll(); } + /** + * Method to execute the query and return a single result + */ + public function one(){ $this->query = $this->type . ' ' . $this->columns . ' FROM ' . $this->table; + + /** + * Check if a WHERE clause is set, if so, then append it to the query. + * Then construct WHERE conditions using placeholders for prepared statements and + * join the WHERE conditions with AND and append them to the query + */ if(!empty($this->where)){ $whereClauses = []; @@ -73,9 +121,13 @@ Class Database{ $this->query .= ' WHERE ' . implode(' AND ', $whereClauses); } - $this->query .= " LIMIT " . $this->limit; - + $this->query .= " LIMIT 1"; $results = $this->pdo->prepare($Query); + + /** + * Bind the values from the WHERE clause to the prepared statement + */ + foreach($this->where as $condition){ $results->bindValue(':' . $condition[0], $condition[1]); } diff --git a/router.php b/router.php index ec5440a..372b6ba 100644 --- a/router.php +++ b/router.php @@ -53,6 +53,6 @@ class Router{ // '/contact' => 'controllers/contact.php', // ]; -// $router = (new Router)->routeToController($routes); +// $router = Router::routeToController($routes); ?> \ No newline at end of file diff --git a/template.php b/template.php index 215f27a..55fab29 100644 --- a/template.php +++ b/template.php @@ -20,6 +20,6 @@ class Template{ } -// $templ = (new Template)->render('about', '['title' => 'About']'); +// $templ = Template::render('about', '['title' => 'About']'); ?> \ No newline at end of file