CodeIgniter Pagination with Ajax filters using gender, specialization and city

Here i put doctor’s name, specialization or location for searching and press continue for search.

enter image description here

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

enter image description here

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.

enter image description here

here is my search doctors, specializations or locations html form

<form action="site/doctors" method="post" role="form" class="form-home">
<div class="form-group">
<label for="search1">Enter Your Search Criteria:</label>
<input type="text" class="form-control" name="search_doctor" id="search_doctor" placeholder="Enter Doctor's Name, Speciality or Location">
 </div>
<button type="submit" class="btn btn-success pull-right">Continue &raquo;</button>
</form><!-- /Form -->

Doctors Controller

public function doctors($start=0)
{


        $this->load->model('Doctors_Model');
        //Doctors Specializations
        $data['specializations'] = $this->Doctors_Model->doctors_specializations();
        $data['cities'] = $this->Doctors_Model->cities();

        $data['doctors'] = $this->Doctors_Model->index(2,$start);

        //pagination
        $query = $this->input->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->db->query($sql);
        //echo $q->num_rows();
        //exit;

        $this->load->library('pagination');
        $config['base_url']=base_url("/site/doctors/");
        $config['total_rows']=$q->num_rows();
        $config['per_page']=2;
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['prev_link'] = '&lt;';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = '&gt;';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="current"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';
        $config['first_link'] = '&lt;&lt;';
        $config['last_link'] = '&gt;&gt;';
        $this->pagination->initialize($config);
        $data['pages']=$this->pagination->create_links();

        $data['site_title'] = 'Doctors';
        $data['main_content']='doctors';
        $this->load->view('includes/template',$data);


}//end doctors

filter Controller

public function doctors_filter(){



    //echo $this->uri->segment(3);
    $this->load->model('Doctors_Model');
    $search_query = $this->Doctors_Model->doctors_filtered();


    if($search_query){
        $html = '';

    foreach($search_query as $doctor){

        $html.='<div class="result"><div class="row">
                        <div class="col-md-2 col-sm-3 col-xs-3">
                        <a href="'. base_url('site/doctor/'.$doctor->username) .'">
                            <img alt="doctor's profile" src="'.base_url('doctordirectory-theme/images/doctors/maria.jpg').'" class="img-circle" />
                        </a>
                        </div>
                        <div class="col-md-6 col-sm-9 col-xs-9">
                            <ul class="list-unstyled doctors-info">
                                <li> <i class="fa fa-user-md"></i> '. ucwords($doctor->doctor).' | '.ucwords($doctor->specialist).' </li>
                                <li> <i class="fa fa-female"></i> '. ucwords($doctor->gender).' </li>
                                <li> <i class="fa fa-envelope"></i> '. $doctor->email .' </li>
                                <li> <i class="fa fa-map-marker"></i> '. ucwords($doctor->city).' </li>
                                <li> <i class="fa fa-home"></i> '. ucwords($doctor->address).' </li>
                            </ul>
                        </div>
                        <div class="col-md-4 col-sm-12 col-xs-12">
                            <ul class="list-unstyled heart-bg-area">
                                <li> <i class="fa fa-heart fa-4x"></i> </li>
                                <li> ( 10 ) </li>
                                <li> Click on the heart above to like this Doctor. </li>
                            </ul>
                        </div>
                    </div></div>';



    }
    echo $html;
    }else{ ?>

       <div class="result"> <div class="row">


            <div class="col-md-12">
            <p>No Records Found!</p>
            </div>


        </div><!-- /row--></div>
    <?php }


    }   

Doctors Model

public function index($num=2,$start=0)
{
 $query = $this->input->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->db->query($sql);
        return $q->result();

}

filters model

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->db->query($sql);
        if($q->num_rows()>0){
                foreach($q->result() as $row){
                    $data[] = $row;
                    }//end foreach
            return $data;
            }//endif



    }

AND THIS IS JQUERY / AJAX CODE

<script type="text/javascript">

    $(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 = '<?php echo site_url(); ?>';
            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 = '<?php echo site_url(); ?>';
            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)
                }
            });
        })
    });
</script>

Thanks


Source: ajax

Leave a Reply