lepisi-pengumuman/App/Models/Post.php

96 lines
2.4 KiB
PHP

<?php
namespace App\Models;
class Post extends \Core\Model
{
public function __construct()
{
// Create table for posts
$this->createTable(
'pengumuman',
[
'id int(3) NOT NULL AUTO_INCREMENT',
'category int(3) NOT NULL',
'created_at timestamp DEFAULT CURRENT_TIMESTAMP',
'expired_at timestamp NOT NULL',
'creator int(3) NOT NULL',
'edited_at timestamp',
'editor timestamp',
'content varchar(255) NOT NULL',
'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()
{
try {
$db = static::connectDB();
$sql = "SELECT * FROM pengumuman WHERE status = ? ORDER BY created_at";
$query = $db->prepare($sql);
$query->bindValue(1, 1);
if ($query->execute()) {
if ($query->rowCount() != 0) {
$result = $query->fetchAll(\PDO::FETCH_ASSOC);
return $result;
}
}
} catch (PDOException $e) {
echo $e->getMessage();
}
}
public function showSingle($id)
{
try {
$db = static::connectDB();
$sql = "SELECT * FROM pengumuman WHERE id = ?";
$query = $db->prepare($sql);
if ($query->execute([$id])) {
if ($query->rowCount() === 1) {
$result = $query->fetchAll(\PDO::FETCH_ASSOC);
return $result;
}
}
} catch (PDOException $e) {
echo $e->getMessage();
}
}
public function showCategories()
{
try {
$db = static::connectDB();
$sql = "SELECT * FROM kategori";
$query = $db->prepare($sql);
if ($query->execute()) {
if ($query->rowCount() > 1) {
$results = $query->fetchAll(\PDO::FETCH_ASSOC);
return $results;
}
}
} catch (PDOException $e) {
echo $e->getMessage();
}
}
}