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';
$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");
}
}

View File

@ -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()) {

View File

@ -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 = ?";