Start by creating a controller that will handle the search queries and display the search page, followed by the search term passed to the model to search for the database (and send it back to the controller). The controller will pass it to the view.
A small example:
Controller
class Search extends CI_Controller { public function __construct() { parent::__construct(); $this->load->helper('form'); $this->load->model('search_model'); } public function index() { $this->load->view('search_form'); } public function execute_search() {
Model
class Search_model extends CI_Model { public function get_results($search_term='default') { // Use the Active Record class for safer queries. $this->db->select('*'); $this->db->from('members'); $this->db->like('username',$search_term); // Execute the query. $query = $this->db->get(); // Return the results. return $query->result_array(); } }
View to display the search form
<?php echo form_open('search/execute_search'); echo form_input(array('name'=>'search')); echo form_submit('search_submit','Submit'); ?>
View to display results
<div> <?php // List up all results. foreach ($results as $val) { echo $val['username']; } ?> </div>
source share