PHP分页
摘录于:http://www.phpfreaks.com/tutorial/basic-pagination
这是PHP+Mysql实现分页的技术:
<?php // database connection info $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); // find out how many rows are in the table $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]; // number of rows to show per page $rowsperpage = 10; // find out total pages $totalpages = ceil($numrows / $rowsperpage); // get the current page or set a default if (isset($_GET[‘currentpage‘]) && is_numeric($_GET[‘currentpage‘])) { // cast var as int $currentpage = (int) $_GET[‘currentpage‘]; } else { // default page num $currentpage = 1; } // end if // if current page is greater than total pages... if ($currentpage > $totalpages) { // set current page to last page $currentpage = $totalpages; } // end if // if current page is less than first page... if ($currentpage < 1) { // set current page to first page $currentpage = 1; } // end if // the offset of the list, based on current page $offset = ($currentpage - 1) * $rowsperpage; // get the info from the db $sql = "SELECT id, number FROM numbers LIMIT $offset, $rowsperpage"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); // while there are rows to be fetched... while ($list = mysql_fetch_assoc($result)) { // echo data echo $list[‘id‘] . " : " . $list[‘number‘] . "<br />"; } // end while /****** build the pagination links ******/ // range of num links to show $range = 3; // if not on page 1, don‘t show back links if ($currentpage > 1) { // show << link to go back to page 1 echo " <a href=‘{$_SERVER[‘PHP_SELF‘]}?currentpage=1‘><<</a> "; // get previous page num $prevpage = $currentpage - 1; // show < link to go back to 1 page echo " <a href=‘{$_SERVER[‘PHP_SELF‘]}?currentpage=$prevpage‘><</a> "; } // end if // loop to show links to range of pages around current page for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { // if it‘s a valid page number... if (($x > 0) && ($x <= $totalpages)) { // if we‘re on current page... if ($x == $currentpage) { // ‘highlight‘ it but don‘t make a link echo " [<b>$x</b>] "; // if not current page... } else { // make it a link echo " <a href=‘{$_SERVER[‘PHP_SELF‘]}?currentpage=$x‘>$x</a> "; } // end else } // end if } // end for // if not on last page, show forward and last page links if ($currentpage != $totalpages) { // get next page $nextpage = $currentpage + 1; // echo forward link for next page echo " <a href=‘{$_SERVER[‘PHP_SELF‘]}?currentpage=$nextpage‘>></a> "; // echo forward link for lastpage echo " <a href=‘{$_SERVER[‘PHP_SELF‘]}?currentpage=$totalpages‘>>></a> "; } // end if /****** end build pagination links ******/ ?>
为防止原链接失效,特将讲解摘录如下:
Okay so there‘s the code. Basically the idea is to:
- Find out how many rows you have in your table
- Find out how many pages you want to make, based on how many rows per page you want to show
- Find out what page we‘re on
- Pull out just the rows of the current page
- Display the info
- Make some links to go to the first page, previous page, some pages around the current page, the next page, and the last page.
There‘s comments aplenty in the code, so you may be able to figure out what‘s going on based on that, but let‘s go ahead and break it down for some further commentary. Moving on...
// database connection info
$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);
Just a couple of lines of code to connect to your database, obviously.
// find out how many rows are in the table
$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];
We need to find out how many rows are in the table so we can figure out how many pages we are going to have. So we do a basic count(*) query and assign that number to $numrows.
// number of rows to show per page
$rowsperpage = 10;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);
$rowsperpage is where we decide how many rows per page we want to have. To find out the total pages we will have, we take the number of rows ($numbrows) and divide that by the rows per page ($rowsperpage). Since there‘s no such thing as a half a page (unless your dog chews it up), we will round up, using ceil().
// get the current page or set a default
if (isset($_GET[‘currentpage‘]) && is_numeric($_GET[‘currentpage‘])) {
// cast var as int
$currentpage = (int) $_GET[‘currentpage‘];
} else {
// default page num
$currentpage = 1;
} // end if
When you click on one of the page navigation links, the target page is passed through the URL via the GET method. So here we want to grab the target page number. We want to first check to see if it‘s even there and if it is number, because if this is the first time someone loads the page, it won‘t be set. Or someone could manually change the value in the URL.
If it is there and it is numeric, we are going to force it to be type int. What this means is if someone were to change the page number in the URL to like 9.75, it will truncate it to 9, because there‘s no such thing as page 9.75 unless you‘re Harry Potter.
If the value fails to be set or is not numeric, we default to page 1.
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
The next thing we want to do is check to make sure it‘s a valid page number. We don‘t want users trying manually enter in page 401 in our 400 page book, just because they hate the ending and refuse to believe it ended that way omg what were we thinking nono there‘s GOT to be another page stuck together or something right??? So yeah... if they enter a negative number or some number higher than our total pages, it defaults to 1 or $totalpages, respectively.
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
Since we have our list of 106 rows, and we want to only grab 10 specific rows of our target page, we need to find the "offset." That is, for example, page 8 is not going to start at the top of the list. It‘s going to start on row 80. Actually, it‘s going to start on row 70, since computers like to start counting at zero, not one. So we take our current page (let‘s say page 8), subtract 1 from it, and then multiply by our rows per page (10). Again, computers start at zero, so:
page 1 of our list will be rows 0-9.
page 2 of our list will be rows 10-19.
etc...
// get the info from the db
$sql = "SELECT id, number FROM numbers LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
Now we run our query. It‘s a basic query telling sql to look at our numbers table, start at our offset, and give us 10 rows, starting at that offset. Since our table has 106 rows, if we are on the last page, it will give us the last 6 rows.
// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) {
// echo data
echo $list[‘id‘] . " : " . $list[‘number‘] . "<br />";
} // end while
Next we have a basic loop to display the rows of the current page. Nothing fancy like tables or divs or anything like that, as that is not the point of this tutorial. Next we look at building the navigation bar...
/****** build the pagination links ******/
// if not on page 1, don‘t show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href=‘{$_SERVER[‘PHP_SELF‘]}?currentpage=1‘><<</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href=‘{$_SERVER[‘PHP_SELF‘]}?currentpage=$prevpage‘><</a> ";
} // end if
The first thing we want to do is make some "<< <" links. "<<" brings us back to page 1. "<" takes us to the previous page. If we are on page 1, then there‘s no reason to show the links, so our condition checks to see if we are on some other page besides page 1.
The first echo makes the "<<", passing page 1 as the target page. Since the "<" link targets the previous page, we make a variable that simply subtracts 1 from the current page, and make the link.
// range of num links to show
$range = 3;
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it‘s a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we‘re on current page...
if ($x == $currentpage) {
// ‘highlight‘ it but don‘t make a link
echo " [<b>$x</b>] ";
// if not current page...
} else {
// make it a link
echo " <a href=‘{$_SERVER[‘PHP_SELF‘]}?currentpage=$x‘>$x</a> ";
} // end else
} // end if
} // end for
In this chunk of code, we make the range of page links display. The idea is that if we‘re on page 8, it will show:
<< < 5 6 7 [8] 9 10 11 > >>
We start with a range ($range). The range covers how many numbers to the left and to the right of the current page (not total numbers to be displayed). The range in the code is 3, so if we are on page 8, it shows 5 6 7 on the left, 8 in the middle, 9 10 11 on the right. The loop starts at the current page minus 3, and iterates to the current page plus 3.
Inside the loop, we first check to make sure that the current iteration is a valid page number, because for instance, if we are on page 1, we do not want to show links to page -2 -1 0. The next thing we do is check to see if the current iteration of the loop is on the current page. If it is, we want to ‘highlight‘ it by making it bold with brackets around it to make it stand out a little. We also do not want to make it a link, because it‘s just silly to make a link to the same page you are on. I can just imagine poorly written page raker or spider bots getting stuck in an infinite loop from that, which is kinda funny, except it‘s yourbandwidth they are eating up.
Anyways, if it‘s a valid page number, and it‘s not the current page, then make a link of that page number. The end.
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " <a href=‘{$_SERVER[‘PHP_SELF‘]}?currentpage=$nextpage‘>></a> ";
// echo forward link for lastpage
echo " <a href=‘{$_SERVER[‘PHP_SELF‘]}?currentpage=$totalpages‘>>></a> ";
} // end if
/****** end build pagination links ******/
?>
This chunk of code works exactly like the first chunk of code that made the "<< <" links, except the opposite. If we‘re not on the last page, make the "> >>" links. Take the current page and add 1 to it to make the ">" link. Target page for the ">>" link is simply the $totalpages var.
Well, there you have it. That‘s really all there is to it. There are plenty of extra bells and whistles you can add on to this, like having links to sort your data by each column, etc.. the sky‘s the limit! Perhaps I will eventually publish some sort of "Expanded Pagination" tutorial.
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。