Now can include WHERE clause on showAll() method

This commit is contained in:
Gregorio Chiko Putra 2017-09-07 11:21:15 +07:00
parent 041e4dd2dd
commit cf7d5b4c5a
3 changed files with 43 additions and 22 deletions

View File

@ -26,7 +26,9 @@ class Posts
$table = 'pengumuman'; $table = 'pengumuman';
$valid = $this->post->showAll('valid_at', '<=', $now); $valid = $this->post->showAll([
['valid_at', '<=', $now]
]);
if ($valid !== false) { if ($valid !== false) {
foreach ($valid as $fields) { foreach ($valid as $fields) {
$id = $fields['id']; $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) { if ($not_valid !== false) {
foreach ($not_valid as $fields) { foreach ($not_valid as $fields) {
$id = $fields['id']; $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) { if ($expired !== false) {
foreach ($expired as $fields) { foreach ($expired as $fields) {
$id = $fields['id']; $id = $fields['id'];
@ -58,7 +64,9 @@ class Posts
{ {
$this->checkValid(); $this->checkValid();
$posts = $this->post->showAll('status', '=', 1); $posts = $this->post->showAll([
['status', '=', 1]
]);
$url = 'Data/pengumuman.html'; $url = 'Data/pengumuman.html';
@ -196,7 +204,7 @@ class Posts
Session::flash('info', 'Data successfuly updated'); Session::flash('info', 'Data successfuly updated');
Redirect::to('/'); Redirect::to('/');
} else { } else {
Session::flash('info', 'Data must not be same'); Session::flash('info', 'Error');
Redirect::to("./$id"); Redirect::to("./$id");
} }
} }

View File

@ -35,21 +35,43 @@ class Post extends \Core\Model
); );
} }
public function showAll($key = '', $operator = '', $cond = '') public function showAll($conditions = [])
{ {
try { try {
$db = static::connectDB(); $db = static::connectDB();
$sql = "SELECT * FROM pengumuman"; $sql = "SELECT * FROM pengumuman";
if ($key && $operator && $cond) { if ($conditions) {
$sql .= " WHERE {$key} {$operator} ?"; $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); $query = $db->prepare($sql);
if ($key && $operator && $cond) { if ($conditions) {
$query->bindValue(1, $cond); $x = 1;
foreach ($values as $value) {
$query->bindValue($x, $value);
$x++;
}
} }
if ($query->execute()) { if ($query->execute()) {

View File

@ -144,18 +144,9 @@ abstract class Model
try { try {
$db = static::connectDB(); $db = static::connectDB();
$result = $this->showAll('id', '=', $id); $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;
}
}
}
}
$sql = "UPDATE {$table} SET {$fields} WHERE id = ?"; $sql = "UPDATE {$table} SET {$fields} WHERE id = ?";