Manipulasi Database CRUD dengan Code Igniter Framework

Persiapan pertama kali dalam praktek CRUD menggunakan Framework Code Igniter adalah tampilan awalnya mau seperti apa, dalam contoh ini saya menggunakan tampilan awal yaitu home, dimana halaman home tampil setelah berhasil login. untuk materi login anda bisa lihat pada materi Membuat Halaman Login dengan code igniter.
Untuk CRUD ini saya ambil contoh manipulasi data user yaitu merubah password dan menambah user serta menghapus user.
Pertama kali buka dan rubah file di application/views/vhome_view.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
   <title>Simple Login with CodeIgniter - Private Area</title>
 </head>
 <body>
   <h1>Home</h1>
   <h2>Welcome <?php echo $username; ?>!</h2> <a href="chome/logout">Logout</a>
   <h3>Menu</h3>
   <ul><li><a href="chome/viewusers">List</a></br></li>
    <li><a href="chome/addusers/">Input</a></li></ul>
 </body>
</html>
Tujuan file vhome_view.php adalah kita menyiapkan menu untuk akses ke list dan form ubah dan tambah user.
Setelah itu buka dan rubah file model di :  application/models/muser.php
<?php
Class Muser extends CI_Model
{
 function login($username, $password)
 {
   $this -> db -> select('id, username, password');
   $this -> db -> from('tuser');
   $this -> db -> where('username', $username);
   $this -> db -> where('password', MD5($password));
   $this -> db -> limit(1);
 
   $query = $this -> db -> get();
 
   if($query -> num_rows() == 1)
   {
     return $query->result();
   }
   else
   {
     return false;
   }
 }
 
    // get users all
 function get_all_users() {
 $query = $this->db->get('tuser');
 return $query->result(); }
 
   // get users by id
    function get_by_id($id){
  $query = $this->db->get_where('tuser',array('id'=>$id));
  return $query->result();
    }
    // add new users
    function save($username,$password){
  $data=array('username'=>$username,
                'password'=>md5($password));
        $this->db->insert('tuser', $data);
        return $this->db->insert_id();
    }
    // update users by id
    function update($id, $password){
  $data = array('password' => md5($password));
        $this->db->where('id', $id);
        $this->db->update('tuser', $data);
    }
    // delete users by id
    function delete($id){
        $this->db->where('id', $id);
        $this->db->delete('tuser');
    }
 
}
?>
Tujuan file model ini adalah untuk menempatkan fungsi perintah tambah, rubah, dan hapus suatu data pada table tertent.
Setelah itu buka dan rubah file application/controllers/chome.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Chome extends CI_Controller {
 
   function __construct()
   {
     parent::__construct();
     
     $this->load->model('muser');
   }
   
   function index()
   {
     $session_data =$this->session->userdata('logged_in');
   // echo $session_data['username'];
    //exit ;
     if($this->session->userdata('logged_in'))
     {
   
    $session_data = $this->session->userdata('logged_in');
    $data['username'] = $session_data['username'];
    $this->load->view('vhome_view', $data);
     }
     else
     {
   
    //If no session, redirect to login page
    redirect('clogin', 'refresh');
     }
   }
   
   function logout()
   {
     $this->session->unset_userdata('logged_in');
     session_destroy();
     redirect('chome', 'refresh');
   }
   
    function viewusers()
   {
   $data['user_list'] = $this->muser->get_all_users();
   $this->load->view('vhome_list', $data);
   }
   
     function addusers()
   {
   $this->load->view('vusers');
   }
   
   function edituser($id)
   {
   //$id = $this->uri->segment(3);
   $data['user_list'] = $this->muser->get_by_id($id);
   if ($data['user_list'] == null)
    {
    //Empty Result
    $data['user_list'] = $this->muser->get_all_users();
    $this->load->view('vhome_list', $data);
    }
    else
    {
    //print_r($data);
    $this->load->view('vusers', $data);
    }
   }
   
    function simpanedituser()
    {
    //print_r($_POST);
    $this->muser->update($_POST['Id'],$_POST['password']);
    redirect('chome'); 
    }
    
    
    function simpanadduser()
    {
    $this->muser->save($_POST['username'],$_POST['password']);
    redirect('chome');  
    }
    
   function deleteuser()
   {
   $this->muser->delete($this->uri->segment(3));
   redirect('chome');
   }
 
}
 
?>
Setelah control ini terbentuk, kita sudah mendapatkan control yang nanti nya akan berfungsi memanggil list, menambah user baru, merubah user yang ada serta menghapus user.
Sekarang saatnya membuat list data untuk menampilkan data yang nantinya akan ada perintah edit dan delete. Buatlah file di application/views/vhome_list.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CI CRUD</title>
</head>
<script type="text/javascript">

 function show_confirm(act,gotoid)
 {
  if(act=="edituser")
   var r=confirm("Do you really want to edit?");
  else
   var r=confirm("Do you really want to delete?");
   if (r==true)
 {
  window.location="<?php echo base_url();?>index.php/chome/"+act+"/"+gotoid;
 }
}
</script>
<body>

<h2> Simple CI CRUD Application </h2>
<table width="600" border="1" cellpadding="5">
<tr>
<th scope="col">Id</th>
<th scope="col">User Name</th>
<th scope="col">password</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
<?php foreach ($user_list as $datauser){ ?>
<tr>
<td><?php echo $datauser->id; ?></td>
<td><?php echo $datauser->username; ?></td>
<td><?php echo $datauser->password; ?></td>
<td width="40" align="left" ><a href="#" onClick="show_confirm('edituser',<?php echo $datauser->id;?>)">Edit</a></td>

<td width="40" align="left" ><a href="#" onClick="show_confirm('deleteuser',<?php echo $datauser->id;?>)">Delete </a></td>
</tr>
<?php }?>

</table>
<a href="<?php echo base_url().'index.php/chome'; ?>"> Menu </a>   </br>
<?php echo anchor('chome/addusers/','add new data',array('class'=>'add')); ?>
</body>
</html>
Nantinya tampilan Listnya kira-kira akan seperti ini:

Selanjutnya Buat File untuk Form ubah dan tambah di application/views/vusers.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CI Insert Form</title>
</head>
<body>
<?php if($this->uri->segment(2)=='addusers'){ ?>
<form method="post" action="<?php echo base_url();?>index.php/chome/simpanadduser">
<?php } else { ?>
<form method="post" action="<?php echo base_url();?>index.php/chome/simpanedituser">
 <?php 
} ?>
<h2>DATA USER</h2>
<a href="<?php echo base_url().'index.php/chome'; ?>"> Menu </a>   </br>
<hr>
<table width="400" border="0" cellpadding="5">

<?php 
 if (ISSET($user_list)) {
  foreach ($user_list as $userdata) { ?>
 
    <tr>
    <th width="213" align="right" scope="row">Username</th>

    <td width="161">
    <input type="hidden" name="Id" value="<?php echo $userdata->id;  ?>" />
    <input type="text" name="username" id="username"  value="<?php echo $userdata->username;  ?>" size="15" maxlength="15" readonly ></td>

    </tr>

    <tr>

    <th align="right" scope="row">Password</th>
    <td><input type="password" name="password" id ="password"  placeholder="password" size="15" maxlength="15" required/></td>
    </tr>


    <tr>
    <th align="right" scope="row">&nbsp;</th>
    <td><input type="submit" name="submit" value="Submit" /></td>
    </tr>

   <?php

   }
    
 }
 else
 { ?>
  <tr>
    <th width="213" align="right" scope="row">Username</th>

    <td width="161">
    
    <input type="text" name="username" id="username"  placeholder="isi username"  value="" size="15" maxlength="15" required ></td>

    </tr>

    <tr>

    <th align="right" scope="row">Password</th>
    <td><input type="password" name="password" id ="password"  placeholder="isi password" size="15" maxlength="15" required/></td>
    </tr>


    <tr>
    <th align="right" scope="row">&nbsp;</th>
    <td><input type="submit" name="submit" value="Submit" /></td>
    </tr>
    <?php
 }
 ?>
</table>
</form>
</body>
</html>
 Sampai disini sudah selesai, saat ini jika anda menambah data baru, merubah, dan menghapus maka akan ke redirect ke halaman home.
Manipulasi Database CRUD dengan Code Igniter Framework
Item Reviewed: Manipulasi Database CRUD dengan Code Igniter Framework 9 out of 10 based on 10 ratings. 9 user reviews.
Emoticon? nyengir

Berkomentarlah dengan Bahasa yang Relevan dan Sopan.. #okemasbro! ^_^

Komentar Terbaru

Just load it!