From e1d70ed8f41461fab1a061450de88e15b3370047 Mon Sep 17 00:00:00 2001 From: Gregorio Chiko Putra Date: Mon, 4 Sep 2017 16:20:27 +0700 Subject: [PATCH] Receive data from database and pass to html using twig --- App/Config.php | 4 ++-- App/Controllers/Home.php | 3 ++- App/Controllers/Posts.php | 31 +++++++++++++++++++++------- App/Models/Post.php | 28 +++++++++++++++++++------ App/Views/Data/entry_pengumuman.html | 7 +++---- App/Views/Data/pengumuman.html | 16 +++++--------- 6 files changed, 57 insertions(+), 32 deletions(-) diff --git a/App/Config.php b/App/Config.php index fc9c12b..ad051d7 100644 --- a/App/Config.php +++ b/App/Config.php @@ -4,8 +4,8 @@ namespace App; class Config { const - DB_HOST = '127.0.0.1', - DB_DB = 'cfp_test', + DB_HOST = 'mariadb', + DB_DB = 'lepisi', DB_UNAME = 'root', DB_PWD = 'root'; } diff --git a/App/Controllers/Home.php b/App/Controllers/Home.php index ec8710f..8199250 100644 --- a/App/Controllers/Home.php +++ b/App/Controllers/Home.php @@ -8,7 +8,8 @@ class Home public function index() { // echo "This is index of home"; // Nanti di replace sama twig view ke App\Views\Data\pengumuman.html - View::render('Data/pengumuman.html'); + $posts = new Posts(); + $posts->index(); return true; } diff --git a/App/Controllers/Posts.php b/App/Controllers/Posts.php index e1c22aa..80c2894 100644 --- a/App/Controllers/Posts.php +++ b/App/Controllers/Posts.php @@ -2,36 +2,51 @@ namespace App\Controllers; use \Core\View; +use App\Models\Post; class Posts { + private $model; + + public function __construct() + { + $this->model = new Post(); + } + public function index() { + $posts = $this->model->showAll(); // echo "This is index of posts."; // Nanti di replace sama twig view ke App\Views\Data\pengumuman.html - View::render('Data/pengumuman.html'); + View::render('Data/pengumuman.html', [ + 'posts' => $posts + ]); return true; } public function entry() { + $categories = $this->model->showCategories(); // echo "You can entry new data here."; // Nanti di replace sama twig view ke App\Views\Data\entry_pengumuman.html - View::render('Data/entry_pengumuman.html'); + View::render('Data/entry_pengumuman.html', [ + 'categories' => $categories + ]); return true; } public function edit($id = null) { if ($id) { + $posts = $this->model->showSingle($id); // echo "You can edit exists data with id $id here"; // Nanti di replace sama twig view ke App\Views\Data\edit_pengumuman.html View::render( 'Data/edit_pengumuman.html', [ - 'category' => 4, - 'content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.', - 'created_at' => '2017-05-10 10:00', - 'creator' => '5', - 'edited_at' => '2017-08-10 10:00', - 'editor' => '8' + 'category' => $posts['category'], + 'content' => $posts['content'], + 'created_at' => $posts['created_at'], + 'creator' => $posts['creator'], + 'edited_at' => $posts['edited_at'], + 'editor' => $posts['editor'] ] ); return true; diff --git a/App/Models/Post.php b/App/Models/Post.php index e5e0da5..608d77f 100644 --- a/App/Models/Post.php +++ b/App/Models/Post.php @@ -41,10 +41,8 @@ class Post extends \Core\Model if ($stmt = $db->query($sql)) { $result = $stmt->fetchAll(\PDO::FETCH_ASSOC); - // For tests - return true; + return $result; } - return false; } catch (PDOException $e) { echo $e->getMessage(); } @@ -62,11 +60,29 @@ class Post extends \Core\Model if ($query->execute([$id])) { if ($query->rowCount() === 1) { $result = $query->fetchAll(\PDO::FETCH_ASSOC); - // For tests - return true; + 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; } } - return false; } catch (PDOException $e) { echo $e->getMessage(); } diff --git a/App/Views/Data/entry_pengumuman.html b/App/Views/Data/entry_pengumuman.html index fdb4d35..f43c72b 100644 --- a/App/Views/Data/entry_pengumuman.html +++ b/App/Views/Data/entry_pengumuman.html @@ -8,10 +8,9 @@
diff --git a/App/Views/Data/pengumuman.html b/App/Views/Data/pengumuman.html index 16837af..9785ef5 100644 --- a/App/Views/Data/pengumuman.html +++ b/App/Views/Data/pengumuman.html @@ -5,17 +5,11 @@ {% block body %}

List Pengumuman

-

Pengumuman 1

-Edit -

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

- -

Pengumuman 1

-Edit -

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

- -

Pengumuman 1

-Edit -

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

+{% for post in posts %} +

Pengumuman {{ post.id }}

+Edit +

{{ post.content }}

+{% endfor %} + Tambah Pengumuman {% endblock %}