Shihas
Shihas

Reputation: 814

Pagination Issue in CodeIgniter

I'm facing an issue with the pagination in my system. Pagination is working fine here.

enter image description here

But if I'm searching something in that text field and if the result is more than 5 ($config['per_page'] = 5;) its not showing the pagination buttons.

enter image description here

And actually, I have 6 results for the search keyword 'shihas'.

And also I can see the 6th result once I go to www.example.com/path/2 .

enter image description here

But still, the pagination link is not there. Please tell me a solution for this issue.
Controller:

public function index()
    {

        //all the posts sent by the view       
        $search_string = $this->input->post('search_string');        
        $order = $this->input->post('order'); 
        $order_type = $this->input->post('order_type'); 

        //pagination settings
        $config['per_page'] = 5;
        $config['base_url'] = base_url().'admin/posts';
        $config['use_page_numbers'] = TRUE;
        $config['num_links'] = 20;       
        $config['full_tag_open'] = '<ul>';
        $config['full_tag_close'] = '</ul>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active"><a>';
        $config['cur_tag_close'] = '</a></li>';

        //limit end
        $page = $this->uri->segment(3);

        //math to get the initial record to be select in the database
        $limit_end = ($page * $config['per_page']) - $config['per_page'];
        if ($limit_end < 0){
            $limit_end = 0;
        } 

        //if order type was changed
        if($order_type){
            $filter_session_data['order_type'] = $order_type;
        }
        else{
            //we have something stored in the session? 
            if($this->session->userdata('order_type')){
                $order_type = $this->session->userdata('order_type');    
            }else{
                //if we have nothing inside session, so it's the default "Asc"
                $order_type = 'Asc';    
            }
        }
        //make the data type var avaible to our view
        $data['order_type_selected'] = $order_type;        


        //we must avoid a page reload with the previous session data
        //if any filter post was sent, then it's the first time we load the content
        //in this case, we clean the session filter data
        //if any filter post was sent but we are on some page, we must load the session data

        //filtered && || paginated
        if($search_string !== false && $order !== false || $this->uri->segment(3) == true){ 

            /*
            The comments here are the same for line 79 until 99

            if the post is not null, we store it in session data array
            if is null, we use the session data already stored
            we save order into the var to load the view with the param already selected       
            */

            if($search_string){
                $filter_session_data['search_string_selected'] = $search_string;
            }else{
                $search_string = $this->session->userdata('search_string_selected');
            }
            $data['search_string_selected'] = $search_string;

            if($order){
                $filter_session_data['order'] = $order;
            }
            else{
                $order = $this->session->userdata('order');
            }
            $data['order'] = $order;

            //save session data into the session
            if(isset($filter_session_data)){
            $this->session->set_userdata($filter_session_data);
            }

            $data['count_posts']= $this->posts_model->count_posts($search_string, $order);
            $config['total_rows'] = $data['count_posts'];

            //fetch sql data into arrays
            if($search_string){
                if($order){
                    $data['posts'] = $this->posts_model->get_posts($search_string, $order, $order_type, $config['per_page'],$limit_end);        
                }else{
                    $data['posts'] = $this->posts_model->get_posts($search_string, '', $order_type, $config['per_page'],$limit_end);           
                }
            }else{
                if($order){
                    $data['posts'] = $this->posts_model->get_posts('', $order, $order_type, $config['per_page'],$limit_end);        
                }else{
                    $data['posts'] = $this->posts_model->get_posts('', '', $order_type, $config['per_page'],$limit_end);        
                }
            }

        }else{

            //clean filter data inside section
            $filter_session_data['search_string_selected'] = null;
            $filter_session_data['order'] = null;
            $filter_session_data['order_type'] = null;
            $this->session->set_userdata($filter_session_data);

            //pre selected options
            $data['search_string_selected'] = '';
            $data['order'] = 'id';

            //fetch sql data into arrays
            $data['count_posts']= $this->posts_model->count_posts('','');
            $data['posts'] = $this->posts_model->get_posts('', '', $order_type, $config['per_page'],$limit_end);        
            $config['total_rows'] = $data['count_posts'];

        }

        //initializate the panination helper 
        $this->pagination->initialize($config);   

        //load the view
        $data['main_content'] = 'admin/posts/list';
        $this->load->view('includes/template', $data);  
}  

Upvotes: 3

Views: 1120

Answers (1)

Googlian
Googlian

Reputation: 6723

You can use datatable which is so real time.

DataTables is a plug-in for the jQuery Javascript library. It is a highly flexible tool, based upon the foundations of progressive enhancement, and will add advanced interaction controls to any HTML table.

Pagination, instant search and multi-column orderingSupports almost any data source:DOM, Javascript, Ajax and server-side processingEasily theme-able: DataTables, jQuery UI, Bootstrap, FoundationWide variety of extensions inc. Editor, Buttons, FixedColumns and moreExtensive options and a beautiful, expressive APIFully internationalisableProfessional quality: backed by a suite of 2900+ unit testsFree open source software (MIT license)! Commercial support available.

Just put code:

$(document).ready(function(){  
   $('#myTable').DataTable();
});

enter image description here

Upvotes: 2

Related Questions