Sunteți pe pagina 1din 5

PENCARIAN

<?php mysql_connect("localhost", "root", ""); mysql_select_db("contoh"); $bagianWhere = ""; if (isset($_POST['nimCat'])) { $nim = $_POST['nim']; if (empty($bagianWhere)) { $bagianWhere .= "nim = '$nim'"; } } if (isset($_POST['namaCat'])) { $namamhs = $_POST['nama']; if (empty($bagianWhere)) { $bagianWhere .= "namamhs LIKE '%$namamhs%'"; } else { $bagianWhere .= " AND namamhs LIKE '%$namamhs%'"; } } if (isset($_POST['alamatCat'])) { $alamat = $_POST['alamat']; if (empty($bagianWhere)) { $bagianWhere .= "alamat LIKE '%$alamat%'"; } else { $bagianWhere .= " AND alamat LIKE '%$alamat%'"; } } if (isset($_POST['sexCat'])) { $sex = $_POST['sex']; if (empty($bagianWhere)) { $bagianWhere .= "sex = '$sex'"; } else {

$bagianWhere .= " AND sex = '$sex'"; } } $query = "SELECT * FROM mhs WHERE ".$bagianWhere; $hasil = mysql_query($query); echo "<table border='1'>"; echo "<tr><td>NIM</td><td>Nama Mahasiswa</td><td>Alamat</td><td>Jenis Kelamin</td></tr>"; while ($data = mysql_fetch_array($hasil)) { echo "<tr><td>".$data['nim']."</td><td>".$data['namamhs']."</td><td>". $data['alamat']."</td><td>".$sex."</td></tr>"; } echo "</table>"; ?>

TAMPILAN
<h1>Cari Data Mahasiswa</h1> <p>Pilih kategori pencarian</p> <form method="post" action="proses.php"> <table> <tr><td><input type="checkbox" name="nimCat"> NIM</td><td><input type="text" name="nim"></td></tr> <tr><td><input type="checkbox" name="namaCat"> Nama Mahasiswa</td><td><input type="text" name="nama"></td></tr> <tr><td><input type="checkbox" name="alamatCat"> Alamat</td><td><input type="text" name="alamat"></td></tr> <tr><td><input type="checkbox" name="sexCat"> Jenis Kelamin</td><td><input type="radio" name="sex" value="L"> Laki-Laki <input type="radio" name="sex" value="P"> Perempuan</td></tr> <tr><td></td><td><input type="submit" name="submit" value="Submit"></td></tr> </table> </form>

LAIN LAGI CONTROLLER :


<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class search_paging extends CI_Controller { function __construct() { parent::__construct(); $this->load->helper(array('url')); $this->load->database();

$this->load->library(array('pagination','session')); $this->load->model('search_model'); } function index() { $this->load->view('search/depan'); } function cari() { $page=$this->uri->segment(3); $batas=5; if(!$page): $offset = 0; else: $offset = $page; endif; $data['nama']=""; $postkata = $this->input->post('nama'); if(!empty($postkata)) { $data['nama'] = $this->input->post('nama'); $this->session->set_userdata('pencarian_dosen', $data['nama']); } else { $data['nama'] = $this->session>userdata('pencarian_dosen'); } $data['nama_dosen'] = $this->search_model>cari_dosen($batas,$offset,$data['nama']); $tot_hal = $this->search_model>tot_hal('ja_mst_dosen','nama_dosen',$data['nama']); $config['base_url'] = base_url() . 'index.php/search_paging/cari/'; $config['total_rows'] = $tot_hal->num_rows(); $config['per_page'] = $batas; $config['uri_segment'] = 3; $config['first_link'] = 'Awal'; $config['last_link'] = 'Akhir'; $config['next_link'] = 'Selanjutnya'; $config['prev_link'] = 'Sebelumnya'; $this->pagination->initialize($config); $data["paginator"] =$this->pagination->create_links(); $this->load->view('search/hasil',$data); }

} /* End of file welcome.php */ /* Location: ./application/controllers/welcome.php */ MODEL <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class search_model extends CI_Model{ function __construct() { parent::__construct(); } function cari_dosen($limit,$offset,$nama) { $q = $this->db->query("select * from ja_mst_dosen where nama_dosen like '%$nama%' LIMIT $offset,$limit"); return $q; } function tot_hal($tabel,$field,$kata) { $q = $this->db->query("select * from $tabel where $field like '%$kata%'"); return $q; } } /* End of file welcome.php */ /* Location: ./application/controllers/welcome.php */ VIEW -DEPAN <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <title>Tutorial CodeIgniter : Membuat dan Menerapkan Paging Pada Searching di CodeIgniter</title> <style> body{ font-family:Tahoma; font-size:12px; } </style> <body> <form method="post" action="<?php echo base_url(); ? >index.php/search_paging/cari">

<table width="500" cellpadding="5" align="center"> <tr><td width="190">Masukkan Nama Dosen :</td><td><input type="text" name="nama" size="50" autocomplete="off" /></td></tr> <tr><td width="190"></td><td><input type="submit" value="Cari Dosen" /></td></tr> </table> </form> </body> -HASIL <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <title>Tutorial CodeIgniter : Membuat dan Menerapkan Paging Pada Searching di CodeIgniter</title> <style> body{ font-family:Tahoma; font-size:12px; } </style> <body> <table width="500" cellpadding="5" align="center"> <?php $banyak = count($nama_dosen->result_array()); echo '<tr><td>Ditemukan <b>'.$banyak.'</b> hasil pencarian dengan kata <b>"'.$nama.'"</b></td></tr>'; if(count($nama_dosen->result_array())>0) { foreach($nama_dosen->result_array() as $nd) { echo '<tr><td>&raquo; '. $nd['nama_dosen'].'</td></tr>'; } } else { echo '<tr><td>Tidak ditemukan dosen dengan nama <b>"'. $nama.'"</b></td></tr>'; } ?> <tr><td><?php echo $paginator; ?></td></tr> </table> </body>

S-ar putea să vă placă și