Ghufron Asadly. Diberdayakan oleh Blogger.

CRUD data Code Igniter 2.25

Kamis, 29 Oktober 2015

form tambah atau create
buat file dengan nama form_tambah php di views sebelumnya buat folder daftaragenda

view tambah
form.tambah.php
<h2>Form Tambah Agenda</h2>
<fieldset>
<form action="<?php echo site_url('daftaragenda/proses_tambah');?>" method="POST">
Nama : <br/><textarea name="nama" cols="50" rows="5"></textarea>
<br/><br/>
Keterangan : <br/><textarea name="keterangan" cols="50" rows="5"></textarea>
<br/><br/>
<input type="submit" value="Tambah" />
</form>
</fieldset>
</body>
</html>
membuat controller tambah

public function tambah_agenda(){
$this->load->view('daftaragenda/form_tambah');
}
public function proses_tambah(){
$data['nama'] = $this->input->post('nama');
$data['keterangan'] = $this->input->post('keterangan');
$this->agenda_model->insert_agenda($data);
redirect(site_url('daftaragenda'));
}
membuat model tambah
function insert_agenda($data){
$this->db->insert('agenda', $data);
}
Proses update/edit
membuat form edit dulu
<h2>Form Edit Agenda</h2>
<fieldset>
<form action="<?php echo site_url('daftaragenda/proses_edit_agenda');?>" method="POST">
Agenda : <br/><textarea name="nama" cols="50" rows="5"><?php echo $agenda->nama;?></textarea>
<br/><br/>
Keterangan : <br/><textarea name="keterangan" cols="50" rows="5"><?php echo $agenda->keterangan;?></textarea>
<br/><br/>
<input type="hidden" name="id_agenda" value="<?php echo $agenda->id_agenda;?>" />
<input type="submit" value="Ubah" />
</form>
</fieldset>
</body>
</html>
membuat controller edit dan view select 1 agenda
public function edit_agenda($id_agenda){
$data['agenda'] = $this->agenda_model->select_by_id($id_agenda)->row();
$this->load->view('daftaragenda/form_edit_agenda', $data);
}
public function proses_edit_agenda(){
$data['nama'] = $this->input->post('nama');
$data['keterangan'] = $this->input->post('keterangan');
$id_agenda=$this->input->post('id_agenda');
$this->agenda_model->update_agenda($id_agenda, $data);
redirect(site_url('daftaragenda'));
}
model edit dan select 1 data
function select_by_id($id_agenda){
$this->db->select('*');
$this->db->from('agenda');
$this->db->where('id_agenda', $id_agenda);
return $this->db->get();
}
function update_agenda($id_agenda, $data){
$this->db->where('id_agenda', $id_agenda);
$this->db->update('agenda', $data);
}
membuat delete
ini adalah view select all
<h2>Daftar Agenda</h2>
<a href="<?php echo site_url('daftaragenda/tambah_agenda');?>">Tambah Agenda</a>
<br />
<br />
<?php foreach ($daftar_agenda as $agenda) {?>
<fieldset>
<h3><?php echo $agenda->nama;?></h3>
<a href="<?php echo site_url('daftaragenda/edit_agenda/'.$agenda->id_agenda);?>">Edit</a> |
<a href="<?php echo site_url('daftaragenda/delete_agenda/'.$agenda->id_agenda);?>">Delete</a>
<br />
<p>
<?php echo $agenda->keterangan;?>
</p>
</fieldset>
<br />
<?php } ?>
</body>
controller delete
public function delete_agenda($id_agenda){
$this->agenda_model->delete_agenda($id_agenda);
redirect(site_url('daftaragenda'));
model delete
function delete_agenda($id_agenda){
$this->db->where('id_agenda', $id_agenda);
$this->db->delete('agenda');
}

Tidak ada komentar:

Posting Komentar

 

Most Reading