admin
by on June 7, 2020
1,779 views

CRUD merupakan singkatan dari create, read, update dan delete jika diterjemahkan dalam bahasa indonesia menjadi 4M yaitu Membuat, Membaca, Memperbaharui dan Menghapus. Dalam pembuatan CRUD  memerlukan akses database, untuk pembuatan dan pengaturan database silahkan tonton video yang telah disediakan.

Buat file controller di app/Controllers/Penjualan.php isi dengan skrip berikut

<?php namespace App\Controllers;   use CodeIgniter\Controller; use App\Models\PenjualanModel;   class Penjualan extends Controller {     public function __construct()     {         $this->callMdl = new PenjualanModel();     }       public function index()     {             $data['penjualan_item'] = $this->callMdl->orderBy('id', 'DESC')->findAll();         return view('penjualan/index', $data);     }           public function view($id = NULL)     {         $data['item'] = $this->callMdl->getPenjualan($id);         if (empty($data['item']))         {             throw new \CodeIgniter\Exceptions\PageNotFoundException('Data tidak ditemukan '. $id);         }         $data['title'] = $data['item']['judul'];         $data['keyword'] = $data['item']['judul'];         $data['description'] = '';         echo view('penjualan/view', $data);     }     public function tambah()     {             if($this->request->getVar('judul'))         {             helper(['form', 'url']);                           $userModel = new PenjualanModel();                          $data = [                 'judul' => $this->request->getVar('judul'),                 'deskripsi'  => $this->request->getVar('deskripsi'),                 ];             $this->callMdl->insert($data);             return redirect()->to( base_url('penjualan') );         } else {             return view('penjualan/tambah');                     }     }       public function ubah($id = null)     {         $data['penjualan'] = $this->callMdl->where('id', $id)->first();         if($this->request->getVar('judul'))         {             $data = [                 'judul' => $this->request->getVar('judul'),                 'deskripsi'  => $this->request->getVar('deskripsi'),             ];             $this->callMdl->getUpd($id, $data);             return redirect()->to(base_url('penjualan'));         }        return view('penjualan/ubah', $data);     }     public function hapus($id = null){         $this->callMdl->where('id', $id)->delete();         return redirect()->to( base_url('penjualan') );     } } ?>

 

Buat file model di app/Models/PenjualanModel.php isi dengan skrip berikut

<?php namespace App\Models; use CodeIgniter\Database\ConnectionInterface; use CodeIgniter\Model;   class PenjualanModel extends Model {     protected $table = 'penjualan';       protected $allowedFields = ['judul','deskripsi'];     public function getPenjualan($id = false)     {         if ($id === false)         {             return $this->findAll();         }         return $this->asArray()                     ->where(['id' => $id])                     ->first();     }     public function getUpd($id, $data)     {         return $this->db->table($this->table)->update($data, ['id' => $id]);     } } ?>

 

Buat file view untuk halaman index di app/Views/penjualan/index.php isi dengan skrip berikut

<!DOCTYPE html> <html> <head> <style> .container {     max-width: 500px;     margin: auto; } .tombol{   background-color:rgb(76,175,80);   color: white;   padding: 8px 15px;   border: none;   border-radius: 4px;   cursor: pointer;   float: right;   margin-bottom: 10px; } .tombol:hover {   background-color:rgb(69,160,73);   font-weight: bold; } .tbl-hapus {   background-color:rgb(255,128,128);   color: white;   padding: 8px 15px;   border: none;   border-radius: 4px;   cursor: pointer;   float: right;   margin-left: 1px;   width: 30px;   text-align: center; } .tbl-ubah {   background-color:rgb(102,153,255);   color: white;   padding: 8px 15px;   border: none;   border-radius: 4px;   cursor: pointer;   float: right;   margin-right: 1px;   width: 30px;   text-align: center; } table {   border-collapse: collapse;   width: 100%; } td {   text-align: left;   padding: 8px; } th {   background-color:rgb(76,175,80);   color: white;   text-align: left;   padding: 5px;   font-weight: bold; } tr:nth-child(even) {background-color:rgb(242,242,242);} </style> </head> <body> <div class="container"> <h2>Koleksi Buku</h2> <p>Koleksi buku yang tersedia di perpustakaan sampai dengan hari ini</p> <a href="<?php echo base_url('penjualan/tambah') ?>"><div class="tombol">Tambah</div></a> <table border="2" align="center">     <tr>         <th>JUDUL</th>         <th>KETERANGAN</th>         <th style="text-align: center;">AKSI</th>     </tr>     <?php      foreach($penjualan_item as $item){         ?>         <tr>             <td><a href="<?php echo base_url('penjualan/'.$item['id'].'/'.$item['judul']); ?>"><?php echo $item['judul']; ?></a></td>             <td><?php echo $item['deskripsi']; ?></td>             <td style="width: 127px;"><a  href="<?php echo base_url(); ?>/penjualan/hps/<?php echo $item['id']; ?>"><div class="tbl-hapus">Hapus</div></a>&nbsp;<a  href="<?php echo base_url(); ?>/penjualan/ubah/<?php echo $item['id']; ?>"><div class="tbl-ubah">Ubah</div></a></td>         </tr>         <?php     }     ?>  </table> </div> </body> </html>

 

Buat file view untuk halaman tambah di app/Views/penjualan/tambah.php isi dengan skrip berikut

<!DOCTYPE html> <html> <style> input[type=text], select {   width: 100%;   padding: 12px 20px;   margin: 8px 0;   display: inline-block;   border: 1px solid #ccc;   border-radius: 4px;   box-sizing: border-box; } input[type=submit] {   width: 100%;   background-color:rgb(76,175,80);   color: white;   padding: 14px 20px;   margin: 8px 0;   border: none;   border-radius: 4px;   cursor: pointer; } input[type=submit]:hover {   background-color:rgb(69,160,73); } .oke {   border-radius: 5px;   background-color:rgb(242,242,242);   padding: 20px;   max-width: 600px;   margin: auto; } </style> <body> <div class="oke"> <h3>Tambah Data</h3>   <form method="post" name="tambah" action="<?php echo base_url('penjualan/tambah');?>">     <label for="judul">Judul</label>     <input type="text" name="judul" placeholder="Judul..">     <label for="deskripsi">Deskripsi</label>     <input type="text" name="deskripsi" placeholder="Deskripsi..">       <input type="submit" value="Tambah">   </form> </div> </body> </html>

 

Buat file view untuk halaman ubah di app/Views/penjualan/ubah.php isi dengan skrip berikut

<!DOCTYPE html> <html> <style> .container {     max-width: 500px;     margin: auto; } input[type=text], select {   width: 100%;   padding: 12px 20px;   margin: 8px 0;   display: inline-block;   border: 1px solid #ccc;   border-radius: 4px;   box-sizing: border-box; } input[type=submit] {   width: 100%;   background-color:rgb(76,175,80);   color: white;   padding: 14px 20px;   margin: 8px 0;   border: none;   border-radius: 4px;   cursor: pointer; } input[type=submit]:hover {   background-color:rgb(69,160,73); } .oke {   border-radius: 5px;   background-color:rgb(242,242,242);   padding: 20px;   max-width: 600px;   margin: auto; } </style> <body> <div class="oke"> <h3>Tambah Data</h3>   <form method="post" name="ubah" action="<?php echo base_url('penjualan/ubah/'.$penjualan['id']);?>">     <label for="judul">Judul</label>     <input type="text" name="judul" value="<?php echo $penjualan['judul']; ?>">     <label for="deskripsi">Deskripsi</label>     <input type="text" name="deskripsi" value="<?php echo $penjualan['deskripsi']; ?>">       <input type="submit" value="Ubah">   </form> </div> </body> </html>

 

Buat file view untuk halaman view di app/Views/penjualan/view.php isi dengan skrip berikut

<!DOCTYPE html> <html> <body> <div style="max-width: 500px;margin: auto;">     <h3 style="text-align: center;"><?= esc($item['judul']); ?></h3>     <p><?= esc($item['deskripsi']); ?></p> </div> </body> </html>

 

Setelah selsesai membuat Controller, Model dan View maka selanjutnya adalah membuat route dengan cara buka file  Routes di app/Config/Routes.php tambahkan routenya seperti berikut :

$routes->get('penjualan/(:num)/(:segment)', 'Penjualan::view/$1'); $routes->get('penjualan', 'Penjualan::index');

 

Sampai disini telah berhasil membuat CRUD, untuk melihat bisa denganb mengakses
http://localhost/ci4/penjualan dengan tampilan seperti berikut :

Posted in: CodeIgniter 4
Be the first person to like this.