Membuat CRUD dengan Code Igniter

Setelah menginstall Codeigniter beberapa waktu yang lalu, kali ini kita akan belajar beberapa cara menggunakan dasar" Code Igniter, yaitu untuk fungsi Create, Read, Update, dan Delete atau CRUD. Mari kita langsung mulai!

Disini kita buat dulu file controller bernama crud.php untuk keempat fungsi tersebut, codenya akan terlihat seperti ini


<?php

class Crud extends CI_Controller{

  function __construct(){
    parent::__construct();
    $this->load->model('m_data');
    $this->load->helper('url');
  }

  function index(){
    $data['phonebook'] = $this->m_data->tampil_data()->result();
    $this->load->view('v_tampil', $data);
  }

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

  function tambah_aksi(){
    $nama_up = $this->input->post('nama_in');
    $alamat_up = $this->input->post('alamat_in');
    $email_up = $this->input->post('email_in');
    $hp_up = $this->input->post('hp_in');
    $kelas_up = $this->input->post('kelas_in');

    $data = array(
      'nama' => $nama_up,
      'alamat' => $alamat_up,
      'email' => $email_up,
      'hp' => $hp_up,
      'kelas' => $kelas_up
    );
    $this->m_data->input_data($data,'phonebook');
    redirect('http://localhost:56789/codeig/index.php/crud');
  }

  function hapus($id){
    $where = array('Id' => $id);
    $this->m_data->hapus_data($where,'phonebook');
    redirect('http://localhost:56789/codeig/index.php/crud');
  }

  function edit($id){
    $where = array('Id' => $id);
    $data['phonebook'] = $this->m_data->edit_data($where,'phonebook')->result();
    $this->load->view('v_edit',$data);
  }

  function update(){
       $id = $this->input->post('id');
       $nama = $this->input->post('nama_up');
       $alamat = $this->input->post('alamat_up');
       $email = $this->input->post('email_up');
     $hp = $this->input->post('hp_up');
     $kelas = $this->input->post('kelas_up');
        $data = array(
               'Nama' => $nama,
              'Alamat' => $alamat,
                'Email' => $email,
            'HP' => $hp,
            'Kelas' => $kelas
          );

            $where = array(
                  'Id' => $id
            );

             $this->m_data->update_data($where,$data,'phonebook');
            redirect('http://localhost:56789/codeig/index.php/crud');
  }
}

 ?>


Kemudian, baru kita buat file modelnya bernama m_data.php


<?php

class m_data extends CI_Model{

  function __construct(){
    parent::__construct();
    $this->load->database();
  }

  function tampil_data(){
    return $this->db->get('phonebook');
  }

  function input_data($data,$table){
    $this->db->insert($table,$data);
  }

  function hapus_data($where,$table){
    $this->db->where($where);
    $this->db->delete($table);
  }

  function edit_data($where,$table){
    return $this->db->get_where($table,$where);
  }

  function update_data($where,$data,$table){
    $this->db->where($where);
    $this->db->update($table,$data);
  }
}

?>
<?php

class m_data extends CI_Model{

  function __construct(){
    parent::__construct();
    $this->load->database();
  }

  function tampil_data(){
    return $this->db->get('phonebook');
  }

  function input_data($data,$table){
    $this->db->insert($table,$data);
  }

  function hapus_data($where,$table){
    $this->db->where($where);
    $this->db->delete($table);
  }

  function edit_data($where,$table){
    return $this->db->get_where($table,$where);
  }

  function update_data($where,$data,$table){
    $this->db->where($where);
    $this->db->update($table,$data);
  }
}

?>
<?php

class m_data extends CI_Model{

  function __construct(){
    parent::__construct();
    $this->load->database();
  }

  function tampil_data(){
    return $this->db->get('phonebook');
  }

  function input_data($data,$table){
    $this->db->insert($table,$data);
  }

  function hapus_data($where,$table){
    $this->db->where($where);
    $this->db->delete($table);
  }

  function edit_data($where,$table){
    return $this->db->get_where($table,$where);
  }

  function update_data($where,$data,$table){
    $this->db->where($where);
    $this->db->update($table,$data);
  }
}

?>

Lalu, baru kita buat file viewnya satu persatu. Pertama yaitu file v_tampil.php seperti dibawah


<!DOCTYPE html>
<html>
  <head>
    <title>| Coba Coba CRUD |</title>
  </head>
  <body>
    <center>
      <h1>| Coba Coba CRUD |</h1>
      <h3>View Data</h3>
    </center>
    <table style="margin:30px auto;" border="2">
      <tr>
        <th>No.</th>
        <th>Name</th>
        <th>Address</th>
        <th>Email</th>
        <th>HP</th>
        <th>Class</th>
        <th>Action</th>
      </tr>
      <?php
      $no = 1;
      foreach ($phonebook as $p) {
       ?>
       <tr>
         <td><?php echo $no++ ?></td>
         <td><?php echo $p->Nama ?></td>
         <td><?php echo $p->Alamat ?></td>
         <td><?php echo $p->Email ?></td>
         <td><?php echo $p->HP ?></td>
         <td><?php echo $p->Kelas ?></td>
         <td><?php echo anchor('http://localhost:56789/codeig/index.php/crud/edit/'.$p->Id, 'Edit'); ?>
           <?php echo anchor('http://localhost:56789/codeig/index.php/crud/hapus/'.$p->Id, 'Delete'); ?>
         </td>
       </tr>
       <?php }
        ?>
      </table>
      <center>
        <h4><?php echo anchor('http://localhost:56789/codeig/index.php/crud/tambah/','Insert Data'); ?></h4>
      </center>
    </body>
  </html>

Tampilannya akan seperti ini


Lalu kita buat file v_input.php untuk fungsi Create


<!DOCTYPE html>
<html>
<head>
    <title>| Coba Coba CRUD | </title>
</head>
<body>
    <center>
        <h1>| Coba Coba CRUD |</h1>
        <h3>Insert data</h3>
    </center>
    <form action="<?php echo 'http://localhost:56789/codeig/index.php/crud/tambah_aksi'; ?>" method="post">
        <table style="margin:20px auto;">
            <tr>
                <td>Name</td>
                <td><input type="text" name="nama_in"></td>
            </tr>
            <tr>
                <td>Address</td>
                <td><input type="text" name="alamat_in"></td>
            </tr>
            <tr>
                <td>Email</td>
                <td><input type="text" name="email_in"></td>
            </tr>
      <tr>
                <td>HP</td>
                <td><input type="text" name="hp_in"></td>
            </tr>
      <tr>
                <td>Class</td>
                <td><input type="text" name="kelas_in"></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Insert"></td>
            </tr>
        </table>
    </form>
</body>
</html>


Tampilannya akan menjadi seperti ini


Kemudian kita buat file v_edit.php untuk fungsi Update



<!DOCTYPE html>
<html>
  <head>
    <title>|Coba Coba CRUD|</title>
  </head>
  <body>
    <center>
      <h1>| Coba Coba CRUD |</h1>
      <h3> Update Data </h3>
    </center>
    <?php foreach($phonebook as $p) { ?>
      <form action="<?php echo 'http://localhost:56789/codeig/index.php/crud/update'; ?>" method="post">
        <table style="margin: 20px auto;">
          <tr>
            <td>Name</td>
            <td>
              <input type="hidden" name="id" value="<?php echo $p->Id ?>">
              <input type="text" name="nama_up" value="<?php echo $p->Nama ?>">
            </td>
          </tr>
          <tr>
            <td>Address</td>
            <td><input type="text" name="alamat_up" value="<?php echo $p->Alamat ?>"></td>
          </tr>
          <tr>
            <td>Email</td>
            <td><input type="text" name="email_up" value="<?php echo $p->Email ?>"></td>
          </tr>
          <tr>
            <td>HP</td>
            <td><input type="text" name="hp_up" value="<?php echo $p->HP ?>"></td>
          </tr>
          <tr>
            <td>Class</td>
            <td><input type="text" name="kelas_up" value="<?php echo $p->Kelas ?>"></td>
          </tr>
          <tr>
            <td></td>
            <td><input type="submit" value="Save"></td>
          </tr>
        </table>
      </form>
      <?php } ?>
    </body>
</html>


Tampilannya akan menjadi seperti berikut



Nah, untuk kali ini cukup segitu saja ilmu nya. Silahkan berbagi pada teman - teman anda. Sekian Terima kasih.  Byeee

Tidak ada komentar:

Posting Komentar