Langsung ke konten utama

Insert, Update, Delete Dengan CodeIgniter

langkah petama adalah Download dulu Ci di Website CodeIgniter
Langkah selanjutnya Simpan di  HTDOCS

Silahkan di buat dulu databasenya












1. Koneksi Ci dengan MySql
\application\config\database.php 


<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '123';
$db['default']['database'] = 'web_fi';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;


*Hanya Tulisan Yg bewarna Yg Perlukan Untuk koneksi

2.Pengaturan Router
\application\config\router.php 

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$route['default_controller'] = "registrasi/entry";
$route['404_override'] = '';


/* End of file routes.php */

/* Location: ./application/config/routes.php */

*registrasi -> adalah nama file control
*entry -> adalah nama fungsi dari class registrasi

3.Controllers Registrasi
Simpan File berikut ini dengan nama regristrasi.php dan simpan di  \application\controllers

<?php
 if(!defined('BASEPATH')) exit('Keluar dari sistem');
 
 class Registrasi extends CI_Controller
 {
  public function __construct()
  {
  parent::__construct();
 
  //load helpernya
  $this->load->helper('form');
  //load model
    $this->load->library('form_validation');
    $this->load->model('Model_mhs');

  }
 

 public function entry(){
    $data['action']="registrasi/simpan";

$data['conten']="form_registrasi";
$this->load->view('home',$data);

 }



 public function simpan(){

 $data = array(
   'nobp'=>$this->input->Post('nobp'),
   'nama'=>$this->input->Post('nama'),
   'alamat'=>$this->input->Post('alamat'),
   'agama'=>$this->input->Post('agama'),
   'jurusan'=>$this->input->Post('jurusan'),
   );
    $rules=array(
        array(
          'field'=>'nobp',
          'rules'=>'numeric|required'
        )
      );

    $this->form_validation->set_rules($rules);
    if($this->form_validation->run()==false){
      $data['psn']="NoBp Harus Angka";
       $data['conten']="form_registrasi";
       $this->load->view('home',$data);
    }
    else{
     $this->Model_mhs->add_data_mhs($data);
    }
}

 public function laporan(){
    $data['data_mhs']=$this->Model_mhs->data_mhs();
     $data['conten']="laporan";
        $this->load->view('home',$data);
}
 public function t_update($bp){
    $this->db->where('nobp',$bp);
    $ambil=$this->Model_mhs->data_mhs();
    foreach ($ambil->result_array() as $row)
    $data['action']="registrasi/s_update";
    $data['psn']="";
    $data['nobp']=$row['nobp'];
    $data['nama']=$row['nama'];
    $data['agama']=$row['agama'];
    $data['alamat']=$row['alamat'];
    $data['conten']="form_registrasi";
  $this->load->view('home',$data);
}

 public function s_update(){
   $data = array(
   'nobp'=>$this->input->Post('nobp'),
   'nama'=>$this->input->Post('nama'),
   'alamat'=>$this->input->Post('alamat'),
   'agama'=>$this->input->Post('agama'),
   'jurusan'=>$this->input->Post('jurusan'),
   );

  $update=array('nama'=>$data['nama'],'agama'=>$data['agama'],'alamat'=>$data['alamat'],'jurusan'=>$data['jurusan']);
  $this->db->where('nobp',$data['nobp']);
  $this->db->update('mhs',$update);

  $data['data_mhs']=$this->Model_mhs->data_mhs();
  $data['conten']="laporan";
  $this->load->view('home',$data);

 }

  function delete($bp){
    $this->db->where('nobp',$bp);
    $this->db->delete("mhs");
  $data['data_mhs']=$this->Model_mhs->data_mhs();
    $data['conten']="laporan";
  $this->load->view('home',$data);

  }
    function p(){
   $data['conten']="p";
  $this->load->view('home',$data);


  }
      function l(){
   $data['conten']="l";
  $this->load->view('home',$data);


  }
}

?>


4.Models
Simpan dengan nama model_mhs.php di \application\models

<?php
class Model_mhs extends CI_Model{
function __construct(){
parent::__construct();

  $this->load->database();

  //load helper
  $this->load->helper('url');
 }



 //add data ke database
 function add_data_mhs($data){
  $this->db->insert('mhs', $data);
  redirect('registrasi/laporan');
 }

  function data_mhs(){
$query=$this->db->get('mhs');
return $query;

}

function update($set){
$this->db->set($set);
$this->db->update('mhs');

}

}
?>

5 View
Simpan di \aplication\view

form_registrasi.php
<?php error_reporting(0);
?>
 <html>
  <head>
  <title>Form Registrasi DIMASEDU INSTITUE </title>


  <h2>Registrasi Mahasiswa</h2>
  <?php
  //deklarasikan awal form

  echo form_open($action);
  $this->load->helper('form');

 ?>
  <table>
 <tr>
  <td>Nobp</td>
  <td>:</td>
  <td><?php echo form_input('nobp',$nobp)?></td>
  </tr>
 <tr>
  <td>Nama Lengkap</td>
  <td>:</td>
  <td><?php echo form_input('nama',$nama)?></td>
  </tr>
  <tr>
  <td>Alamat Lengkap</td>
  <td>:</td>
   <td><?php
        $dt=array(
          'name'=>'alamat',
          'rows'=>'3',
          'cols'=>'30',
          'value'=>$alamat
          );
        echo form_textarea($dt);
       ?>
 </td>
  </tr>
  <tr>
  <td>Agama</td>
  <td>:</td>
  <td><?php
  $option=array(
              'islam'=>'Islam',
              'kristen'=>'kristen',
              'budha'=>'Budha'
              );

  echo form_dropdown('agama',$option,'islam');?></td>
  </tr>
  <tr>
 <tr>
 <td>Jurusan</td>
  <td>:</td>
  <td><?php
  $option=array(
              'SI'=>'Sistem Informasi',
              'SK'=>'Sistem Komputer'
              );

  echo form_dropdown('jurusan',$option,'SI');?></td>
 </tr>
  <tr>
  <td></td>
  <td></td>
  <td><?php
echo form_submit('submit','Simpan'); echo  $psn;
  ?></td>
  </tr>
 </table>
 <?php
 //deklarasikan akhir form
  form_close();
  ?>
  </body>
</html>












laporan.php

<?php
echo anchor('http://localhost/lat_CI/index.php/registrasi/entry','<button> Tambah </button>');
echo "<table border='1' width='600px' cellspacing='0' cellpadding='2'>";
echo "<tr bgcolor='#F6F6F6'><td>Nim</td><td>Nama</td><td>Agama</td><td>Jurusan</td><td>Alamat</td><td>Edit</td><td>Hapus</td></tr>";

foreach ($data_mhs->result_array() as $row){
echo "<tr>";
echo "<td>".$row['nobp']."</td>";
echo "<td>".$row['nama']."</td>";
echo "<td>".$row['agama']."</td>";
echo "<td>".$row['jurusan']."</td>";
echo "<td>".$row['alamat']."</td>";
echo "<td>".anchor("registrasi/t_update/".$row['nobp'],"Edit")."</td>";
echo " <td>".anchor("registrasi/delete/".$row['nobp'],"Delete")."</td>";
echo "</tr>";
}
echo "</table>";





Komentar

Postingan populer dari blog ini

T-Code (SAP)

Sebuah kode transaksi (atau t-code ) terdiri dari huruf, angka, atau keduanya, dan dimasukkan di bidang perintah di bagian atas layar SAP. Setiap fungsi dalam SAP ERP memiliki sebuah kode transaksi SAP .

Cara Membuat Format Nomor Halaman Yang Berbeda Dalam Dokumen Yang Sama

Baiklah berikut langkah-langkah membuatnya agar dalam satu dokumen bisa dibuat sekaligus walaupun format penomorannya berbeda-beda.