PHP中常用的分页类总结
php分页是目前在显示大量结果时所采用的最好的方式,有了下面这些代码的帮助,开发人员可以在多个页面中显示大量的数据,在互联网上,分页是一般用于搜索结果或是浏览全部信息.
php基本分页,代码如下:
- <?php
-
- $conn = mysql_connect('localhost','dbusername','dbpass') or trigger_error("SQL", E_USER_ERROR);
- $db = mysql_select_db('dbname',$conn) or trigger_error("SQL", E_USER_ERROR);
-
-
- $sql = "SELECT COUNT(*) FROM numbers";
- $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
- $r = mysql_fetch_row($result);
- $numrows = $r[0];
-
-
- $rowsperpage = 10;
-
- $totalpages = ceil($numrows / $rowsperpage);
-
-
- if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
-
- $currentpage = (int) $_GET['currentpage'];
- } else {
-
- $currentpage = 1;
- }
-
-
- if ($currentpage > $totalpages) {
-
- $currentpage = $totalpages;
- }
-
- if ($currentpage < 1) {
-
- $currentpage = 1;
- }
-
-
- $offset = ($currentpage - 1) * $rowsperpage;
-
-
- $sql = "SELECT id, number FROM numbers LIMIT $offset, $rowsperpage";
- $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
-
-
- while ($list = mysql_fetch_assoc($result)) {
-
- echo $list['id'] . " : " . $list['number'] . "<br />";
- }
-
-
-
- $range = 3;
-
-
- if ($currentpage > 1) {