Posted in CodeIgniter
$autoload['helper'] = array('url');Tambahkan source code di user_controller.php menjadi seperti berikut: user_controller.php
<?php class User_controller extends CI_Controller{ function __Construct() { parent ::__construct(); } function user() { $this->load->model('user_model'); $data['judul'] = 'Delete Record Menggunakan Codeigniter'; $data['daftar_user'] = $this->user_model->get_user_all(); $this->load->view('daftar_user', $data); } function delete_user($id_user) { $this->load->model('user_model'); $username = $this->user_model->delete_user($id_user); redirect('user_controller/user'); } }
<?php class User_model extends CI_Model{ function get_user_all() { $query=$this->db->query("SELECT * FROM user ORDER BY id_user DESC"); return $query->result(); } function delete_user($id_user) { $query=$this->db->query("DELETE FROM user WHERE id_user='$id_user'"); } }
<html> <head> <title><?php echo $judul; ?></title> </head> <body> <h1>Daftar User</h1> <table border="1"> <thead> <tr> <th>Nama Lengkap</th> <th>Username</th> <th>Email</th> <th>Alamat</th> <th>Action</th> </tr> </thead> <tbody> <?php foreach($daftar_user as $user){ ?> <tr> <td><?php echo $user->nama_lengkap; ?></td> <td><?php echo $user->username; ?></td> <td><?php echo $user->email; ?></td> <td><?php echo $user->alamat; ?></td> <td><?php echo '<a href="'.base_url().'index.php/user_controller/delete_user/'.$user->id_user.'" onclick="return confirm(\'Anda yakin akan menghapus '.$user->username.'?\')">Delete</a>'?></td> </tr> <?php } ?> </tbody> <tfoot> <tr> <th>Nama Lengkap</th> <th>Username</th> <th>Email</th> <th>Alamat</th> <th>Action</th> </tr> </tfoot> </table> </body> </html>Cobalah untuk menjalankan aplikasi tersebut dengan URL http://localhost/codeigniter/index.php/user_controller/user Ketika anda klik delete, maka akan muncul peringatan yang jika di OK maka database akan dihapus dari dalam database. [caption id="" align="aligncenter" width="539"]