php paging code learning examples to share
- 2021-01-11 01:56:54
- OfStack
<?php
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
$sql1 = "select count(*) from user";
$ret1 = mysql_query($sql1);
$row1 = mysql_fetch_row($ret1);
$tot = $row1[0];
// Number of rows per page
$length = 5;
// Total number of pages
$totpage = ceil($tot / $length);
// The current number of pages
$page = @$_GET['p'] ? $_GET['p'] : 1;
//limit The lower limit
$offset = ($page - 1) * $length;
echo "<center>";
echo "<h2>php padding</h2>";
echo "<table width='700px' border='1px' >";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>USER</th>";
echo "<th>PASS</th>";
echo "</tr>";
// Display the query 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 "<tr>";
echo "<td>{$row2['id']}</td><td>{$row2['name']}</td><td>{$row2['pass']}</td>";
echo "</tr>";
}
echo "</table>";
// on 1 Under the page and 1 page
$prevpage = $page - 1;
if ($page >= $totpage) {
$nextpage = $totpage;
} else {
$nextpage = $page + 1;
}
// jump
echo "<h3><a href='index.php?p={$prevpage}'> on 1 page </a>|<a href='index.php?p={$nextpage}'> Under the 1 page </a></h3>";
echo "</center>";
Emphasis:
< 1 > $sql2 = "select * from user order by id limit {$offset}, {$length}"; , $offset, $length, and page number.
< 2 > How to get the previous page and the next page, and the critical point.