Today, see silently explain pagination, think of as if the original area for a long time no one posted, and by the way the silent extension, to a Php+ajax page demo it, OK, say, first of all, we are still the basic AJAX development Framework:
The content I put in a div shows, when the page action is generated, use Ajax to update div to page effect This is the Content Display page code:
<?php
Header ("CONTENT-TYPE:TEXT/HTML;CHARSET=GBK")//output encoding to avoid Chinese garbled
?>
<title>ajax Pagination Demo </title>
<script language= "javascript" src= "Ajaxpg.js" ></script>
<body>
<div id= "Result" >
<?php
$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=dopage (' result ', ' $url? page=1 ');> home </a>";
if ($PREPG) $pagenav. = "<a href=dopage (' result ', ' $url? page= $prepg ');> front page </a>"; else $pagenav. = "front page";
if ($NEXTPG) $pagenav. = "<a href=dopage (' result ', ' $url? page= $nextpg ');> back page </a>"; else $pagenav. = "Back Page";
$pagenav. = "<a href=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
?>
</div>
</body>
The key to turning the page is to call the Dopage () function on the page, and then use the callback information to update the contents of the Div. Server-side Core code:
Copy Code code as follows:
<?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=dopage (' result ', ' $url? page=1 ');> home </a>";
if ($PREPG) $pagenav. = "<a href=dopage (' result ', ' $url? page= $prepg ');> front page </a>"; else $pagenav. = "front page";
if ($NEXTPG) $pagenav. = "<a href=dopage (' result ', ' $url? page= $nextpg ');> back page </a>"; else $pagenav. = "Back Page";
$pagenav. = "<a href=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
?>