From cf7d5b4c5a99057aaf91556f75e8503e6e3b83dd Mon Sep 17 00:00:00 2001 From: Gregorio Chiko Putra Date: Thu, 7 Sep 2017 11:21:15 +0700 Subject: [PATCH] Now can include WHERE clause on showAll() method --- App/Controllers/Posts.php | 18 +++++++++++++----- App/Models/Post.php | 32 +++++++++++++++++++++++++++----- Core/Model.php | 15 +++------------ 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/App/Controllers/Posts.php b/App/Controllers/Posts.php index ef1d0b8..efc46e3 100644 --- a/App/Controllers/Posts.php +++ b/App/Controllers/Posts.php @@ -26,7 +26,9 @@ class Posts $table = 'pengumuman'; - $valid = $this->post->showAll('valid_at', '<=', $now); + $valid = $this->post->showAll([ + ['valid_at', '<=', $now] + ]); if ($valid !== false) { foreach ($valid as $fields) { $id = $fields['id']; @@ -35,7 +37,9 @@ class Posts } } - $not_valid = $this->post->showAll('valid_at', '>', $now); + $not_valid = $this->post->showAll([ + ['valid_at', '>', $now] + ]); if ($not_valid !== false) { foreach ($not_valid as $fields) { $id = $fields['id']; @@ -44,7 +48,9 @@ class Posts } } - $expired = $this->post->showAll('expired_at', '<', $now); + $expired = $this->post->showAll([ + ['expired_at', '<', $now] + ]); if ($expired !== false) { foreach ($expired as $fields) { $id = $fields['id']; @@ -58,7 +64,9 @@ class Posts { $this->checkValid(); - $posts = $this->post->showAll('status', '=', 1); + $posts = $this->post->showAll([ + ['status', '=', 1] + ]); $url = 'Data/pengumuman.html'; @@ -196,7 +204,7 @@ class Posts Session::flash('info', 'Data successfuly updated'); Redirect::to('/'); } else { - Session::flash('info', 'Data must not be same'); + Session::flash('info', 'Error'); Redirect::to("./$id"); } } diff --git a/App/Models/Post.php b/App/Models/Post.php index 0763307..cca6c52 100644 --- a/App/Models/Post.php +++ b/App/Models/Post.php @@ -35,21 +35,43 @@ class Post extends \Core\Model ); } - public function showAll($key = '', $operator = '', $cond = '') + public function showAll($conditions = []) { try { $db = static::connectDB(); $sql = "SELECT * FROM pengumuman"; - if ($key && $operator && $cond) { - $sql .= " WHERE {$key} {$operator} ?"; + if ($conditions) { + $sql .= " WHERE"; + foreach ($conditions as $condition) { + + $keys[] = $condition[0]; + $operators[] = $condition[1]; + $values[] = $condition[2]; + } + + $x = 1; + $i = 0; + foreach ($keys as $key) { + $sql .= " $key $operators[$i] ?"; + $i++; + + $x++; + if ($x <= count($keys)) { + $sql .= " AND"; + } + } } $query = $db->prepare($sql); - if ($key && $operator && $cond) { - $query->bindValue(1, $cond); + if ($conditions) { + $x = 1; + foreach ($values as $value) { + $query->bindValue($x, $value); + $x++; + } } if ($query->execute()) { diff --git a/Core/Model.php b/Core/Model.php index 314a9b1..83aa809 100644 --- a/Core/Model.php +++ b/Core/Model.php @@ -144,18 +144,9 @@ abstract class Model try { $db = static::connectDB(); - $result = $this->showAll('id', '=', $id); - - foreach ($result as $post) { - foreach ($post as $key => $val) { - if (isset($args[$key])) { - if ($args[$key] == $post[$key]) { - return false; - } - } - } - } - + $result = $this->showAll([ + ['id', '=', $id] + ]); $sql = "UPDATE {$table} SET {$fields} WHERE id = ?";