PHP 分页代码 [英] PHP Pagination Code

查看:36
本文介绍了PHP 分页代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我要做的是创建分页,最多显示 5 页.如果我的 MySQL 语句只返回了五页,则返回五页.否则,每个方向返回 2 页.所以理想情况下,如果我目前在第 1 页,它会显示第 1 2 3 4 5 页.如果我在第 5 页并且最多有 5 页,则显示 1 2 3 4 5.但是,如果我在第 20 页,总共有 100 个,我想显示 18 19 20 21 22.有什么建议吗?

So What I'm trying to do is create pagination, with a max of 5 pages showing. If there are only five pages that are returned from my MySQL statement, return the five pages. Otherwise, return 2 pages in each direction. So ideally, if I'm currently on page 1, it shows pages 1 2 3 4 5. If I am on page 5 and there are a max of 5 pages, show 1 2 3 4 5. If, however, I am on page 20, and there are 100 total, I would like to show 18 19 20 21 22. Any suggestions?

推荐答案

假设您有一个返回大量行的查询:

Let's say you have a query that returns lots and lots of rows:

$query_results = array(...);

并且您对页面上显示的结果数量有最大限制:

And you have a maximum limit to how many results show up on the page:

$results_per_page = 20;

现在,您需要的总页数是

Now, the total number of pages you will need is

$total_pages = ceil(count($query_results)/$results_per_page);

但现在是棘手的部分;您需要知道用户在哪个页面上,以及在该页面上显示什么结果.假设您通过一个简单的 GET 变量确定页码:

But now comes the tricky part; you need to know what page the user is on, and what results show up on that page. Say you determine page number through a simple GET variable:

yoursite.com/results.php?page=2

所以第 1 页对应于结果 0 - 19,第 2 页到 20 - 29 等.然后,您可以像这样确定在那里显示的结果:

So page 1 corresponds to results 0 - 19, page 2 to 20 - 29, etc. Then, you can determine what results get shown there like this:

$page = min($_GET['page'], $total_pages); //ensures you never have an illegal page
$start_results = ($page - 1) * $results_per_page;

$results_on_page = array_slice($query_results, $start_results, $results_per_page);

现在您可以根据需要以 HTML 格式显示这些内容.但是链接呢?你说你想要两个方向的链接,页面上总共有五个链接.

Now you can display those in HTML however you want. But what about the links? You said you wanted two links in either direction, for a total of five links on the page.

$total_links = 5;
$pivot = floor($total_links / 2);

$leftmost = max(1, $page - $pivot);
$rightmost = min($total_pages, $page + $pivot);

$links = array();
for ($i = $leftmost; $i <= $rightmost; $i++) {
    $links[] = "results.php?page=$i";
}

这将为您提供一系列链接,供您在页面上显示.我会把那部分留给你.

That will give you an array of links for you to display on your page. I'll leave that part up to you.

现在,这可以工作,但对于大型结果集来说效率极低,因为所有处理和限制都发生在从数据库返回之后.因此,您可能希望在查询中执行此操作.我将把它留给读者作为练习,但这里有一个提示可以帮助您入门:

Now, this will work, but it will be extremely inefficient for large result sets, because all the processing and limiting happens after it comes back from the database. Therefore, you probably want to do that in your query. I'll leave that as an exercise to the reader, but here's a hint to get you started:

SELECT * FROM some_table LIMIT $results_per_page OFFSET ($page - 1) * $results_per_page

话虽如此,为如此平凡和常见的事情滚动您自己的代码是一种很好的学习体验,但很少能很好地利用您的时间.如果这是用于任何类型的专业工作,请考虑使用预先构建的库,例如 PEAR pager 或 Zend Framework 的 Zend_Paginator 库.

That being said, rolling your own code for something so mundane and common is a good learning experience, but rarely a good use of your time. If this is for any kind of professional work, consider using a pre-built library, like the PEAR pager or Zend Framework's Zend_Paginator library.

这篇关于PHP 分页代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆