Web Programming : Meet 6 (Menampilkan Detail Data dan Pencarian)

Meet 6 – Menampilkan Detail Data dan Pencarian

Download disini

Buat Sebuah Controller dengan nama Mahasiswa.php (dalam folder application/controllers)

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

class Mahasiswa extends CI_Controller {

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

  public function index()
  {
    $data = array(
      'judul'	=> 'Daftar Nama Mahasiswa',
      'jurusan' => 'Informatics Computer',
      'cari'	=> '',
      'mhs'	=> $this->Mhs_model->data(),
        );

    $this->load->view('data_mhs',$data);	
  }

  public function penggunaan_where($id){

    $mhs = $this->Mhs_model->data_where($id);

    if($mhs){
    $data_where = array(
      'judul' => 'Daftar Nama Mahasiswa (Menggunakan Where ID)',
      'jurusan' => 'Informatics Computer',
      'nim' => $mhs->nim,
      'nama' => $mhs->nama,
      'jur' => $mhs->jurusan,
    );
      $this->load->view('data_mhs_where',$data_where);
    }else{
      redirect('mahasiswa');
    }
  }

  public function pencarian(){

    $cari = urldecode($this->input->get('cari', TRUE));

    $data = array(
      'judul'	=> 'Daftar Nama Mahasiswa (Pencarian untuk Kata '.$cari.' )',
      'jurusan' => 'Informatics Computer',
      'cari'	=> $cari,
      'mhs'	=> $this->Mhs_model->data_pencarian($cari),
        );

    $this->load->view('data_mhs',$data);		
  }

}

Kemudian Buat Sebuah Model dengan nama Mhs_model (dalam folder application/models)

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

class Mhs_model extends CI_Model
{

    function __construct()
    {
        parent::__construct();
    }

    function data(){
        return $this->db->get('data_mhs')->result();
    } 


    function data_where($id){
      $this->db->select('*');
      $this->db->where('id',$id);
      $this->db->from('data_mhs');
      return $this->db->get()->row();
    }

    function data_pencarian($cari){
      $this->db->select('*');
      $this->db->like('nim',$cari);
      $this->db->or_like('nama',$cari);
      $this->db->or_like('jurusan',$cari);
      $this->db->from('data_mhs');
        return $this->db->get()->result();

    }

}

Buat 2 buah View dalam folder application/views :

  • buat view dengan nama data_mhs.php
<!doctype html>
<html>
  <head>
    <title>Data Mahasiswa</title>
  </head>
<body>
  <h1>### CONTOH MENAMPILKAN DATA ###</h1><hr>
  <h1><?php echo $judul;?></h1>
  Kolom Pencarian
  <form action="<?php echo base_url('mahasiswa/pencarian?')?>" method="get">
    <input type="text" value="<?php echo $cari;?>" name="cari">
    <button type="submit">Cari</button>
    <a href="<?php echo base_url('mahasiswa')?>">Kembali</a>
  </form>
  <table width="100%" border="1">
    <tr>
      <th>No</th>
      <th>NIM</th>
      <th>Nama</th>
      <th>Jurusan</th>
      <th>Aksi</th>
    </tr>
    <?php 
      $nomor = 1;
      foreach($mhs as $mhs){ ?>
    <tr>
      <td><?php echo $nomor++;?></td>
      <td><?php echo $mhs->nim;?></td>
      <td><?php echo $mhs->nama;?></td>
      <td><?php echo $mhs->jurusan;?></td>
      <td><a href="<?php echo base_url('mahasiswa/penggunaan_where/'.$mhs->id)?>">Lihat Data</a></td>
    </tr>
    <?php } ?>
  </table>
</body>
</html>
  • buat view dengan nama data_mhs_where.php
<!doctype html>
<html>
  <head>
    <title>Data Mahasiswa</title>
  </head>
<body>
  <h1>### CONTOH MENAMPILKAN DATA ###</h1><hr>
  <h1><?php echo $judul;?></h1>
  <table width="50%" border="1">
    <tr>
      <td>NIM</td>
      <td>:</td>
      <td><?php echo $nim;?></td>
    </tr>
    <tr>
      <td>NAMA</td>
      <td>:</td>
      <td><?php echo $nama;?></td>
    </tr>
    <tr>
      <td>JURUSAN</td>
      <td>:</td>
      <td><?php echo $jur;?></td>
    </tr>
  </table>
  <a href="<?php echo base_url('mahasiswa');?>">Kembali</a>
</body>
</html>

 

*** Selamat Mencoba ***

 

Recommended For You

About the Author: adyrhs

Leave a Reply

Your email address will not be published. Required fields are marked *