Anda di halaman 1dari 4

CRUD CODEIGNITER DAN MYSQL

UNTUK KELAS XI RPL [SMK SALAFIYAH]

2019 – SM II

1. Membuat database dengan nama crud dan table dengan nama siswa

2. Meng – Extract CodeIgniter di dalam htdocs , lalu ganti nama folder menjadi crud.
Selanjutnya cek di browser : localhost/crud

3. Mengkonfigurasi CodeIgniter
a. Buka file Application/config.php
$config['base_url'] = 'http://localhost/crud/';
b. Buka file Application/database.php
'username' => 'root',
'password' => '',
'database' => 'crud',
c. Buka file Application/autoload.php
$autoload['libraries'] = array(‘database’,’session’);
$autoload['helper'] = array(‘url’);
4. Membuat Controller dengan nama SiswaCtr.php simpan di
Application/Controllers

<?php
class SiswaCtr extends CI_Controller{
function __construct(){
parent::__construct();
}
}
?>

5. Membuat View dengan nama TambahSiswa.php simpan di Application/Views

<form action="<?php echo base_url()?>index.php/SiswaCtr/TambahSiswa" method="post">


<table>
<tr>
<td>NIS</td>
<td><input type="text" name="nis"></td>
</tr>
<tr>
<td>NAMA SISWA</td>
<td><input type="text" name="nama"></td>
</tr>
<tr>
<td>JENIS KELAMIN</td>
<td><input type="text" name="gender"></td>
</tr>
<tr>
<td>ALAMAT</td>
<td><input type="text" name="alamat"></td>
</tr>
<tr>
<td>KOTA LAHIR</td>
<td><input type="text" name="kotalhr"></td>
</tr>
<tr>
<td>TANGGAL LAHIR</td>
<td><input type="text" name="tgllhr"></td>
</tr>
</table>
</form>

6. Me-Load TambahSiswa pada SiswaCtr


function index(){
$this->load->view("TambahSiswa");
}
7. Membuat Model dengan nama CrudModel.php simpan di Application/Model
<?php
class CrudModel extends CI_Model{

function tambah($tbl,$data){
$this->db->insert($tbl,$data);
}

}
?>

8. Membuat fungsi TambahSiswa pada SiswaCtr dan Menampung data dari form
menjadi array

function TambahSiswa(){
$data=array(
'nis'=>$this->input->post('nis'),
'nama'=>$this->input->post('nama'),
'gender'=>$this->input->post('gender'),
'alamat'=>$this->input->post('alamat'),
'kotalhr'=>$this->input->post('kotalhr'),
'tgllhr'=>$this->input->post('tgllhr')
);
}

9. Me-Load Model CrudModel pada SiswaCtr __construct()


function __construct(){
parent::__construct();
$this->load->Model("CrudModel");
}

10. Me-Load method tambah pada fungsi TambahSiswa


function TambahSiswa(){
$data=array(
'nis'=>$this->input->post('nis'),
'nama'=>$this->input->post('nama'),
'gender'=>$this->input->post('gender'),
'alamat'=>$this->input->post('alamat'),
'kotalhr'=>$this->input->post('kotalhr'),
'tgllhr'=>$this->input->post('tgllhr')
);

$this->CrudModel->tambah("siswa",$data);

}
11. Uji coba menambahkan data , Lalu cek pada database.

12. To be continued … …

Anda mungkin juga menyukai