PHP Pagination Code Learning sample Sharing _php instance

Source: Internet
Author: User

Copy Code code as follows:

<?php
Header ("Content-type:text/html;charset=utf-8");
Database connection
$conn = mysql_connect ("localhost", "root", "a") or Die ("Not connnected:". Mysql_error ());
mysql_select_db ("Test", $conn);
mysql_query ("Set names UTF8");

How many rows of data are there in a query
$sql 1 = "SELECT count (*) from user";
$ret 1 = mysql_query ($sql 1);
$row 1 = mysql_fetch_row ($ret 1);
$tot = $row 1[0];

How many rows of data per page
$length = 5;
Total pages
$totpage = Ceil ($tot/$length);

Current page
$page = @$_get[' P ']? $_get[' P ']: 1;
Limit lower Limit
$offset = ($page-1) * $LENGTH;

echo "<center>";
echo "echo "<table width= ' 700px ' border= ' 1px ' >";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>USER</th>";
echo "<th>PASS</th>";
echo "</tr>";

Display the queried data in a table
$sql 2 = "SELECT * from user order by ID limit {$offset}, {$length}";
$ret 2 = mysql_query ($sql 2);
while ($row 2 = MYSQL_FETCH_ASSOC ($ret 2)) {
echo "<tr>";
echo "<td>{$row 2[' id ']}</td><td>{$row 2[' name ']}</td><td>{$row 2[' pass ']}</td > ";
echo "</tr>";
}

echo "</table>";

Previous page and next page
$prevpage = $page-1;
if ($page >= $totpage) {
$nextpage = $totpage;
} else {
$nextpage = $page + 1;
}

Jump
echo "echo "</center>";

Core point:

<1> "$sql 2 =" SELECT * from user order by ID limit {$offset}, {$length} ";", $offset, $length, and the relationship between pages.

<2> the previous page and the next page to get the way, as well as the critical point.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.