PHP Classic Page Function _ Suitable for beginners

Source: Internet
Author: User

<?php
Header ("CONTENT-TYPE:TEXT/HTML;CHARSET=GBK")//output encoding to avoid Chinese garbled
$page =isset ($_get[' page ')? Intval ($_get[' page '): 1; This is to get the value of page in Page=18, if there is no page, then the page number is 1.
$num = 10; Display 10 data per page

$db =mysql_connect ("localhost", "root", "7529639"); Creating a database connection
mysql_select_db ("Cr_download"); Select the database to manipulate

/*
First we have to get the database in the end how much data to determine the specific number of pages, the specific formula is
The total database is divided by the number of bars displayed per page.
That is to say, 10/3=3.3333=4 will enter one.
*/

$result =mysql_query ("SELECT * from Cr_userinfo");
$total =mysql_num_rows ($result); Query all the data

$url = ' test.php ';//Get the URL of this page

Page number calculation
$pagenum =ceil ($total/$num); Get the total number of pages, also the last page
$page =min ($pagenum, $page);//Get home
$PREPG = $page -1;//Previous page
$NEXTPG = ($page = = $pagenum 0: $page + 1);//Next page
$offset = ($page-1) * $NUM; Gets the value of the first parameter of the limit, if the first page is (1-1) *10=0, and the second page is (2-1) *10=10.

To start the pager bar code:
$pagenav = "Show <B>". ($total? ($offset + 1): 0). " </B>-<B> "min ($offset +10, $total)." </B> Records, a total of $total records ";

If only one page jumps out of the function:
if ($pagenum <=1) return false;

$pagenav. = "<a href=javascript:dopage (' result ', ' $url? page=1 ');> home </a>";
if ($PREPG) $pagenav. = "<a href=javascript:dopage (' result ', ' $url? page= $prepg ');> front page </a>"; else $pagenav. = "front page";
if ($NEXTPG) $pagenav. = "<a href=javascript:dopage (' result ', ' $url? page= $nextpg ');> back page </a>"; else $pagenav. = "Back Page";
$pagenav. = "<a href=javascript:dopage (' result ', ' $url? page= $pagenum ');> last </a>";
$pagenav. = "</select> page, total $pagenum page";

If the number of pages passed in is greater than the total number of pages, an error message is displayed
If ($page > $pagenum) {
Echo "Error:can not Found the page". $page;
Exit;
}

$info =mysql_query ("select * from Cr_userinfo limit $offset, $num"); Get the data that the page needs to display
while ($it =mysql_fetch_array ($info)) {
Echo $it [' username '];
echo "<br>";
}//Display data
echo "<br>";
echo $pagenav//Output paging navigation

?>

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.