title: CodeIgniter Pagination with Ajax filters using gender, specialization and city
body: <p>Here i put doctor's name, specialization or location for searching and press continue for search.</p>
  
  <p><img src="https://i.stack.imgur.com/3bTO6.jpg" alt="enter image description here"></p>
  
  <p>Then i comes here now you can see search records with pagination. This pagination is working fine and specializations, gender and city filters also works fine but issue is that</p>
  
  <p><img src="https://i.stack.imgur.com/MDnA5.jpg" alt="enter image description here"></p>
  
  <p>when i am filtering result using these filters result showing perfectly accurate but pagination now working according to filters. This is what i want to know how to solve it.</p>
  
  <p><img src="https://i.stack.imgur.com/abem1.jpg" alt="enter image description here"></p>
  
  <p>here is my search doctors, specializations or locations <strong>html form</strong></p>
  
  <pre><code>&lt;form action="site/doctors" method="post" role="form" class="form-home"&gt;
  &lt;div class="form-group"&gt;
  &lt;label for="search1"&gt;Enter Your Search Criteria:&lt;/label&gt;
  &lt;input type="text" class="form-control" name="search_doctor" id="search_doctor" placeholder="Enter Doctor's Name, Speciality or Location"&gt;
   &lt;/div&gt;
  &lt;button type="submit" class="btn btn-success pull-right"&gt;Continue &amp;raquo;&lt;/button&gt;
  &lt;/form&gt;&lt;!-- /Form --&gt;
  </code></pre>
  
  <p><strong>Doctors Controller</strong></p>
  
  <pre><code>public function doctors($start=0)
  {
  
  
          $this-&gt;load-&gt;model('Doctors_Model');
          //Doctors Specializations
          $data['specializations'] = $this-&gt;Doctors_Model-&gt;doctors_specializations();
          $data['cities'] = $this-&gt;Doctors_Model-&gt;cities();
  
          $data['doctors'] = $this-&gt;Doctors_Model-&gt;index(2,$start);
  
          //pagination
          $query = $this-&gt;input-&gt;post('search_doctor');
          $sql = "SELECT doctors.id,doctors.doctor,doctors.gender,doctors.username,doctors.contactno,doctors.email,doctors.city,doctors.address,
          doctors.country,doctors.profile,
          specialist.specialist
          from doctors_specialists_bridge
          LEFT JOIN doctori4_PakDoctors.doctors 
          ON (doctors.id = doctors_specialists_bridge.doctorid)
          LEFT JOIN doctori4_PakDoctors.specialist 
          ON (specialist.id = doctors_specialists_bridge.specialistid)
          WHERE doctors.doctor LIKE '%$query%' OR specialist.specialist LIKE '%$query%'
          ";          
          $q = $this-&gt;db-&gt;query($sql);
          //echo $q-&gt;num_rows();
          //exit;
  
          $this-&gt;load-&gt;library('pagination');
          $config['base_url']=base_url("/site/doctors/");
          $config['total_rows']=$q-&gt;num_rows();
          $config['per_page']=2;
          $config['full_tag_open'] = '&lt;ul class="pagination"&gt;';
          $config['full_tag_close'] = '&lt;/ul&gt;';
          $config['prev_link'] = '&amp;lt;';
          $config['prev_tag_open'] = '&lt;li&gt;';
          $config['prev_tag_close'] = '&lt;/li&gt;';
          $config['next_link'] = '&amp;gt;';
          $config['next_tag_open'] = '&lt;li&gt;';
          $config['next_tag_close'] = '&lt;/li&gt;';
          $config['cur_tag_open'] = '&lt;li class="current"&gt;&lt;a href="#"&gt;';
          $config['cur_tag_close'] = '&lt;/a&gt;&lt;/li&gt;';
          $config['num_tag_open'] = '&lt;li&gt;';
          $config['num_tag_close'] = '&lt;/li&gt;';
          $config['first_tag_open'] = '&lt;li&gt;';
          $config['first_tag_close'] = '&lt;/li&gt;';
          $config['last_tag_open'] = '&lt;li&gt;';
          $config['last_tag_close'] = '&lt;/li&gt;';
          $config['first_link'] = '&amp;lt;&amp;lt;';
          $config['last_link'] = '&amp;gt;&amp;gt;';
          $this-&gt;pagination-&gt;initialize($config);
          $data['pages']=$this-&gt;pagination-&gt;create_links();
  
          $data['site_title'] = 'Doctors';
          $data['main_content']='doctors';
          $this-&gt;load-&gt;view('includes/template',$data);
  
  
  }//end doctors
  </code></pre>
  
  <p><strong>filter Controller</strong></p>
  
  <pre><code>public function doctors_filter(){
  
  
  
      //echo $this-&gt;uri-&gt;segment(3);
      $this-&gt;load-&gt;model('Doctors_Model');
      $search_query = $this-&gt;Doctors_Model-&gt;doctors_filtered();
  
  
      if($search_query){
          $html = '';
  
      foreach($search_query as $doctor){
  
          $html.='&lt;div class="result"&gt;&lt;div class="row"&gt;
                          &lt;div class="col-md-2 col-sm-3 col-xs-3"&gt;
                          &lt;a href="'. base_url('site/doctor/'.$doctor-&gt;username) .'"&gt;
                              &lt;img alt="doctor\'s profile" src="'.base_url('doctordirectory-theme/images/doctors/maria.jpg').'" class="img-circle" /&gt;
                          &lt;/a&gt;
                          &lt;/div&gt;
                          &lt;div class="col-md-6 col-sm-9 col-xs-9"&gt;
                              &lt;ul class="list-unstyled doctors-info"&gt;
                                  &lt;li&gt; &lt;i class="fa fa-user-md"&gt;&lt;/i&gt; '. ucwords($doctor-&gt;doctor).' | '.ucwords($doctor-&gt;specialist).' &lt;/li&gt;
                                  &lt;li&gt; &lt;i class="fa fa-female"&gt;&lt;/i&gt; '. ucwords($doctor-&gt;gender).' &lt;/li&gt;
                                  &lt;li&gt; &lt;i class="fa fa-envelope"&gt;&lt;/i&gt; '. $doctor-&gt;email .' &lt;/li&gt;
                                  &lt;li&gt; &lt;i class="fa fa-map-marker"&gt;&lt;/i&gt; '. ucwords($doctor-&gt;city).' &lt;/li&gt;
                                  &lt;li&gt; &lt;i class="fa fa-home"&gt;&lt;/i&gt; '. ucwords($doctor-&gt;address).' &lt;/li&gt;
                              &lt;/ul&gt;
                          &lt;/div&gt;
                          &lt;div class="col-md-4 col-sm-12 col-xs-12"&gt;
                              &lt;ul class="list-unstyled heart-bg-area"&gt;
                                  &lt;li&gt; &lt;i class="fa fa-heart fa-4x"&gt;&lt;/i&gt; &lt;/li&gt;
                                  &lt;li&gt; ( 10 ) &lt;/li&gt;
                                  &lt;li&gt; Click on the heart above to like this Doctor. &lt;/li&gt;
                              &lt;/ul&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;&lt;/div&gt;';
  
  
  
      }
      echo $html;
      }else{ ?&gt;
  
         &lt;div class="result"&gt; &lt;div class="row"&gt;
  
  
              &lt;div class="col-md-12"&gt;
              &lt;p&gt;No Records Found!&lt;/p&gt;
              &lt;/div&gt;
  
  
          &lt;/div&gt;&lt;!-- /row--&gt;&lt;/div&gt;
      &lt;?php }
  
  
      }   
  </code></pre>
  
  <p><strong>Doctors Model</strong></p>
  
  <pre><code>public function index($num=2,$start=0)
  {
   $query = $this-&gt;input-&gt;post('search_doctor');
   $sql = "SELECT doctors.id,doctors.doctor,doctors.gender,doctors.username,doctors.contactno,doctors.email,doctors.city,doctors.address,
          doctors.country,doctors.profile,
          specialist.specialist
          from doctors_specialists_bridge
          LEFT JOIN doctori4_PakDoctors.doctors 
          ON (doctors.id = doctors_specialists_bridge.doctorid)
          LEFT JOIN doctori4_PakDoctors.specialist 
          ON (specialist.id = doctors_specialists_bridge.specialistid)
          WHERE doctors.doctor LIKE '%$query%' OR specialist.specialist LIKE '%$query%'
          LIMIT $start,$num";         
          $q = $this-&gt;db-&gt;query($sql);
          return $q-&gt;result();
  
  }
  </code></pre>
  
  <p><strong>filters model</strong></p>
  
  <pre><code>public function doctors_filtered(){
  
  
          $gender = $_REQUEST['gender'];
          $city = $_REQUEST['city'];
          $check = isset($_REQUEST['check']);
  
               $sql = "SELECT doctors.id,doctors.doctor,doctors.gender,doctors.username,doctors.contactno,doctors.email,doctors.city,doctors.address,
                      doctors.country,doctors.profile,
                      specialist.specialist
                      FROM doctors_specialists_bridge
                      INNER JOIN doctori4_PakDoctors.doctors 
                      ON (doctors.id = doctors_specialists_bridge.doctorid)
                      INNER JOIN doctori4_PakDoctors.specialist 
                      ON (specialist.id = doctors_specialists_bridge.specialistid)";
  
  
  
  
                          $ddd=0;
                          if($gender!="Any" || $city!="Any" || $check!='')
                          {
                              $sql.="WHERE ";
                          }
  
                          if($check!=''){
                              $sql.=" ( ";
                              foreach ($_REQUEST['check'] as $value)
                                  {
  
  
  
                                      if($ddd==1)
                                      {
                                          $sql.=" OR ";
                                      }
                                      $ddd=1;
                                      $sql.="specialist='".$value."' ";
  
                                  }
                                  $sql.=" ) ";
  
  
                          }
  
  
                          if($gender!="Any"){
                              if($ddd==1)
                              {
                                  $sql.=" AND ";
                              }
                          $sql.="gender='".$gender."'";
                          $ddd=1;
                          }
  
  
  
                          if($city!="Any"){ 
                              if($ddd==1)
                              {
                                  $sql.=" AND ";
                              }
                              $sql.="city='".$city."'"; 
                              $ddd=1;
                          } 
  
  
  
          $q = $this-&gt;db-&gt;query($sql);
          if($q-&gt;num_rows()&gt;0){
                  foreach($q-&gt;result() as $row){
                      $data[] = $row;
                      }//end foreach
              return $data;
              }//endif
  
  
  
      }
  </code></pre>
  
  <p><strong>AND THIS IS JQUERY / AJAX CODE</strong></p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
      $(document).ready(function () {
  
          $( "input[type=checkbox]" ).on( 'click', function(){
  
  
  
  
              //console.log($(this).serialize());
  
              //var values = $('#form1').serialize();
              var values = $('form').serialize();
  
              var controller = 'site';
              var base_url = '&lt;?php echo site_url(); ?&gt;';
              var  myurl = base_url + controller + '/doctors_filter/';
  
  
              $.ajax({
                  type: "POST",
                  url: myurl,
                  data: values,
                  complete: function (data) {
                      //alert(data.responseText);
                      $('#result').empty().html(data.responseText)
                      //console.log(data.responseText);
                  }
              });
  
  
          } );
  
  
          $('#gender,#city').on('change', function () {
              //var values = $('#form1').serialize();
              var values = $('form').serialize();
  
              var controller = 'site';
              var base_url = '&lt;?php echo site_url(); ?&gt;';
              var  myurl = base_url + controller + '/doctors_filter/';
  
              //console.log(myurl);
  
              $.ajax({
                  type: "POST",
                  url: myurl,
                  data: values,
                  complete: function (data) {
                     //alert(data.responseText);
                      $('#result').empty().html(data.responseText)
                  }
              });
          })
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>Thanks</p>