How to search record in mysql codeigniter

In this tutorial we are going to explain you how to perform CodeIgniter select query to fetch data from database using ID or name with form get method.

In this demo we are using input box and student id and student name. In input box you can enter an ID which you want to search and enter student name which you want to search. You may also like Live Search in php with Ajax and Mysql and How to Autocomplete Textbox Using jQuery,PHP and MySQL.

HTML Code

<div class="row">
 <div class="col-lg-12">
  <div class="ibox float-e-margins">
    <div class="ibox-title"><h5>Search Students</h5></div>
        <div class="ibox-content m-b-sm border-bottom">
          <form  class="" action="" method="get">
          <div class="row">
            <div class="col-sm-4">
              <div class="form-group">
                <label class="control-label" for="order_id">Student ID</label>
                <input name="student_id" value="<?=!empty($_GET['student_id'])?$_GET['student_id']:''?>" placeholder="Student ID" class="form-control" tabindex="1" type="text">
              </div>
            </div>
            <div class="col-sm-4">
              <div class="form-group">
                <label class="control-label" for="customer">Student Name</label>
                <input  name="student_name" value="<?=!empty($_GET['student_name'])?$_GET['student_name']:''?>" placeholder="Student Name" class="form-control" tabindex="2" type="text">
              </div>
            </div>
           </div>
           <button class="btn btn-info" type="submit" ><i class="fa fa-search"></i> Search</button>
           <a class="btn btn-white" href="<?=base_url()?>tutors/student/student_list" >Reset</a>
          </form>
        </div>
    </div>
  </div>
</div>

Controller Code

public function student_list() {
    $this->load->model ( 'tutor/tutor_student_model', '', TRUE );
    $data ['student_list'] = $this->tutor_student_model->get_student_list ();
    $this->load->view('tutor/student/student_list', $data);
}

Model Code

function get_student_list() {
    $this->db->select ( 'student_id,student_name,total_classes' );
    $this->db->from ( 'student' );
    $this->db->where ( 'student_status', 1 );
    if($this->input->get('student_id'))
    {
        $this->db->where ( 'student_id', $this->input->get('student_id') );
    }
    if($this->input->get('student_name'))
    {
        $this->db->like ( 'student_name', $this->input->get('student_name') );
    }
    $query = $this->db->get ();
    return $query->result ();
}

Leave a Reply

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