-
- header("content-type:text/html;charset=utf-8");
- //Database connection
- $conn = mysql_connect("localhost", "root", "111 ") or die("not connnected : ".mysql_error());
- mysql_select_db("test", $conn);
- mysql_query("set names utf8");
- //Query how many rows of data there are in total
- $sql1 = " select count(*) from user";
- $ret1 = mysql_query($sql1);
- $row1 = mysql_fetch_row($ret1);
- $tot = $row1[0];
- //How many rows of data per page
- $length = 5;
- //Total number of pages
- $totpage = ceil($tot / $length);
- //Current number of pages
- $page = @$_GET['p'] ? $_GET['p'] : 1 ;
- //limit lower limit
- $offset = ($page - 1) * $length;
- echo "";
- echo "
php padding";
- echo "
";
- echo "
";
- echo "
ID | ";
- echo "
USER | " ;
- echo "
PASS | ";
- echo "
";
- //Display the queried data in a table
- $sql2 = "select * from user order by id limit { $offset}, {$length}";
- $ret2 = mysql_query($sql2);
- while ($row2 = mysql_fetch_assoc($ret2)) {
- echo "
";
- echo "
{ $row2['id']} | {$row2['name']} | {$row2['pass']} | " ;
- echo "
";
- }
- echo "
";
- //Previous page and next page
- $prevpage = $page - 1;
- if ($page >= $totpage) {
- $nextpage = $totpage;
- } else {
- $nextpage = $page + 1;
- }
- //Jump
- echo "";
- echo "";
Copy code
Key points of pagination code:
"$sql2 = "select * from user order by id limit {$offset}, {$length}";", the relationship between $offset, $length and the number of pages.
How to get the previous page and next page, and the critical point.
|