Skip to content

Instantly share code, notes, and snippets.

@purwandi
Last active December 19, 2015 04:39
Show Gist options
  • Save purwandi/5899078 to your computer and use it in GitHub Desktop.
Save purwandi/5899078 to your computer and use it in GitHub Desktop.
<?php
// File ini ada di application/views/add.php
;?>
<h1>Add Post</h1>
<form action="" method="POST" id="postForm">
<input type="text" name="title" />
<textarea name="body"></textarea>
<button type="submit">Simpan</button>
</form
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="http://jquery.bassistance.de/validate/jquery.validate.js"></script>
<script type="text/javascript">
<?php
// Cek Form validation menggunakan http://jquery.bassistance.de/validate/demo/
// validate signup form on keyup and submit
?>
$(document).ready(function(){
$("#postForm").validate({
rules: {
title: "required",
body: "required",
},
messages: {
title: "Please enter your title",
body: "Please enter your body"
}
});
});
</script>
<?php
// File ini ada di application/views/edit.php
;?>
<h1>Add Post</h1>
<form action="" method="POST">
<input type="text" name="title" value="<?php echo $post->title;?>" />
<textarea name="body"><?php echo $post->body;?></textarea>
<button type="submit">Simpan</button>
</form
<?php
// File ini ada di application/views/index.php
;?>
<h1>Post</h1>
<?php if ($posts):?>
<?php foreach($posts as $post):?>
<h1><?php echo $post->title;?></h1>
<p><?php echo $post->body;?></p>
<a href="/post/edit/<?php echo $post->id;?>">Edit</a>
<a href="/post/delete/<?php echo $post->id;?>">Delete</a>
<?php endforeach;?>
<?php else:?>
Belum ada post
<?php endif;?>
<?php
// File ini ada di application/controllers/post.php
class Post extends CI_Controller
{
public function index()
{
$this->load->model('post_model');
$data['posts'] = $this->post_model->getAll();
return $this->load->view('index', $data);
}
public function add()
{
if ($_POST){
$this->load->library('form_validation');
$this->form_validation->set_rules('title','Judul','required');
$this->form_validation->set_rules('body','Isi post','required');
if ($this->form_validation->run() == TRUE) {
$this->db->insert('tb_posts', array(
'title' => $this->input->post('title'),
'body' => $this->input->post('body')
));
}
}
return $this->load->view('add');
}
public function edit($id)
{
$this->load->model('post_model');
if ($_POST) {
$this->load->library('form_validation');
$this->form_validation->set_rules('title','Judul','required');
$this->form_validation->set_rules('body','Isi post','required');
if ($this->form_validation->run() == TRUE) {
$this->db->update('tb_posts', array(
'title' => $this->input->post('title'),
'body' => $this->input->post('body')
), array(
'id' => $id
));
}
}
$data['post'] = $this->post_model->getById($id);
return $this->load->view('edit', $data);
}
public function delete($id)
{
$this->db->where('id', $id);
return $this->db->delete('tb_posts');
}
}
<?php
// File ini ada di application/controllers/post_model.php
class Post_Model extend CI_Model
{
public function getAll()
{
$this->db->where('status', 1);
$this->db->from('tb_posts');
return $this->db->get()->result();
}
public function getById($id)
{
$this->db->where('status', 1);
$this->db->where('id', $id);
$this->db->from('tb_posts');
return $this->db->get()->row();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment