使用codeigniter
When you perform a search on Google, the results are displayed in a list and you have the option to click through pages of the results. That is an example of pagination. Pagination simply means the user has the ability to click through the pages of results, viewing a subset of them each time, rather than having to scroll down the screen for ages.
当您在Google上执行搜索时,搜索结果会显示在列表中,并且您可以选择点击搜索结果页面。 这是分页的一个例子。 分页只是意味着用户可以单击结果页面,每次查看结果的一个子集,而不必向下滚动屏幕一段时间。
Pagination is particularly useful when you are coding an application that interfaces with a database. A large dataset might have hundreds of possible results for one query, and pagination creates a much nicer user experience.
当您编码与数据库接口的应用程序时,分页特别有用。 大型数据集可能对一个查询具有数百种可能的结果,而分页可提供更好的用户体验。
In this tutorial, I’ll use CodeIgniter’s pagination library to show you how you can create a paginated list of results from a MySQL database. Along the way, you’ll also see how to fix a problem with the pagination links that the library might produce.
在本教程中,我将使用CodeIgniter的分页库向您展示如何从MySQL数据库创建结果的分页列表。 在此过程中,您还将看到如何解决库可能产生的分页链接问题。
I’ll assume you have an installation of CodeIgniter 2.1 ready to go. For the database, I’ll use the official sample database provided by MySQL that available for download as an SQL file.
我假设您已经准备好安装CodeIgniter 2.1。 对于数据库,我将使用MySQL提供的官方示例数据库,该数据库可作为SQL文件下载。
We’ll start by creating a model in the application which needs to do two things: provide a count of all the records in the Country table, and retrieve a list of countries from the table. Save the following as models/countries.php:
我们将首先在应用程序中创建一个模型,该模型需要做两件事:提供Country表中所有记录的计数,并从表中检索国家列表。 将以下内容另存为models/countries.php :
class Countries extends CI_Model { public function __construct() { parent::__construct(); } public function record_count() { return $this->db->count_all("Country"); } public function fetch_countries($limit, $start) { $this->db->limit($limit, $start); $query = $this->db->get("Country"); if ($query->num_rows() > 0) { foreach ($query->result() as $row) { $data[] = $row; } return $data; } return false; } }CodeIgniter’s database class with Active Record is used to count and retrieve the data from the database.
具有Active Record的CodeIgniter的数据库类用于对数据库中的数据进行计数和检索。
The record_count() method returns the number of records and is needed because one of the options in the $config array for the pagination library is $config["total_rows"]. The library will use this along with other options we set to work out how to divide the results up into pages.
record_count()方法返回记录数,这是需要的,因为$config数组中用于分页库的选项之一是$config["total_rows"] 。 该库将与我们设置的其他选项一起使用,以确定如何将结果分成页面。
The fetch_countries() method retrieves a list of all the records from the Country table. There are two arguments for this method: $limit and $start. They will be passed into the query to determine how many records to return, and what record to start from. The arguments will be set in the controller.
fetch_countries()方法从Country表中检索所有记录的列表。 此方法有两个参数: $limit和$start 。 它们将被传递到查询中,以确定要返回多少条记录以及从哪条记录开始。 参数将在控制器中设置。
Next, we need to create a method in the default Welcome controller (controllers/welcome.php) called example1(). But just before we do that, we’ll need to load the model and also the pagination library in the class’ constructor.
接下来,我们需要在默认的Welcome控制器( controllers/welcome.php )中创建一个名为example1() 。 但是在执行此操作之前,我们需要在类的构造函数中加载模型以及分页库。
<?php class Welcome extends CI_Controller { public function __construct() { parent:: __construct(); $this->load->helper("url"); $this->load->model("Countries"); $this->load->library("pagination"); } public function example1() { $config = array(); $config["base_url"] = base_url() . "welcome/example1"; $config["total_rows"] = $this->Countries->record_count(); $config["per_page"] = 20; $config["uri_segment"] = 3; $this->pagination->initialize($config); $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; $data["results"] = $this->Countries-> fetch_countries($config["per_page"], $page); $data["links"] = $this->pagination->create_links(); $this->load->view("example1", $data); } }The example1() method starts by providing some of the most common configuration options for the pagination library. The options are placed in an array which is then passed as an argument to the library’s initialize method. The library requires the options must pass a base URL, a total row count, how many rows per page we want, and which part of the URL will contain the page section the user is using.
example1()方法首先为分页库提供一些最常见的配置选项。 将选项放置在数组中,然后将其作为参数传递给库的initialize方法。 该库要求选项必须传递基本URL,总行数,每页需要多少行以及URL的哪一部分将包含用户正在使用的页面部分。
Remember those parameters that were set required by the query for a list of countries ($limit and $start)? Those are set next in the call to the model’s fetch_countries() method. The $page variable uses the ternary operator to either set its value to whatever is in the third segment of the URI string or to zero (meaning the user is on the first page).
还记得查询中为国家列表( $limit和$start )设置的那些参数吗? 这些将在对模型的fetch_countries()方法的调用中接下来设置。 $page变量使用三元运算符将其值设置为URI字符串第三段中的任何值或设置为零(表示用户位于第一页上)。
Finally, the create_links() method of the pagination library creates the pagination links and then we load the view to display the outcome.
最后,分页库的create_links()方法创建分页链接,然后加载视图以显示结果。
For the view file, we can copy the views/welcome_message.php and called it example1.php and replace most of the content of the body with the following:
对于视图文件,我们可以复制views/welcome_message.php并将其命名为example1.php ,并将主体的大部分内容替换为以下内容:
<body> <div id="container"> <h1>Countries</h1> <div id="body"> <?php foreach($results as $data) { echo $data->Name . " - " . $data->Continent . "<br>"; } ?> <p><?php echo $links; ?></p> </div> <p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p> </div> </body>So now when you visit http://yourdomain/welcome/example1 you should see something like this:
因此,现在当您访问http://yourdomain/welcome/example1您应该会看到类似以下内容:
Click the pagination links and you should find yourself moving through the 239 records in the Country table. But you may notice that the number of linked digits change. Initially there were there digits, and later there five!
单击分页链接,您应该会发现自己遍历了Country表中的239条记录。 但是您可能会注意到链接数字的数量发生了变化。 最初有数字,后来有五个!
There is a config item that might help to solve this problem. It’s probably useful at this stage to remember that we are retrieving records from a database and CodeIgniter has to re-calculate the digit links as each page is accessed. The first thing we can do is to disable the first/last previous/next links to tidy up the navigation. It doesn’t stop the expanding digits, but it does tidy things up.
有一个配置项可能有助于解决此问题。 在此阶段可能要记住,我们正在从数据库中检索记录,并且在访问每个页面时CodeIgniter必须重新计算数字链接,这可能是有用的。 我们要做的第一件事就是禁用第一个/最后一个上一个/下一个链接以整理导航。 它不会阻止数字的扩展,但会整理整齐。
The second thing we can do is calculate how many pages there are by dividing the total rows by the number of rows required per page, round the result, and pass it to the $config["num_links"] parameter.
我们可以做的第二件事是通过将总行除以每页所需的行数来计算有多少页,将结果四舍五入,然后将其传递给$config["num_links"]参数。
Here’s what the example1() method looks like with those changes:
经过这些更改后, example1()方法的外观如下所示:
public function example1() { $config["base_url"] = base_url() . "welcome/example1"; $config["total_rows"] = $this->Countries->record_count(); $config["per_page"] = 20; $config["uri_segment"] = 3; $choice = $config["total_rows"] / $config["per_page"]; $config["num_links"] = round($choice); $this->pagination->initialize($config); $page = ($this->uri->segment(3))? $this->uri->segment(3) : 0; $data["results"] = $this->Countries ->fetch_countries($config["per_page"], $page); $data["links"] = $this->pagination->create_links(); $this->load->view("example1", $data); }This will improve the links now.
现在将改善链接。
Now you know how to use the most useful configuration options for the CodeIgniter pagination library, and you can also fix the way the pagination links are displayed to provide a consistent experience for your users. A user guide comes with every download of CodeIgniter, so be sure to check the other configuration options for the library there. It contains options for styling the pagination links, and changing the way the links are rendered on the page.
现在,您知道如何对CodeIgniter分页库使用最有用的配置选项,并且还可以修复分页链接的显示方式,以为用户提供一致的体验。 每次下载CodeIgniter时都会附带一个用户指南,因此请确保检查该库的其他配置选项。 它包含用于设置分页链接样式以及更改链接在页面上呈现方式的选项。
Image via Alexis Puentes / Shutterstock
图片来自Alexis Puentes / Shutterstock
And if you enjoyed reading this post, you’ll love Learnable; the place to learn fresh skills and techniques from the masters. Members get instant access to all of SitePoint’s ebooks and interactive online courses, like Jump Start PHP.
并且,如果您喜欢阅读这篇文章,您会喜欢Learnable的 ; 向大师学习新鲜技能的地方。 会员可以立即访问所有SitePoint的电子书和交互式在线课程,例如Jump Start PHP 。
Comments on this article are closed. Have a question about PHP frameworks? Why not ask it on our forums?
本文的评论已关闭。 对PHP框架有疑问吗? 为什么不在我们的论坛上提问呢?
翻译自: https://www.sitepoint.com/pagination-with-codeigniter/
使用codeigniter
相关资源:CodeIgniter分页类pagination使用方法示例