CodeIgniter中的分页问题

时间:2016-09-08 09:30:35

标签: php mysql html5 codeigniter pagination

我在系统中遇到了分页问题。分页在这里运作良好。

enter image description here

但是,如果我在该文本字段中搜索某些内容,并且结果超过5($config['per_page'] = 5;),则表示不显示分页按钮。

enter image description here

实际上,搜索关键字' shihas '的结果为6。

一旦我去www.example.com/path/2,我也能看到第六个结果。

enter image description here

但是,分页链接不存在。请告诉我这个问题的解决方案。
控制器:

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);  
}  

1 个答案:

答案 0 :(得分:2)

您可以使用实时的数据表。

DataTables是jQuery Javascript库的插件。它是一个高度灵活的工具,基于渐进增强的基础,并将为任何HTML表添加高级交互控件。

分页,即时搜索和多列排序几乎支持任何数据源:DOM,Javascript,Ajax和服务器端处理易于主题化:DataTables,jQuery UI,Bootstrap,FoundationWide各种扩展程序。编辑器,按钮,FixedColumns和更多广泛的选项和一个美丽的,  富有表现力的APIFully internationalisableProfessional质量:由2900+单元测试套件支持免费开源软件(MIT许可证)!提供商业支持。

只需输入代码:

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

enter image description here