Now can include WHERE clause on showAll() method

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

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