Membuat Pencarian dengan CodeIgniter 3

Script searching/pencarian ini adalah porting dari script Pencarian Laravel 5, jadi databasenya diambil dari sana aja :).

Model

Buat model dengan nama Orang_model.php

<?php
class Orang_model extends CI_Model {
	public function __construct()
	{
		$this->load->database();
	}
	public function cariOrang()
	{
		$cari = $this->input->GET('cari', TRUE);
		$data = $this->db->query("SELECT * from dborang where Name like '%$cari%' ");
		return $data->result();
	}
 
 
}

Controller

Buat controller baru dengan nama Orang.php

<?php
class Orang extends CI_Controller {
 
	public function __construct()
	{
		parent::__construct();
		$this->load->helper("url");
		$this->load->model('orang_model');
	}
 
	public function cari() 
	{
		$this->load->view('search');
	}
 
	public function hasil()
	{
		$data2['cari'] = $this->orang_model->cariOrang();
		$this->load->view('result', $data2);
	}
}

View

Buat view dengan nama search.php

<!DOCTYPE html>
<html lang="">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>Pencarian dengan CodeIgniter 3 &raquo; Jaranguda.com</title>
		<link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
	</head>
	<body>
		<div class="container">
			<h3>Pencarian</h3>
			<hr>
			<form action="<?php echo base_url('index.php/orang/hasil')?>" action="GET">
				<div class="form-group">
					<label for="cari">data yang dicari</label>
					<input type="text" class="form-control" id="cari" name="cari" placeholder="cari">
				</div>
				<input class="btn btn-primary" type="submit" value="Cari">
			</form>
		</div>
	</body>
</html>

result.php

<!DOCTYPE html>
<html lang="">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>Pencarian dengan CodeIgniter 3 &raquo; Jaranguda.com</title>
		<link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
	</head>
	<body>
 
	<div class="container">
	<h3>Hasil Pencarian</h3>
	<hr>
 
		<?php
 
		if(count($cari)>0)
		{
			foreach ($cari as $data) {
			echo $data->Name . " => " . $data->Country ."<br>";
			}
		}
 
		else
		{
			echo "Data tidak ditemukan";
		}
 
		?>
 
	</div>
	</body>
</html>

Screenshot Web

pencarian codeigniter 3

tommy

Adi bekasta encari e, ja pe la lit renggetna.

One thought on “Membuat Pencarian dengan CodeIgniter 3

Leave a Reply

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