tb_crud.sql
CREATE TABLE IF NOT EXISTS `tb_crud` (
`ID_crud` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(100) NOT NULL,
`content` text NOT NULL,
`modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`slug` varchar(100) NOT NULL,
PRIMARY KEY (`ID_crud`)
);
./application/config/autoload.php
$autoload['libraries'] = array('database', 'form_validation');
$autoload['helper'] = array('url', 'html');
./application/controllers/crud.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Crud extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$validation = array(
array('field' => 'title', 'rules' => 'required'),
array('field' => 'content', 'rules' => 'required')
);
$this->form_validation->set_rules($validation);
$this->form_validation->set_message('required', 'Tolong jangan kosong');
if ($this->form_validation->run() == true) {
$insert = array(
'title' => $this->input->post('title'),
'content' => $this->input->post('content'),
'modified' => date('Y-m-d H:i:s'),
'slug' => strtolower(url_title($this->input->post('title'))),
);
$this->db->insert('tb_crud', $insert);
redirect('crud');
}
/* insert value cookie */
$data['title'] = set_value('title');
$data['content'] = set_value('content');
/* end insert value cookie */
$data['result'] = $this->db->get('tb_crud')->result();
$this->load->view('crud', $data);
}
public function edit($ID_crud) {
$validation = array(
array('field' => 'title', 'rules' => 'required'),
array('field' => 'content', 'rules' => 'required')
);
$this->form_validation->set_rules($validation);
$this->form_validation->set_message('required', 'Tolong jangan kosong');
if ($this->form_validation->run() == true) {
$update = array(
'title' => $this->input->post('title'),
'content' => $this->input->post('content'),
'modified' => date('Y-m-d H:i:s'),
'slug' => strtolower(url_title($this->input->post('title'))),
);
$this->db->where('ID_crud', $ID_crud);
$this->db->update('tb_crud', $update);
redirect('crud');
}
// mengambil data dari database
$this->db->where('ID_crud', $ID_crud);
$update = $this->db->get('tb_crud')->row();
/* update value */
$data['title'] = $update->title;
$data['content'] = $update->content;
/* end update value */
$data['result'] = $this->db->get('tb_crud')->result();
$this->load->view('crud', $data);
}
function view($slug) {
/* /crud/view/$slug : '$this->uri->segment(3)' */
$this->db->where('slug', $slug);
$data['detail'] = $this->db->get('tb_crud')->row();
$this->load->view('detail_crud', $data);
}
function delete($ID_crud) {
$this->db->where('ID_crud', $ID_crud);
$this->db->delete('tb_crud');
redirect('crud');
}
}
/* End of file crud.php */
/* Location: ./application/controllers/crud.php */
./application/views/crud.php
// html form input dan edit
<?=form_open();?>
<table style="width:100%">
<tr>
<td>Title</td>
<td>
<?=form_input(array('name' => 'title', 'class' => 'form', 'value' => $title));?>
<?=form_error('title', '<div class="error">', '</div>'); ?>
</td>
</tr>
<tr>
<td valign="top">Content</td>
<td>
<?=form_textarea(array('name' => 'content', 'class' => 'form', 'value' => $content));?>
<?=form_error('content', '<div class="error">', '</div>');?>
<td>
</tr>
<tr>
<td> </td>
<td><?=form_submit('submit', 'Submit');?></td>
</tr>
</table>
<?=form_close();?>
// html view result
<table cellpadding="0" cellspacing="0" border="1" style="width:100%">
<tr>
<th align="left" width="10%">Title</th>
<th align="left" width="60%">Content</th>
<th align="left">Date</th>
<th align="left">Slug</th>
<th align="left">Options</th>
</tr>
<?php
foreach ($result as $data) {
echo '<tr>';
echo '<td>'.anchor('crud/view/'.$data->slug, $data->title.'</td>';
echo '<td>'.$data->content.'</td>';
echo '<td>'.$data->modified.'</td>';
echo '<td>'.$data->slug.'</td>';
echo '<td>'.anchor('crud/edit/'.$data->ID_crud, 'Edit').' | '.anchor('crud/delete/'.$data->ID_crud, 'Delete').'</td>';
echo '</tr>';
}
?>
</table>
./application/views/detail_crud.php
<table cellpadding="0" cellspacing="0" border="1" style="width:100%">
<tr>
<th align="left">Title</th>
<th align="left">Content</th>
<th align="left">Date</th>
</tr>
<?php
echo '<tr>';
echo '<td>'.$detail->title.'</td>';
echo '<td>'.$detail->content.'</td>';
echo '<td>'.$detail->modified.'</td>';
echo '</tr>';
?>
</table>
0 comments:
Post a Comment