In the message board, the use of the page, so I wrote this page note since the page has already started to write, certainly mastered some knowledge of PHP and the basic operation of MySQL in the paging, I also encountered a lot of problems, but we do not fear, no matter what problems there will always be a solution First we should deal with the data, if there are 10, or 100 pieces of data you may not feel anything, but if there are 1000 data on a page, you will certainly look impatient, now we set each page display 10, is divided into 100 pages. The corresponding database operation:Select * FROM table limit 0,10
This is a very simple MySQL query statement, its role is to extract 10 data from a table called tables, and the values of all fields are obtained.
The key here is the "limit 0,10", which indicates that 0 is the starting point for querying the following 10 data
How do you want to display id=11 to id=20 data?
Select * FROM table limit 10,10
If you want to show other data, just change the value of the first parameter.
How do I get the value of $page? Recommended use of $page = $_get[' page ']; You can get the value of the page
See the code for details:
<?php
/* Connect to database operation */
$conn = mysql_connect ("localhost", "root", "") or Die ("Linked server Error". Mysql_error ());
mysql_select_db ("Db_book", $conn) or Die ("Link Database error!");
/* Paging principle */
$sql 1 = "SELECT * from Db_liuyan"; The query indicates that all data for Db_liuyan
$result = mysql_query ($sql 1);
$number = mysql_num_rows ($result); Gets the total number of record bars in the table
$pagesize = 10; Show 10 records per page
/* How many pages do I get altogether? Use the Ceil function, divide if the surplus number, will automatically enter 1, meaning that if there are 26 data, each page shows 10, you will calculate 3 pages */
$pagetotal = Ceil ($number/$pagesize); Calculate the total number of pages
$pageno = $_get[' PageNo '); $pageno indicates the number of pages displayed on the page
$start = $pagesize * ($page-1); Calculates which record the display page starts from, and if $page is 1, start with the id=0 record, or 2 from the id=10 record.
$sql 2 = "select * from ' Db_liuyan ' ORDER by ' ID ' ASC limit $startno, $pagesize";
It is important to note that the query should be ordered in order, ASC follows the sequence DESC
$result 2 = mysql_query ($sql 2);
/* When doing paging I, there are some problems, if only one page record, click on the previous page or the next page, will continue to jump to the next page, but I have only one page of records Ah,
I took advantage of if~else to solve the problem.
/* If $page is empty, assign a value of 1 to $page*/
if ($page = = "") {
$pageno = 1;
}
echo "<a href=?pageno=1> home </a>";
if ($pageno ==1) {
echo "<a href=?pageno=1> page </a>";
}
else{
echo "<a href=?pageno=". ($pageno-1). " > Prev </a> ";
}
/* Use for loop, display 1 2 3 Go to jump */
for ($i =1; $i <= $pagetotal; $i + +) {
$show = ($i! = $pageno)? " <a href= ' show.php?pageno= '. $i. "' > $i </a> ":" <b> $i </b> ";
echo "$show";
}
if ($pageno = = $pagetotal) {
echo "<a href=?pageno=". $pagetotal. " > Next </a> ";
}
else{
echo "<a href=?pageno=". ($pageno + 1). " > Next </a> ";
}
echo "<a href=?pageno=". $pagetotal. " > End </a> ";
?>