我记得几年前,当我第一次开始使用 php 和mysql编码时,我第一次从数据库中获取信息并显示在 Web 浏览器中时是多么高兴。
对于数据库和编程知识很少的人来说,根据我编写的代码(好吧,所以我从书中复制了一个示例)看到这些表行显示在屏幕上给了我一种胜利的感觉。那时我可能还没有完全理解工作中的所有魔力,但第一次成功激励我进行更大更好的项目。
虽然我对数据库的热情可能与以前不同,但自从我第一次“hello world”遇到 PHP 和 Mysql 以来,我就迷上了让事情变得简单易用的力量。
作为开发人员,我经常面临的一个问题是获取大量信息并使其易于消化。无论是大公司的客户列表还是个人 MP3 目录,不得不坐下来盯着一排排又一排的数据令人沮丧和沮丧。一个好的开发者能做什么?分页!
寻找快速解决方案?
如果您正在寻找快速解决方案,Envato Market上有大量的分页脚本和助手。
例如,试用ZPager PHP 分页类。只需单击几下,即可轻松生成列表分页链接并对其进行自定义。ZPager 支持 Bootstrap、MySQL 和 Smarty 模板引擎,它还允许您使用对seo友好的 URL。
1.分页
分页本质上是获取一组结果并将它们分散到页面上以使其更易于查看的过程。
我很早就意识到,如果我有 5,000 行信息要显示,不仅有人尝试阅读会很头疼,而且大多数浏览器都需要 Internet 永恒(即大约 5 秒以上)才能显示它。
为了解决这个问题,我创建了一个简单、灵活且易于使用的 PHP 类,我可以在我的所有项目中使用它来进行分页。
2.数据库
必须爱 MySQL。没有冒犯其他数据库系统,但对我来说,我需要的只是 MySQL。MySQL 的一个重要特性是它们在https://dev.mysql.com/doc/#sampledb上为您提供了一些免费的示例数据库供您使用。
对于我的示例,我将使用世界数据库(压缩后约 90k),其中包含超过 4,000 条记录可供使用,但我们将创建的 PHP 脚本的美妙之处在于它可以与任何数据库一起使用。现在我想我们都同意,如果我们决定不对结果进行分页,我们最终会得到一些非常长且难以处理的结果,如下所示:
因此,让我们开始将我们的数据分解成易于消化的片段,如下所示:
漂亮,不是吗?将分页类放入代码后,只需几行代码,您就可以快速轻松地将大量数据转换为易于导航的页面。真的。
3.分页器
这个例子将由两个脚本组成:可重用的分页器类和将显示表项和控件的索引文件。
分页器.class.php
分页器类将只有两个方法和构造函数。我们将逐步构建它,并在我们前进的过程中解释每一步。
<?php class Paginator { private $_conn; private $_limit; private $_page; private $_query; private $_total; }
这个定义只设置了分页器需要d 个成员变量。由于这是一个辅助类,并且只用于分页,因此它将依赖于与 MySQL 服务器的有效连接和已定义的查询,我们将在其中附加必要的参数来对结果进行分页。我们将从构造方法开始。
<?php public function __construct( $conn, $query ) { $this->_conn = $conn; $this->_query = $query; $rs= $this->_conn->query( $this->_query ); $this->_total = $rs->num_rows; }
很简单,对吧?该方法只设置对象的数据库连接和必要的查询。之后,它计算该查询检索到的总行数,没有任何限制或跳过参数。这个总数是为分页器创建链接所必需的。
请注意,为简单起见,我们不对给定参数进行错误检查或任何其他验证,但在实际应用程序中,这些检查将是必要的。
检索结果
现在,让我们创建一个实际对数据进行分页并返回结果的方法。
<?php public function getdata( $limit = 10, $page = 1 ) { $this->_limit = $limit; $this->_page = $page; if ( $this->_limit == 'all' ) { $query = $this->_query; } else { $query = $this->_query . " LIMIT " . ( ( $this->_page - 1 ) * $this->_limit ) . ", $this->_limit"; } $rs = $this->_conn->query( $query ); while ( $row = $rs->fetch_assoc() ) { $results[] = $row; } $result = new stdClass(); $result->page = $this->_page; $result->limit = $this->_limit; $result->total = $this->_total; $result->data = $results; return $result; }
让我们一步一步分析。首先,我们设置limit和page参数,默认分别设置为10和1。然后,我们检查用户是否需要给定数量的行或所有行。基于此和页面参数,我们设置LIMIT查询的术语。请注意,我们从页码中取 1,因为我们的脚本从 1 开始计数,而不是从 0 开始。
在此之后,我们只需评估查询并获得结果。最后,我们创建一个新的结果对象,其中包含已执行查询的限制、页面和总参数,以及每个检索到的行的数据。
显示分页链接
现在,让我们编写用于获取分页链接的方法。
<?php public function createLinks( $links, $list_class ) { if ( $this->_limit == 'all' ) { return ''; } $last = ceil( $this->_total / $this->_limit ); $start = ( ( $this->_page - $links ) > 0 ) ? $this->_page - $links : 1; $end = ( ( $this->_page + $links ) < $last ) ? $this->_page + $links : $last; $html = '<ul class="' . $list_class . '">'; $class = ( $this->_page == 1 ) ? "disabled" : ""; $html .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . ( $this->_page - 1 ) . '">«</a></li>'; if ( $start > 1 ) { $html .= '<li><a href="?limit=' . $this->_limit . '&page=1">1</a></li>'; $html .= '<li class="disabled"><span>...</span></li>'; } for ( $i = $start ; $i <= $end; $i++ ) { $class = ( $this->_page == $i ) ? "active" : ""; $html .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . $i . '">' . $i . '</a></li>'; } if ( $end < $last ) { $html .= '<li class="disabled"><span>...</span></li>'; $html .= '<li><a href="?limit=' . $this->_limit . '&page=' . $last . '">' . $last . '</a></li>'; } $class = ( $this->_page == $last ) ? "disabled" : ""; $html .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . ( $this->_page + 1 ) . '">»</a></li>'; $html .= '</ul>'; return $html; }
这是一个相当长的方法,所以让我们更详细地看一下它。
首先,我们评估用户是否需要给定数量的链接或所有链接。如果用户请求所有链接,那么我们只需返回一个空字符串,因为不需要分页。
在此之后,我们根据可用的总行数和每页所需的项目来计算最后一页。
然后,我们取 links 参数,它表示在当前页面下方和上方显示的链接数,并计算要创建的开始和结束链接。
接下来,我们为列表创建开始标记并使用列表类参数设置它的类。我们还添加了“上一页”链接——注意对于这个链接,我们检查当前页面是否是第一页,如果是,我们设置链接的禁用属性。我们还显示一个指向第一页的链接和一个省略号,以防起始链接不是第一个。
接下来,我们根据之前计算的开始和结束参数添加当前页面下方和上方的链接。在每个步骤中,我们根据显示的链接页面评估当前页面并相应地设置活动类。
在此之后,我们显示另一个省略号和指向最后一页的链接,以防结束链接不是最后一个。
最后,我们显示“下一页”链接,并在用户查看最后一页时设置禁用状态,关闭列表,并返回生成的 HTML 字符串。
这就是Paginator课堂的全部内容!当然,我们可以为数据库连接、限制、页面、查询和总参数添加 setter 和 getter,但为简单起见,我们将保持这种方式。
4.索引页面
现在我们将创建index.php文件,该文件负责使用Paginator类和显示数据。首先,让我向您展示基本的 HTML。
<!DOCTYPE html> <head> <title>PHP Pagination</title> <link rel="stylesheet" href="css/bootstrap.min.css"> </head> <body> <div class="container"> <div class="col-md-10 col-md-offset-1"> <h1>PHP Pagination</h1> <table class="table table-striped table-condensed table-bordered table-rounded"> <thead> <tr> <th>City</th> <th width="20%">Country</th> <th width="20%">Continent</th> <th width="25%">Region</th> </tr> </thead> <tbody></tbody> </table> </div> </div> </body> </html>
非常简单——这个文件只显示一个表格,我们将从数据库中检索到的信息填充该表格。请注意,对于本示例,我使用 Bootstrap 进行基本页面样式设置。
使用分页器
<?php for( $i = 0; $i < count( $results->data ); $i++ ) : ?> <tr> <td><?php echo $results->data[$i]['Name']; ?></td> <td><?php echo $results->data[$i]['Country']; ?></td> <td><?php echo $results->data[$i]['Continent']; ?></td> <td><?php echo $results->data[$i]['Region']; ?></td> </tr> <?php endfor; ?>
现在,为了使用我们的Paginator类,在文档顶部添加以下 PHP 代码。
<?php require_once 'Paginator.class.php'; $conn = new mysqli( '127.0.0.1', 'root', 'root', 'world' ); $limit = ( isset( $_GET['limit'] ) ) ? $_GET['limit'] : 25; $page = ( isset( $_GET['page'] ) ) ? $_GET['page'] : 1; $links = ( isset( $_GET['links'] ) ) ? $_GET['links'] : 7; $query = "SELECT City.Name, City.CountryCode, Country.Code, Country.Name AS Country, Country.Continent, Country.Region FROM City, Country WHERE City.CountryCode = Country.Code"; $Paginator = new Paginator( $conn, $query ); $results = $Paginator->getData( $limit, $page ); ?>
这个脚本非常简单——我们只需要我们的Paginator类。请注意,此代码假定此文件与index.php文件位于同一目录中 - 如果不是这种情况,您应该相应地更新路径。
然后,我们使用 MySQLi 库创建与数据库的连接,从 GET 请求中检索分页器参数,并设置查询。由于这不是一篇关于 MySQL 的文章,因此我不会详细介绍此处使用的连接或查询。
最后,我们创建Paginator对象并检索当前页面的结果。
显示结果
现在,要显示获得的结果,请将以下代码添加到表体中。
<?php for( $i = 0; $i < count( $results->data ); $i++ ) : ?> <tr> <td><?php echo $results->data[$i]['Name']; ?></td> <td><?php echo $results->data[$i]['Country']; ?></td> <td><?php echo $results->data[$i]['Continent']; ?></td> <td><?php echo $results->data[$i]['Region']; ?></td> </tr><?php endfor; ?>
在这里,我们只是简单地遍历包含城市记录的结果数据属性,并为每个记录创建一个表格行。
分页链接
现在,要显示分页器链接,请在表格下方添加以下代码。
<?php echo $Paginator->createLinks( $links, 'pagination pagination-sm' ); ?>
向 paginatorcreateLinks方法传递获取的links参数和用于从 Bootstrap 使用的分页链接的 CSS 类。这是创建页面的结果。
如何实现基于ajax的分页
在本节中,我们将快速介绍如何将上述 PHP 分页示例转换为基于 AJAX 的分页。我们将修改所有文件并讨论我们需要进行的更改。
首先,让我们更新index.php文件,如下面的代码片段所示。
<!DOCTYPE html> <head> <title>PHP Pagination</title> <link rel="stylesheet" href="css/bootstrap.min.css"> </head> <body> <div class="container"> <div class="col-md-10 col-md-offset-1"> <h1>PHP Pagination</h1> <div id="ajax_wrapper"> <?php require_once 'ajax_pagination.php'; ?> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script> <script src="pagination.js"></script> </body> </html>
首先,我们将构建分页所需的 HTML 代码移动到ajax_pagination.php文件中,以便我们可以重用它。接下来,我们在 文件末尾加载了 jQuery 库和pagination.js文件。
ajax_pagination.php文件如下所示。
<?php require_once 'paginator.php'; $conn = new mysqli( '127.0.0.1', 'root', 'root', 'world' ); $limit = ( isset( $_GET['limit'] ) ) ? $_GET['limit'] : 25; $page = ( isset( $_GET['page'] ) ) ? $_GET['page'] : 1; $links = ( isset( $_GET['links'] ) ) ? $_GET['links'] : 7; $query = "SELECT City.Name, City.CountryCode, Country.Code, Country.Name AS Country, Country.Continent, Country.Region FROM City, Country WHERE City.CountryCode = Country.Code"; $Paginator = new Paginator( $conn, $query ); $results = $Paginator->getData( $limit, $page ); ?> <table class="table table-striped table-condensed table-bordered table-rounded"> <thead> <tr> <th>City</th> <th width="20%">Country</th> <th width="20%">Continent</th> <th width="25%">Region</th> </tr> </thead> <tbody> <?php for( $i = 0; $i < count( $results->data ); $i++ ) : ?> <tr> <td><?php echo $results->data[$i]['first_name']; ?></td> <td><?php echo $results->data[$i]['last_name']; ?></td> <td><?php echo $results->data[$i]['email']; ?></td> <td><?php echo $results->data[$i]['phone']; ?></td> </tr> <?php endfor; ?> </tbody> </table> <?php echo $Paginator->createLinks( $links, 'pagination pagination-sm' ); ?>
这几乎是一样的。
接下来,我们看一下pagination.js文件。
$(document).on( "click", ".pagination a", function(e) { var pageValue = $(this).attr("data-page"); $.ajax({ url: '/ajax_pagination.php?limit=25&page='+pageValue, type: "GET", success: function(data){ $("#ajax_wrapper").html(data); } }); e.preventDefault(); });
它在这里做了几件事。首先,它将点击事件绑定到每个分页链接。接下来,当点击链接时,它会进行 AJAX 调用以获取相应页面的列表数据并更新页面。
最后,您需要更新createLinks方法,如以下代码段所示。
public function createLinks( $links, $list_class ) { if ( $this->_limit == 'all' ) { return ''; } $last = ceil( $this->_total / $this->_limit ); $start = ( ( $this->_page - $links ) > 0 ) ? $this->_page - $links : 1; $end = ( ( $this->_page + $links ) < $last ) ? $this->_page + $links : $last; $html = '<ul class="' . $list_class . '">'; $class = ( $this->_page == 1 ) ? "disabled" : ""; $html .= '<li class="' . $class . '"><a data-page="' . ( $this->_page - 1 ) . '" href="?limit=' . $this->_limit . '&page=' . ( $this->_page - 1 ) . '">«</a></li>'; if ( $start > 1 ) { $html .= '<li><a data-page="1" href="?limit=' . $this->_limit . '&page=1">1</a></li>'; $html .= '<li class="disabled"><span>...</span></li>'; } for ( $i = $start ; $i <= $end; $i++ ) { $class = ( $this->_page == $i ) ? "active" : ""; $html .= '<li class="' . $class . '"><a data-page="' . $i . '" href="?limit=' . $this->_limit . '&page=' . $i . '">' . $i . '</a></li>'; } if ( $end < $last ) { $html .= '<li class="disabled"><span>...</span></li>'; $html .= '<li><a data-page="' . $last . '"href="?limit=' . $this->_limit . '&page=' . $last . '">' . $last . '</a></li>'; } $class = ( $this->_page == $last ) ? "disabled" : ""; $html .= '<li class="' . $class . '"><a data-page="' . ( $this->_page + 1 ) . '" href="?limit=' . $this->_limit . '&page=' . ( $this->_page + 1 ) . '">»</a></li>'; $html .= '</ul>'; return $html; }
基本上,我们已经data-page为每个链接添加了该属性,这样我们就可以在用户单击链接时获取页码。
通过这些更改,您已将分页转换为基于 AJAX 的分页!
结论
这应该为您提供您需要知道的一切,以便在应用程序中启动和运行分页。
- 寻找快速解决方案?
- 分页器.class.php
- 检索结果
- 显示分页链接
- 使用分页器
- 显示结果
- 分页链接