Вы находитесь на странице: 1из 7

Tutorial CRUD Code Igniter

Crud.php
<?php
class Crud extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->helper(array('url','form'));
$this->load->model('m_crud');
}
function lihat(){
$data['data_buku']=$this->m_crud->m_lihat();
$this->load->view('v_lihat',$data);
}

function tambah(){
$this->load->view('v_tambah');
}

function tambah_act(){
$data=array(
'judul' => $this->input->post('judul'),
'pengarang' => $this->input->post('pengarang'),
'kategori' => $this->input->post('kategori')
);
$this->m_crud->m_tambah_act($data);
redirect(base_url().'index.php/crud/lihat');
}

function hapus($id){
$data=array(
'id' => $id
);

$this->m_crud->m_hapus($data);
redirect(base_url().'index.php/crud/lihat');
}

function edit($id){
$data=array(
'id'=>$id
);
$data['data_edit']=$this->m_crud->m_edit($data);
$this->load->view('v_edit',$data);
}

function update(){
$id = $this->input->post('id');
$data=array(
'judul'=>$this->input->post('judul'),
'pengarang'=>$this->input->post('pengarang'),
'kategori'=>$this->input->post('kategori')
);
$this->m_crud->m_update($data,$id);
redirect(base_url().'index.php/crud/lihat');
}

?>

m_crud.php
<?php
class M_crud extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->database();
}
function m_lihat(){
$lihat = $this->db->get('buku');
return $lihat->result();

function m_tambah_act($data)
{
$this->db->insert('buku',$data);
}

function m_hapus($data){
$this->db->delete('buku',$data);
}

function m_edit($data){
$this->db->where($data);
$edit = $this->db->get('buku');
return $edit->result();
}

function m_update($data,$id){
$this->db->where('id', $id);
$this->db->update('buku',$data);
}
}
?>

v_edit.php
<!DOCTYPE html>
<html>
<head>
<title>CRUD</title>
</head>
<body>
<h1>EDIT BUKU</h1>
<?php foreach ($data_edit as $tes) {
?>
<form method="post" action="<?php echo base_url().'index.php/crud/update' ?>">
<table>
<tr>
<td><input type="hidden" name="id" value="<?php echo $tes->id ?>"></td>
</tr>
<tr>
<td>Judul</td><td><input type="text" name="judul" value="<?php echo $tes->judul ?>"></td>
</tr>
<tr>
<td>Pengarang</td><td><input type="text" name="pengarang" value="<?php echo $tes->pengarang ?
>"></td>
</tr>
<tr>
<td>Kategori</td><td><input type="text" name="kategori" value="<?php echo $tes->kategori ?
>"></td>
</tr>
<tr>
<td></td><td><input type="submit" value="Update" ></td>
</tr>
</table>
</form>
<?php
}
?>
</body></html>

v_lihat.php
<!DOCTYPE html>
<html>
<head>
<title>CRUD</title>
</head>
<body>
<h1>Data Buku</h1>
<table border="1">
<tr>
<th>judul</th>
<th>pengarang</th>
<th>kategori</th>
<th>Opsi</th>
</tr>
<?php foreach($data_buku as $lihat){
?>
<tr>
<td><?php echo $lihat->judul; ?></td>
<td><?php echo $lihat->pengarang; ?></td>
<td><?php echo $lihat->kategori; ?></td>
<td><?php echo anchor(base_url().'index.php/crud/hapus/'.$lihat->id,'hapus') ?>
<?php echo anchor(base_url().'index.php/crud/edit/'.$lihat->id,'edit') ?></td>
<td><?php echo anchor(base_url().'index.php/crud/tambah','tambah');?></td>
</tr>
<?php
}
?>

</table>
</body>
</html>

v_tambah.php
<!DOCTYPE html>
<html>
<head>
<title>CRUD</title>
</head>
<body>
<h1>Tambah BUKU</h1>
<form method="post" action="<?php echo base_url().'index.php/crud/tambah_act' ?>">
<table>
<tr>
<td>Judul</td><td><input type="text" name="judul"></td>
</tr>
<tr>
<td>Pengarang</td><td><input type="text" name="pengarang"></td>
</tr>
<tr>
<td>Kategori</td><td><input type="text" name="kategori"></td>
</tr>
<tr>
<td></td><td><input type="submit" value="input"></td>
</tr>
</table>
</form>
</body>
</html>

Buat Database
Basis data: `crud`
Struktur dari tabel `buku`
-CREATE TABLE IF NOT EXISTS `buku` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`judul` varchar(30) NOT NULL,
`pengarang` varchar(30) NOT NULL,
`kategori` varchar(30) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

Setting Database
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'crud';

Вам также может понравиться