Receive data from database and pass to html using twig

This commit is contained in:
Gregorio Chiko Putra 2017-09-04 16:20:27 +07:00
parent aa60edbbf0
commit e1d70ed8f4
6 changed files with 57 additions and 32 deletions

View File

@ -4,8 +4,8 @@ namespace App;
class Config class Config
{ {
const const
DB_HOST = '127.0.0.1', DB_HOST = 'mariadb',
DB_DB = 'cfp_test', DB_DB = 'lepisi',
DB_UNAME = 'root', DB_UNAME = 'root',
DB_PWD = 'root'; DB_PWD = 'root';
} }

View File

@ -8,7 +8,8 @@ class Home
public function index() public function index()
{ {
// echo "This is index of home"; // Nanti di replace sama twig view ke App\Views\Data\pengumuman.html // 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; return true;
} }

View File

@ -2,36 +2,51 @@
namespace App\Controllers; namespace App\Controllers;
use \Core\View; use \Core\View;
use App\Models\Post;
class Posts class Posts
{ {
private $model;
public function __construct()
{
$this->model = new Post();
}
public function index() 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 // 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; return true;
} }
public function entry() 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 // 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; return true;
} }
public function edit($id = null) public function edit($id = null)
{ {
if ($id) { 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 // 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( View::render(
'Data/edit_pengumuman.html', 'Data/edit_pengumuman.html',
[ [
'category' => 4, 'category' => $posts['category'],
'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.', 'content' => $posts['content'],
'created_at' => '2017-05-10 10:00', 'created_at' => $posts['created_at'],
'creator' => '5', 'creator' => $posts['creator'],
'edited_at' => '2017-08-10 10:00', 'edited_at' => $posts['edited_at'],
'editor' => '8' 'editor' => $posts['editor']
] ]
); );
return true; return true;

View File

@ -41,10 +41,8 @@ class Post extends \Core\Model
if ($stmt = $db->query($sql)) { if ($stmt = $db->query($sql)) {
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC); $result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
// For tests return $result;
return true;
} }
return false;
} catch (PDOException $e) { } catch (PDOException $e) {
echo $e->getMessage(); echo $e->getMessage();
} }
@ -62,11 +60,29 @@ class Post extends \Core\Model
if ($query->execute([$id])) { if ($query->execute([$id])) {
if ($query->rowCount() === 1) { if ($query->rowCount() === 1) {
$result = $query->fetchAll(\PDO::FETCH_ASSOC); $result = $query->fetchAll(\PDO::FETCH_ASSOC);
// For tests return $result;
return true; }
}
} 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) { } catch (PDOException $e) {
echo $e->getMessage(); echo $e->getMessage();
} }

View File

@ -8,10 +8,9 @@
<label for="kategori">Kategori</label> <label for="kategori">Kategori</label>
<select name="kategori"> <select name="kategori">
<option value="kategori_1">Kategori 1</option> {% for cat in categories %}
<option value="kategori_2">Kategori 2</option> <option value="{{ cat.id }}">{{ cat.category }}</option>
<option value="kategori_3">Kategori 3</option> {% endfor %}
<option value="kategori_4">Kategori 4</option>
</select> </select>
<br> <br>

View File

@ -5,17 +5,11 @@
{% block body %} {% block body %}
<h2>List Pengumuman</h2> <h2>List Pengumuman</h2>
<h3>Pengumuman 1</h3> {% for post in posts %}
<code><a href="./posts/edit/1">Edit</a></code> <h3>Pengumuman {{ post.id }}</h3>
<p>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.</p> <a href="/posts/edit/{{ post.id }}">Edit</a>
<p>{{ post.content }}</p>
<h3>Pengumuman 1</h3> {% endfor %}
<code><a href="./posts/edit/2">Edit</a></code>
<p>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.</p>
<h3>Pengumuman 1</h3>
<code><a href="./posts/edit/3">Edit</a></code>
<p>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.</p>
<a href="./posts/entry">+ Tambah Pengumuman</a> <a href="./posts/entry">+ Tambah Pengumuman</a>
{% endblock %} {% endblock %}