如何在 Codeigniter 中创建分页?

Posted

技术标签:

【中文标题】如何在 Codeigniter 中创建分页?【英文标题】:How to create Pagination in Codeiginter? 【发布时间】:2014-05-30 20:35:24 【问题描述】:

我是 codeigniter 的新手,我正在尝试对从数据库中获取的记录创建分页。我编写了以下代码,它显示了分页,但它不影响结果集,我仍然在同一页面上保留所有记录,当我点击Page2 时,它显示no page found

请指导我如何创建分页?

型号

public function students_list()



    $this->db->select('*');
    $this->db->from('student'); 

    $this->db->limit($limit,$start);                

    $query= $this->db->get();

    return $query->result();

    

控制器

public function all_students()


    //Pagination    
            $this->load->library('pagination');

            $config['base_url'] = base_url().'mycontroller/all_students/';
            $config['total_rows'] = 200;
            $config['per_page'] = 5;

            $this->pagination->initialize($config);

            echo $this->pagination->create_links(); 

    //Pagination

    $this->load->model('loginmodel');
    $data['students_data']= $this->loginmodel->students_list();
    $data['dropdown']=      $this->loginmodel->get_degree_names();

    $this->load->view('all_students', $data);


查看

<?php
include 'header.php';
include 'sidebar.php';


$this->pagination->create_links();
?>
<?php 

foreach($students_data as $teachers_records)

    ...
     ....

【问题讨论】:

很多关于分页的问题 我已经检查了大部分,但仍然无法理解为什么它在这里不起作用 我只是举个例子,根据你的回答转换 在你的代码中你没有设置限制来生成分页 请根据我的回答改写 【参考方案1】:

首先我建议创建一个辅助函数来像这样配置您的分页。您必须从 codeigniter 文档中了解配置中的选项目的

if (!function_exists('getPaginationConfig')) 
    function getPaginationConfig($url,$total_row,$per_page,$num_links=3)
            $config = array();
            $config['use_page_numbers'] = TRUE;
            $config['uri_segment'] = 3;// depends on how you passing your page number
            $config['base_url'] = $url;
            $config['total_rows'] = $total_row;
            $config['per_page'] = $per_page; 
            $config['full_tag_open'] = '<ul>';
            $config['full_tag_close'] = '</ul>';
            $config['cur_tag_open'] = '<li class="active"><a href="">';
            $config['cur_tag_close'] = '</a></li>';
            $config['num_tag_open'] = '<li>';
            $config['num_tag_close'] = '</li>';
            $config['next_tag_open'] = '<li>';
            $config['next_tag_close'] = '</li>';
            $config['prev_tag_open'] = '<li>';
            $config['prev_tag_close'] = '</li>';
            $config['first_link'] = '&lt;&lt;';
            $config['first_tag_open'] = '<li>';
            $config['first_tag_close'] = '</li>';
            $config['last_link'] = '&gt;&gt;';
            $config['last_tag_open'] = '<li>';
            $config['last_tag_close'] = '</li>';
            $config['num_links'] = $num_links;

     return $config;        
   

现在在控制器上..您需要从数据库中获取总行数并在分页中设置每页 并使用配置调用 这是控制器功能的示例

 function getMyList($page)
        $limit = YOUR_PAGE_LIMIT;//may be some global variable or input from frontend
        $starting = ($page-1)*$limit;

        $data['results']  = $this->your_model->getListOfTable($where,$limit,$starting);
        $total_rows = $this->your_model->getTotalRowsOfTable($where);// where is filter condition

        $this -> load -> helper('your_helper');// where yours getPaginationConfig is defined
        $this -> load -> library('pagination');
        $this -> pagination -> initialize(getPaginationConfig(site_url('controller/function'), $total_rows,$limit));
        $data['pagination'] = $this -> pagination -> create_links();
        $this->load->view('yourview',$data);

你的模型可能看起来像这样

function getListOfTable($where = array(), $limit, $starting = 0) 
        $this -> db -> select('*');
        // <-- There is never any reason to write this line!
        $this -> db -> from('your_tables');
        if (!empty($where))
           $this -> db -> where($where);
        $this -> db -> limit($limit, $starting);
        $query = $this -> db -> get();

        if ($query -> num_rows() > 0) 
            return $query -> result();
         else 
            return FALSE;
        
    

function getTotalRowsOfTable($where = array())
    if (!empty($data))
         $this -> db -> where($where);
    $this -> db -> from('your_table');
    return $this -> db -> count_all_results();


现在 On View 你只需要在你喜欢的地方回显分页

  <div id="pagination"><?php echo $pagination;?></div>

【讨论】:

【参考方案2】:

控制器:

public function all_students() 
       $this->load->model('loginmodel');
        $config['anchor_class'] = '';
        $config['show_count'] = true;
        $config['base_url'] = site_url('controllername/all_students');
        $config['total_rows'] = sizeof($this->loginmodel->students_list());

        $data['students_data']= $this->loginmodel->students_list();
        $data['dropdown']=      $this->loginmodel->get_degree_names();
        $config['per_page'] = 10;
        $this->jquery_pagination->initialize($config);
        $data['links'] = $this->jquery_pagination->create_links();
        $data['page'] = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data['details'] = $this->loginmodel->students_list($config['per_page'], $data['page']);
        $this->load->view('all_students', $data);
    

型号

public function students_list($limit=NULL,$start=NULL)



    $this->db->select('*');
    $this->db->from('student'); 

    $this->db->limit($limit,$start);                

    $query= $this->db->get();

    return $query->result();

 

【讨论】:

非常感谢..我已经用你的代码替换了我的代码,现在它在我的 Model A PHP Error was encountered Severity: Notice Message: Undefined variable: start Filename: models/loginmodel.php Line Number: 582 中给了我以下错误消息 我已经在我的问题中添加了模型代码...请检查它 错误依然存在..A PHP Error was encountered Severity: Warning Message: Missing argument 1 for Loginmodel::students_list(), called in F:\xampp\htdocs\University\application\controllers\mycontroller.php on line 613 and defined Filename: models/loginmodel.php Line Number: 590 模型功能略有变化 @AdarshMPallickal 为什么你在控制器中查询两次?我建议把它放在一个变量上使用它。并且 sizeof 返回当前结果的总和 .. 不是表中的所有行【参考方案3】:

为了更好地使用分页,请在您的助手中创建一个函数。这对您将来的使用非常有用。你永远不需要加载库和其他值。 在您的助手中修复此功能。

if( ! function_exists('createPaginationForm')) 
    function createPaginationForm($url, $totalRows, $perPage, $segment, $queryString = '', $config = array()) 
        $CI =& get_instance();
        $CI->load->library('pagination');
        $config['base_url'] = base_url().$url;
        $config['total_rows'] = $totalRows;
        $config['per_page'] = $perPage;
        $config['uri_segment'] = $segment;
        $CI->pagination->initialize($config);
        return $CI->pagination->create_links($queryString);
    

并在您的控制器中使用。

var $perPage = '10';
var $segment = '4';
$total_result = "50";
$pagination = createPaginationForm('admin/news/index/',$total_result ,$this->perPage, $this->segment);
$this->data['pagination'] = $pagination;

并在您的视图中打印。

echo $pagination

【讨论】:

【参考方案4】:

您需要一些数据在分页使用查询中显示此和$config['total_rows'],因此它将显示带有 5 条记录的总结果分页

在我的情况下,我使用 $this-&gt;Account-&gt;search_users($where,false, false,false,true,false); 是一个模型函数,将返回结果数组

return $query->result_array();

控制器:-

      $this->load->library('pagination');   
    $config['total_rows'] = $this->Account->search_users($where,false, false,false,true,false);
    $config['per_page']= 5;
    $config['uri_segment'] = 3;
    $config['base_url']= base_url().'/app/index'; 
    $config['suffix'] = '?'.http_build_query($_GET, '', "&"); 
    $this->pagination->initialize($config);
    $this->data['paginglinks'] = $this->pagination->create_links();     
    $this->data['per_page'] = $this->uri->segment(3);       
    $this->data['offset'] = $offset ;

查看:-

<div class="pagination" style="float:right;"> <?php echo $paginglinks; ?></div>

更多信息:- http://ellislab.com/codeigniter/user-guide/libraries/pagination.html

我的模型函数是:-

public function search_users($where = false, $num = false, $offset = false, $order_by = false, $count = false, $select = false , $table=false)

    if(!$select)$select = 'U.*';

        if(isset($where['where']) && !empty($where['where'])) 
            $this->db->where($where['where']);
        

        if($order_by)
            $this->db->order_by($order_by[0], $order_by[1]);
        else
            $this->db->order_by('U.u_id', 'DESC');
        

        $query = $this->db->get($table . ' AS U');
        //print_r($this->db->last_query()).'<hr>';
        $recount = $query->num_rows;    
        if($count) 
            return $recount;    
        

        if($recount > 0)
            return $query->result_array();
        
        else 
            return false;
        
    

【讨论】:

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

如何在 Django 中创建分页?

在 Spring Data JPA 中创建分页

Codeigniter count_all 用于分页

插入表格会在 s-s-rS 报告中创建分页符

Laravel 分页

在 Spark 中创建分箱直方图