ok, langsung saja, saya beranggapan bahwa dikomputer anda sudah terpasang local web server. pada komputer saya terinstal paket instalasi XAMPP yang di dalamnya ada apache, mysql, php dll. sehingga saya tidak harus menginstal nya satu persatu. jika anda belum memiliki file instalan nya. anda bisa mendownloadnya pada alamat berikut : https://www.apachefriends.org/download.html silahkan anda sesuaikan paket yang akan ada download.
- Cara melakukan instalasi untuk xampp sangat lah mudah, anda cukup mengikuti pentunjuk istalasi saja, maka itu sudah cukup.
- kemudian langkah selanjutnya, silahkan anda akses alamat : localhost/phpmyadmin pada browser anda.
- Kemudian setelah muncul jendela phpmyadmin, anda klik tombol "database" kemudian anda isi kolom database name nya "db_kampus"

Membuat Database - kemudian kita akan buat table "tb_mahasiswa", silahakan anda tuliskan query dibawah ini pada SQL editor phpmyadmin
- "
- CREATE TABLE IF NOT EXISTS `tb_mahasiswa` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `nim` varchar(15) NOT NULL,
- `nama` varchar(25) NOT NULL,
- `angakatan` varchar(5) NOT NULL,
- `jurusan` varchar(20) NOT NULL,
- PRIMARY KEY (`id`)
- ) "
- Sampai disini database sudah siap. kemudian kita akan lanjut pada codeigniter.
- Lakukan instalasi codeigniter, jika anda belum memiliki file codeigniter ataupun belum memahami bagaimana cara melakukan instalasi dan konfigurasu codeigniter, silahkan anda kunjungi artikel saya yang berjudul instalasi setting awal codeigniter di artikel itu dijelaskan bagaimana cara melakukan instalasi dan juga konfigurasinya codeigniter
- Saya anggap anda sudah paham tentang bagaimana cara melakukan instalasi codeigniter.
- Setelah anda mengcopy folder codeigniter dan mempastenya di web server (htdocs/www), rename folder codeigniter menjadi kampus
- Langsung saja buka file config.php dan lakukan perubahan pada :
- $config['base_url'] = ''; menjadi $config['base_url'] = 'http://localhost/kampus';
- $config['encryption_key'] = ''; menjadi $config['encryption_key'] = '[sesuai dengan yang anda inginkan]';
- Setelah itu lakukan perubahan juga pada file database.php, lakukan perubahan pada :
- $db['default']['hostname'] = 'localhost'; *jika memakai localhost
- $db['default']['username'] = '[sesuaikan dengan username anda]';
- $db['default']['password'] = '[sesuaikan password anda]';
- $db['default']['database'] = 'db_kampus';
- $db['default']['dbdriver'] = 'mysql';
- Setelah itu, lakukan perubahan juga pada file autoload.php
- $autoload['libraries'] = array('database','form_validation');
- $autoload['helper'] = array('url','form');
- Sampai disini konfigurasi sudah cukup.
- Sekarang kita buat controller, buat file baru di folder kampus/application/controllers/ dan beri nama c_kampus.php dan ketikan sitak berikut :
- <?php Class C_kampus extends CI_Controller
- {
- function __construct()
- {
- parent::__construct();
- $this->load->model('m_kampus');
- }
- function index()
- {
- $list_mahasiswa = $this->m_kampus->m_list_siswa();
- $data = array('list_mahasiswa'=>$list_mahasiswa);
- $this->load->view('v_kampus.html',$data);
- }
- function c_simpan_data()
- {
- $nim = $this->input->post('nim');
- $nama = $this->input->post('nama');
- $angkatan = $this->input->post('angkatan');
- $jurusan = $this->input->post('jurusan');
- $this->m_kampus->m_simpan_data($nim,$nama,$angkatan,$jurusan);
- redirect('c_kampus','location');
- }
- function c_tampil_edit()
- {
- $row_mhs = $this->m_kampus->m_row_id_siswa($this->uri->segment(3,0));
- $data = array('row_mhs'=>$row_mhs);
- $this->load->view('v_kampus_edit.html',$data);
- }
- function c_simpan_edit()
- {
- $this->load->library('form_validation');
- $this->form_validation->set_rules('nim', 'NIM', 'required');
- $this->form_validation->set_rules('nama', 'Nama', 'required');
- $this->form_validation->set_rules('jurusan', 'Jurusan Confirmation', 'required');
- $this->form_validation->set_rules('angkatan', 'Angkatan', 'required|numeric');
- if ($this->form_validation->run() == FALSE)
- {
- $row_mhs = $this->m_kampus->m_row_id_siswa($this->uri->segment(3,0));
- $data = array('row_mhs'=>$row_mhs);
- $this->load->view('v_kampus_edit.html',$data);
- }
- else
- {
- $id = $this->uri->segment(3,0);
- $nim = $this->input->post('nim');
- $nama = $this->input->post('nama');
- $angkatan = $this->input->post('angkatan');
- $jurusan = $this->input->post('jurusan');
- $this->m_kampus->m_edit_mhs($id,$nim,$nama,$jurusan,$angkatan);
- $list_mahasiswa = $this->m_kampus->m_list_siswa();
- $data = array('list_mahasiswa'=>$list_mahasiswa);
- $this->load->view('v_kampus.html',$data);
- }
- }
- function c_hapus_data()
- {
- $this->m_kampus->m_hapus_data($this->uri->segment(3,0));
- redirect('c_kampus','location');
- }
- }
- /* End of file c_kampus.php */
- /* Location: ./application/controllers/c_kampus.php */
- Kemudian anda buat file baru untuk menjadi view. Buat file baru di folder kampus/application/view/ dan beri nama v_kampus.html dan ketikan sitak berikut :
- <!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>CRUD Dengan CI</title>
- <script>
- function hapusBarang(pesan)
- {
- if (confirm(pesan))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- </script>
- </head>
- <body>
- <div id="container">
- <div id="form_input">
- <?php
- echo '<table>';
- echo form_open('c_kampus/c_simpan_data');
- $data = array(
- 'name' => 'nim',
- 'class' => 'input',
- 'placeholder' => 'isi dengan angka'
- );
- echo '<tr>';
- echo'<td>NIM :</td>';
- echo '<td>';
- echo form_input($data);
- echo '</td>';
- echo '</tr>';
- $data = array(
- 'name' => 'nama',
- 'class' => 'input',
- 'placeholder' => 'isi nama lengkap'
- );
- echo '<tr>';
- echo'<td>Nama :</td>';
- echo '<td>';
- echo form_input($data);
- echo '</td>';
- echo '</tr>';
- $data = array(
- 'name' => 'angkatan',
- 'class' => 'input',
- 'placeholder' => 'isi dengan angka'
- );
- echo '<tr>';
- echo'<td>Angkatan :</td>';
- echo '<td>';
- echo form_input($data);
- echo '</td>';
- echo '</tr>';
- $data = array(
- 'name' => 'jurusan',
- 'class' => 'input',
- 'placeholder' => 'isi jurusan lengkap'
- );
- echo '<tr>';
- echo'<td>Jurusan :</td>';
- echo '<td>';
- echo form_input($data);
- echo '</td>';
- echo '</tr>';
- echo '<tr>';
- echo '<td colspan="2">';
- echo form_submit('mysubmit', 'Submit Post!');
- echo '</td>';
- echo '</tr>';
- echo form_close();
- echo '</table>';
- ?>
- </div>
- <div id="list_data">
- <table>
- <thead>
- <tr>
- <th>NO</th>
- <th>NIM</th>
- <th>Nama Mahasiswa</th>
- <th>Jurusan</th>
- <th>Angkatan</th>
- <th>Aksi</th>
- </tr>
- </thead>
- <tbody>
- <?php
- if(!empty($list_mahasiswa))
- {
- $list_result = $list_mahasiswa->result();
- $no =1;
- foreach($list_result as $row)
- {
- echo'<tr>';
- echo'<td>'.$no.'</td>';
- echo'<td>'.$row->nim.'</td>';
- echo'<td>'.$row->nama.'</td>';
- echo'<td>'.$row->jurusan.'</td>';
- echo'<td>'.$row->angkatan.'</td>';
- echo'<td> <a class="edit" href="'.base_url().'index.php/c_kampus/c_tampil_edit/'.$row->id.'" onclick="return hapusBarang(\'Data Barang '.$row->nama.' Yakin mau Dirubah ? \')"> Edit </a> | <a href="'.base_url().'index.php/c_kampus/c_hapus_data/'.$row->id.'" class="delete" onclick="return hapusBarang(\'Data Barang '.$row->nama.' Yakin mau dihapus ? \')"> Hapus </a> </td>';
- echo'</tr>';
- $no += 1;
- }
- }
- ?>
- </tbody>
- </table>
- </div>
- </div>
- </body>
- </html>
- Kemudian anda buat file baru untuk menjadi view ketika akan melakukan perubahan data. Buat file baru di folder kampus/application/view/ dan beri nama v_kampus_edit.html dan ketikan sitak berikut :
- <!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>Untitled Document</title>
- </head>
- <body>
- <div id="container">
- <div id="form_input">
- <?php
- echo '<table>';
- echo form_open('c_kampus/c_simpan_edit/'.$row_mhs->id);
- $data = array(
- 'name' => 'nim',
- 'class' => 'input',
- 'placeholder' => 'isi dengan angka',
- 'value' => $row_mhs->nim
- );
- echo '<tr>';
- echo'<td>NIM :</td>';
- echo '<td>';
- echo form_input($data);
- echo form_error('nim');
- echo '</td>';
- echo '</tr>';
- $data = array(
- 'name' => 'nama',
- 'class' => 'input',
- 'placeholder' => 'isi nama lengkap',
- 'value' => $row_mhs->nama
- );
- echo '<tr>';
- echo'<td>Nama :</td>';
- echo '<td>';
- echo form_input($data);
- echo form_error('nama');
- echo '</td>';
- echo '</tr>';
- $data = array(
- 'name' => 'angkatan',
- 'class' => 'input',
- 'placeholder' => 'isi dengan angka',
- 'value' => $row_mhs->angkatan
- );
- echo '<tr>';
- echo'<td>Angkatan :</td>';
- echo '<td>';
- echo form_input($data);
- echo form_error('angkatan');
- echo '</td>';
- echo '</tr>';
- $data = array(
- 'name' => 'jurusan',
- 'class' => 'input',
- 'placeholder' => 'isi jurusan lengkap',
- 'value' => $row_mhs->jurusan
- );
- echo '<tr>';
- echo'<td>Jurusan :</td>';
- echo '<td>';
- echo form_input($data);
- echo form_error('jurusan');
- echo '</td>';
- echo '</tr>';
- echo '<tr>';
- echo '<td colspan="2">';
- echo form_submit('mysubmit', 'Submit Post!');
- echo '</td>';
- echo '</tr>';
- echo form_close();
- echo '</table>';
- ?>
- </div>
- </div>
- </body>
- </html>
- Kemudian anda buat file baru untuk menjadi model yang akan menyediakan data untuk ditampilkan. Buat file baru di folder kampus/application/models/ dan beri nama m_kampus.php dan ketikan sitak berikut :
- <?php
- Class M_kampus extends CI_Model
- {
- function __construct()
- {
- parent::__construct();
- }
- function m_list_siswa()
- {
- $query = $this->db->query('SELECT * FROM tb_mahasiswa ORDER BY nim');
- if($query->num_rows() > 0)
- {
- return $query;
- }
- else
- {
- return false;
- }
- }
- function m_row_id_siswa($id)
- {
- $query = $this->db->query('SELECT * FROM tb_mahasiswa WHERE id="'.$id.'" ORDER BY nim');
- if($query->num_rows() > 0)
- {
- return $query->row();
- }
- else
- {
- return false;
- }
- }
- function m_simpan_data($nim,$nama,$angkatan,$jurusan)
- {
- $query = $this->db->query('INSERT INTO tb_mahasiswa (nim,nama,angkatan,jurusan) VALUES ("'.$nim.'","'.$nama.'","'.$angkatan.'","'.$jurusan.'")');
- }
- function m_edit_mhs($id,$nim,$nama,$jurusan,$angkatan)
- {
- $query = $this->db->query('UPDATE tb_mahasiswa SET nim = "'.$nim.'",nama = "'.$nama.'",jurusan = "'.$jurusan.'",angkatan = "'.$angkatan.'" WHERE id="'.$id.'"');
- }
- function m_hapus_data($id)
- {
- $query = $this->db->query('DELETE FROM tb_mahasiswa WHERE id = "'.$id.'";');
- }
- }
- /* End of file m_kampus.php */
- /* Location: ./application/models/m_kampus.php */
- Anda cukup melakukan copy paste saja, aplikasi sudah bisa dijalankan. Berikut saya sertakan gambar hasil nya :

Hasil Jalannya Program
Tidak ada komentar:
Posting Komentar