- Methods that are same (e.g. entry(), update(), delete(), etc.) defined in the parent.
- Added function when entry multiple rows (in this case is when putting categories). - Do test.
This commit is contained in:
@@ -54,93 +54,4 @@ class Access extends \Core\Model
|
||||
echo $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function entry($args)
|
||||
{
|
||||
if (count($args)) {
|
||||
$keys = '`'.implode('`, `', array_keys($args)).'`';
|
||||
|
||||
$x = 1;
|
||||
$values = '';
|
||||
foreach ($args as $field) {
|
||||
$values .= '?';
|
||||
if ($x < count($args)) {
|
||||
$values .= ', ';
|
||||
}
|
||||
$x++;
|
||||
}
|
||||
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "INSERT INTO user ({$keys}) VALUES ({$values})";
|
||||
|
||||
$query = $db->prepare($sql);
|
||||
|
||||
$x = 1;
|
||||
foreach ($args as $value) {
|
||||
$query->bindValue($x, $value);
|
||||
$x++;
|
||||
}
|
||||
|
||||
if ($query->execute()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function update($args, $id)
|
||||
{
|
||||
if (count($args)) {
|
||||
$keys = array_keys($args);
|
||||
|
||||
$fields = [];
|
||||
foreach ($keys as $key) {
|
||||
$fields[] = $key.' = ?';
|
||||
}
|
||||
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "UPDATE user SET ".implode(', ', $fields)." WHERE id = ?";
|
||||
|
||||
$query = $db->prepare($sql);
|
||||
$x = 1;
|
||||
foreach ($args as $value) {
|
||||
$query->bindValue($x, $value);
|
||||
$x++;
|
||||
}
|
||||
$query->bindValue($x, $id);
|
||||
|
||||
if ($query->execute()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function delete($id)
|
||||
{
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "DELETE FROM user WHERE id = ?";
|
||||
|
||||
$query = $db->prepare($sql);
|
||||
|
||||
if ($query->execute([$id])) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ class Post extends \Core\Model
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
// Create table for posts
|
||||
$this->createTable(
|
||||
'pengumuman',
|
||||
[
|
||||
@@ -19,6 +20,16 @@ class Post extends \Core\Model
|
||||
'PRIMARY KEY (id)'
|
||||
]
|
||||
);
|
||||
|
||||
// Create table for categories
|
||||
$this->createTable(
|
||||
'kategori',
|
||||
[
|
||||
'id int(3) NOT NULL AUTO_INCREMENT',
|
||||
'category varchar(20) NOT NULL',
|
||||
'PRIMARY KEY (id)'
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function showAll()
|
||||
@@ -60,95 +71,4 @@ class Post extends \Core\Model
|
||||
echo $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function entry($args)
|
||||
{
|
||||
if (count($args)) {
|
||||
$keys = '`'.implode('`, `', array_keys($args)).'`';
|
||||
|
||||
$x = 1;
|
||||
$values = '';
|
||||
foreach ($args as $field) {
|
||||
$values .= '?';
|
||||
if ($x < count($args)) {
|
||||
$values .= ', ';
|
||||
}
|
||||
$x++;
|
||||
}
|
||||
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "INSERT INTO pengumuman ({$keys}) VALUES ({$values})";
|
||||
|
||||
$query = $db->prepare($sql);
|
||||
|
||||
$x = 1;
|
||||
|
||||
foreach ($args as $val) {
|
||||
$query->bindValue($x, urldecode($val));
|
||||
$x++;
|
||||
}
|
||||
|
||||
if ($query->execute()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function update($args, $id)
|
||||
{
|
||||
if (count($args)) {
|
||||
$keys = array_keys($args);
|
||||
|
||||
$fields = [];
|
||||
foreach ($keys as $key) {
|
||||
$fields[] = $key.' = ?';
|
||||
}
|
||||
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "UPDATE pengumuman SET ".implode(', ', $fields)." WHERE id = ?";
|
||||
|
||||
$query = $db->prepare($sql);
|
||||
$x = 1;
|
||||
foreach ($args as $value) {
|
||||
$query->bindValue($x, $value);
|
||||
$x++;
|
||||
}
|
||||
$query->bindValue($x, $id);
|
||||
|
||||
if ($query->execute()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function delete($id)
|
||||
{
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "DELETE FROM pengumuman WHERE id = ?";
|
||||
|
||||
$query = $db->prepare($sql);
|
||||
|
||||
if ($query->execute([$id])) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user