CodeIgniter 中的分页问题

Posted

技术标签:

【中文标题】CodeIgniter 中的分页问题【英文标题】:Pagination Issue in CodeIgniter 【发布时间】:2017-01-16 04:10:21 【问题描述】:

我的系统中的分页存在问题。分页在这里工作正常。

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

实际上,我有 6 个搜索关键字“shihas”的结果。

一旦我转到 www.example.com/path/2 ,我也可以看到第 6 个结果。

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

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

【问题讨论】:

你的意见是什么? 你的意思是搜索? 你能提供一些代码吗?重新编辑您的问题 您是否有某种条件可以在代码中的某处显示链接? @Tpojka 在视图里面我没有条件。 【参考方案1】:

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

DataTables 是 jQuery javascript 库的插件。它是一个高度灵活的工具,基于渐进增强的基础,可以向任何 html 表格添加高级交互控件。

分页、即时搜索和多列排序支持几乎任何数据源:DOM、Javascript、Ajax 和服务器端处理轻松主题化:DataTables、jQuery UI、Bootstrap、FoundationWide 各种扩展程序。编辑器、按钮、固定列等广泛的选项和漂亮的, 富有表现力的 API 完全国际化的专业品质:由 2900 多个单元测试套件支持免费开源软件(MIT 许可)!提供商业支持。

只需输入代码:

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

【讨论】:

以上是关于CodeIgniter 中的分页问题的主要内容,如果未能解决你的问题,请参考以下文章

如何使 Codeigniter 中的分页类与 AJAX 一起工作?

CodeIgniter 分页 url 页码

在codeigniter中搜索所有分页链接的分页

如何在 Codeigniter 的分页类中隐藏页面链接?

在Codeigniter中搜索的分页不起作用

如何在codeigniter中的单个函数中运行多个分页