CodeIgniter中的分页:完整指南
Chinese (Simplified) (中文(简体)) translation by Soleil (you can also view the original English article)
使用任何全栈Web应用程序框架的好处是您不必担心输入处理,表单验证等常见任务,因为框架已经为这些功能提供了包装器。 因此,它允许您专注于应用程序的业务逻辑,而不是一遍又一遍地重新发明轮子。
今天,我们将探索CodeIgniter框架中的一个重要库 - 分页库。
让我重点介绍一下我们将在本文中介绍的主题:
- 基本分页的演示
- 探索自定义选项
- 分页配置
基本分页的演示
在本节中,我们将通过一个示例演示如何在CodeIgniter中使用分页。 这是了解事情如何完全运作的最佳方式。
在我们的示例中,我们将构建一个非常简单的用户列表,我们将从用户MySQL表中获取记录。 为了成功运行此示例,请确保在users表中包含uid
和uname
字段。
有了这个设置,我们就准备好了。
继续创建一个控制器文件controllers/Paging .php
,其中包含以下内容。
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Paging extends CI_Controller { public function __construct() { parent::__construct(); // load Pagination library $this->load->library('pagination'); // load URL helper $this->load->helper('url'); } public function index() { // load db and model $this->load->database(); $this->load->model('Users'); // init params $params = array(); $limit_per_page = 1; $start_index = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; $total_records = $this->Users->get_total(); if ($total_records > 0) { // get current page records $params["results"] = $this->Users->get_current_page_records($limit_per_page, $start_index); $config['base_url'] = base_url() . 'paging/index'; $config['total_rows'] = $total_records; $config['per_page'] = $limit_per_page; $config["uri_segment"] = 3; $this->pagination->initialize($config); // build paging links $params["links"] = $this->pagination->create_links(); } $this->load->view('user_listing', $params); } public function custom() { // load db and model $this->load->database(); $this->load->model('Users'); // init params $params = array(); $limit_per_page = 2; $page = ($this->uri->segment(3)) ? ($this->uri->segment(3) - 1) : 0; $total_records = $this->Users->get_total(); if ($total_records > 0) { // get current page records $params["results"] = $this->Users->get_current_page_records($limit_per_page, $page*$limit_per_page); $config['base_url'] = base_url() . 'paging/custom'; $config['total_rows'] = $total_records; $config['per_page'] = $limit_per_page; $config["uri_segment"] = 3; // custom paging configuration $config['num_links'] = 2; $config['use_page_numbers'] = TRUE; $config['reuse_query_string'] = TRUE; $config['full_tag_open'] = '<div class="pagination">'; $config['full_tag_close'] = '</div>'; $config['first_link'] = 'First Page'; $config['first_tag_open'] = '<span class="firstlink">'; $config['first_tag_close'] = '</span>'; $config['last_link'] = 'Last Page'; $config['last_tag_open'] = '<span class="lastlink">'; $config['last_tag_close'] = '</span>'; $config['next_link'] = 'Next Page'; $config['next_tag_open'] = '<span class="nextlink">'; $config['next_tag_close'] = '</span>'; $config['prev_link'] = 'Prev Page'; $config['prev_tag_open'] = '<span class="prevlink">'; $config['prev_tag_close'] = '</span>'; $config['cur_tag_open'] = '<span class="curlink">'; $config['cur_tag_close'] = '</span>'; $config['num_tag_open'] = '<span class="numlink">'; $config['num_tag_close'] = '</span>'; $this->pagination->initialize($config); // build paging links $params["links"] = $this->pagination->create_links(); } $this->load->view('user_listing', $params); } }
接下来,我们需要一个模型文件models/Users.php
,它从users表中获取记录。
<?php // models/Users.php defined('BASEPATH') OR exit('No direct script access allowed'); class Users extends CI_Model { function __construct() { parent::__construct(); } public function get_current_page_records($limit, $start) { $this->db->limit($limit, $start); $query = $this->db->get("users"); if ($query->num_rows() > 0) { foreach ($query->result() as $row) { $data[] = $row; } return $data; } return false; } public function get_total() { return $this->db->count_all("users"); } }
最后,让我们在views/user_listing.php
创建一个显示用户列表的视图文件。
<!-- views/user_listing.php --> <html> <head> <title>Paging Example-User Listing</title> </head> <body> <div class="container"> <h1 id='form_head'>User Listing</h1> <?php if (isset($results)) { ?> <table border="1" cellpadding="0" cellspacing="0"> <tr> <th>ID</th> <th>NAME</th> </tr> <?php foreach ($results as $data) { ?> <tr> <td><?php echo $data->uid ?></td> <td><?php echo $data->uname ?></td> </tr> <?php } ?> </table> <?php } else { ?> <div>No user(s) found.</div> <?php } ?> <?php if (isset($links)) { ?> <?php echo $links ?> <?php } ?> </div> </body> </html>
现在,继续访问我们的自定义页面http://your-code-igniter-site/paging/index,你应该看到用户列表以及分页! 所以,就是这样,我们已经做到了! 别担心,我不会很快离开你,因为我们现在开始解析代码的每一部分。
我们将从模型文件models/Users.php
开始,因为这将从我们的控制器方法调用。 我们的模型实现了两个重要的方法get_current_page_records
和get_total
,以构建分页链接。
我们来看看get_total
方法。 它用于计算users表中的记录数。
public function get_total() { return $this->db->count_all("users"); }
接下来,有一个get_current_page_records
方法。
public function get_current_page_records($limit, $start) { $this->db->limit($limit, $start); $query = $this->db->get("users"); if ($query->num_rows() > 0) { foreach ($query->result() as $row) { $data[] = $row; } return $data; } return false; }
您应该在get_current_page_records
方法中注意两个重要参数。 第一个参数$limit
用于指定在查询运行期间返回的记录数。 第二个参数$start
充当记录的起始索引。
因此,正如您所看到的,给定$start
和$limit
的值,我们可以按页面获取记录。 这是分页的本质,同时我们已经实现了本文最重要的方法!
所以,这就是我们的模型 - 简单而优雅!
继续前进,让我们将注意力转移到控制器文件上。 继续并获取构造函数方法的代码。
public function __construct() { parent::__construct(); // load Pagination library $this->load->library('pagination'); // load URL helper $this->load->helper('url'); }
为了在CodeIgniter中使用分页,您需要做的第一件事就是加载分页库。 我们可以使用$this->load->library('pagination')
来实现。
我们还加载了URL帮助程序,以便我们可以使用该帮助程序提供的全局帮助程序函数。
现在,我们已经准备好了解控制器的核心 - index
方法。
public function index() { // load db and model $this->load->database(); $this->load->model('Users'); // init params $params = array(); $limit_per_page = 1; $start_index = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; $total_records = $this->Users->get_total(); if ($total_records > 0) { // get current page records $params["results"] = $this->Users->get_current_page_records($limit_per_page, $start_index); $config['base_url'] = base_url() . 'paging/index'; $config['total_rows'] = $total_records; $config['per_page'] = $limit_per_page; $config["uri_segment"] = 3; $this->pagination->initialize($config); // build paging links $params["links"] = $this->pagination->create_links(); } $this->load->view('user_listing', $params); }
首先,我们确保正确加载数据库。 接下来,我们加载Users
模型,以便我们可以使用模型方法。
$this->load->database(); $this->load->model('Users');
接下来,我们初始化几个重要的变量。
// init params $params = array(); $limit_per_page = 1; $start_index = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; $total_records = $this->Users->get_total();
变量$limit_per_page
定义每页的限制。 当然,您可以按照自己的意愿设置它; 例如,它目前设置为1。
$start_index
变量保存MySQL记录的起始索引。 当CodeIgniter构建分页链接时,它会默认将页面的起始索引作为URL中的第三个段附加。 您可以更改此默认行为,但这是我们将在本文的最后一部分保留的内容,我们将在其中讨论自定义选项。
最后,我们调用Users模型的get_total
方法来获取users表的总记录,并将其分配给$total_records
变量。
接下来,我们使用get_current_page_records
方法获取当前页面的记录。
// get current page records $params["results"] = $this->Users->get_current_page_records($limit_per_page, $start_index);
在我们真正开始构建分页链接之前,我们需要使用分页库的initialize
方法初始化最小分页配置。
$config['base_url'] = base_url() . 'paging/index'; $config['total_rows'] = $total_records; $config['per_page'] = $limit_per_page; $config["uri_segment"] = 3; $this->pagination->initialize($config);
这是构建分页链接的最小参数集。
- base_url:构建分页链接时将使用的URL
- total_rows:记录总数
- per_page:每页记录计数
最后,我们使用create_links
方法来构建分页链接。
// build paging links $params["links"] = $this->pagination->create_links();
其余的只是调用我们的视图user_listing
并呈现输出的形式! 运行URL http://your-code-igniter-site /paging/index以查看用户列表以及分页链接。
这是一个非常简单但有用的分页示例供您使用,您可以扩展以满足您的要求。
在下一节中,我们将探讨如何根据外观和功能自定义默认分页。
探索自定义选项
在本节中,如果您希望自定义默认的分页链接,我们将探索您可以使用的选项。
URI Segment
尽管CodeIgniter分页库会自动检测URL中与分页相关的参数,但如果您具有不同的URL模式,则可以定义自定义值。
$config["uri_segment"] = 4;
数字链接数
num_links
选项允许您定义将在分页链接中的活动页码之前和之后显示的数字链接数。
$config['num_links'] = 2;
页码作为URI段
当您访问分页URI段时,默认情况下它是一个起始索引。 例如,如果每页有10条记录,则第三页的分页URI段为20。 相反,如果要在分页链接中显示实际页码,可以将use_page_numbers
设置为TRUE
。
$config['use_page_numbers'] = TRUE;
当然,您需要确保根据从URL检索的页码计算正确的起始索引。
保留查询字符串
通常情况下,您最终会遇到要保留与分页无关的查询字符串参数的情况。 您可以使用reuse_query_string
选项来启用该工具。
$config['reuse_query_string'] = TRUE;
这些是您可以用来更改默认分页功能的几个选项。 接下来,我们将介绍一些其他选项,允许您更改分页链接的显示方式。
包装标签
如果要将分页代码与任何其他HTML标记包装在一起,则可以使用full_tag_open
和full_tag_close
选项来完成。
$config['full_tag_open'] = '<div class="pagination">'; $config['full_tag_close'] = '</div>';
如果您希望将自定义样式应用于分页链接,那么它可能非常有用。
第一个,最后一个,下一个和上一个
如果要更改将为第一个,最后一个,下一个和上一个链接显示的文本,您也可以这样做。
$config['first_link'] = 'First Page'; $config['last_link'] = 'Last Page'; $config['next_link'] = 'Next Page'; $config['prev_link'] = 'Prev Page';
此外,如果您想将这些单独的链接与任何HTML标记包装在一起,您可以像我们用它来包装整个分页代码一样。
$config['first_tag_open'] = '<span class="firstlink">'; $config['first_tag_close'] = '</span>'; $config['last_tag_open'] = '<span class="lastlink">'; $config['last_tag_close'] = '</span>'; $config['next_tag_open'] = '<span class="nextlink">'; $config['next_tag_close'] = '</span>'; $config['prev_tag_open'] = '<span class="prevlink">'; $config['prev_tag_close'] = '</span>';
活动链接和号码链接
有时,您希望以不同方式设置活动链接的样式。 您可以通过应用包装器标签来实现,如下所示。
$config['cur_tag_open'] = '<span class="curlink">'; $config['cur_tag_close'] = '</span>';
同样,如果你想用一些东西包装数字链接:
$config['num_tag_open'] = '<span class="numlink">'; $config['num_tag_close'] = '</span>';
这就结束了定制的故事。 实际上,您可以继续查看http://your-code-igniter-site/paging/custom中的自定义示例,该示例已包含在我们的控制器文件中!
寻呼配置
现在您已经了解了为任何模型列表设置合适分页所需的配置。 大多数情况下,您希望在整个网站中保持相同。 你要做什么来实现这一目标? 您可能想要复制配置代码并将其粘贴到需要分页配置的每个操作中。
事实上,有一种更好的方法可以处理这种情况。 您可以在application/config/pagination.php
中创建分页配置文件,并使用$config
变量来定义您的设置。
<?php $config['per_page'] = 10; $config["uri_segment"] = 3; $config['full_tag_open'] = '<div class="pagination">'; $config['full_tag_close'] = '</div>'; $config['first_link'] = 'First Page'; $config['first_tag_open'] = '<span class="firstlink">'; $config['first_tag_close'] = '</span>'; $config['last_link'] = 'Last Page'; $config['last_tag_open'] = '<span class="lastlink">'; $config['last_tag_close'] = '</span>'; $config['next_link'] = 'Next Page'; $config['next_tag_open'] = '<span class="nextlink">'; $config['next_tag_close'] = '</span>'; $config['prev_link'] = 'Prev Page'; $config['prev_tag_open'] = '<span class="prevlink">'; $config['prev_tag_close'] = '</span>'; $config['cur_tag_open'] = '<span class="curlink">'; $config['cur_tag_close'] = '</span>'; $config['num_tag_open'] = '<span class="numlink">'; $config['num_tag_close'] = '</span>';
基于此,修订后的index
操作方法应如下所示:
public function index() { // load db and model $this->load->database(); $this->load->model('Users'); // init params $params = array(); $start_index = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; $total_records = $this->Users->get_total(); // load config file $this->config->load('pagination', TRUE); $settings = $this->config->item('pagination'); $settings['total_rows'] = $this->Users->get_total(); $settings['base_url'] = base_url() . 'paging/config'; if ($total_records > 0) { // get current page records $params["results"] = $this->Users->get_current_page_records($settings['per_page'], $start_index); // use the settings to initialize the library $this->pagination->initialize($settings); // build paging links $params["links"] = $this->pagination->create_links(); } $this->load->view('user_listing', $params); }
当然,total_rows
和base_url
变量在不同操作之间变化,因此您需要在每个操作中明确设置它们。
要实现这一点,您需要首先加载分页配置。
$this->config->load('pagination', TRUE); $settings = $this->config->item('pagination');
接下来,您可以覆盖特定于操作的设置。
$settings['total_rows'] = $this->Users->get_total(); $settings['base_url'] = base_url() . 'paging/config';
而你已经完成了!
这就是分页配置的故事,这也结束了这篇文章!
结论
今天,我们通过CodeIgniter中的分页库。
在本文的第一部分中,我通过提供一个非常简单但有用的示例演示了如何使用分页库。
接下来,我们讨论了在设置分页时可以使用的自定义选项。
最后,我们在最后一节讨论了分页配置。
CodeIgniter是一个功能强大的PHP平台。 无论您是刚开始使用还是从下一个版本开始,都不要忘记查看我们可以为您提供的服务。
我很乐意使用下面的Feed以查询和评论的形式了解您的反馈!
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Update me weekly