ajax+php Page-Paging program

Source: Internet
Author: User

ajax+php paging demo, with the source download!!
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:
Copy Content to Clipboard
Code:
var Http_request=false;
function send_request (URL) {//initialization, specifying handler functions, sending requested functions
Http_request=false;
Start initializing the XMLHttpRequest object
if (window. XMLHttpRequest) {//mozilla browser
Http_request=new XMLHttpRequest ();
if (http_request.overridemimetype) {//Set MIME category
Http_request.overridemimetype ("Text/xml");
}
}
else if (window. ActiveXObject) {//ie browser
try{
Http_request=new ActiveXObject ("msxml2.xmlhttp");
}catch (e) {
try{
Http_request=new ActiveXObject ("Microsoft.XMLHTTP");
}catch (e) {}
}
}
if (!http_request) {//exception, failed to create object instance
Window.alert ("Create XMLHTTP object failed!") ");
return false;
}
Http_request.onreadystatechange=processrequest;
Determine how to send the request, the URL, and whether to execute the next code synchronously
Http_request.open ("Get", url,true);
Http_request.send (NULL);
}
Functions that process return information
function ProcessRequest () {
if (http_request.readystate==4) {//Judge object state
if (http_request.status==200) {//information returned successfully, start processing information
document.getElementById (reobj). Innerhtml=http_request.responsetext;
}
else{//page is not normal
Alert ("The page you requested is not normal!") ");
}
}
}
function Dopage (obj,url) {
document.getElementById (obj). innerhtml= "reading data ...";
Send_request (URL);
Reobj=obj;
}
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:
Copy Content to Clipboard
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=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 to be displayed for the corresponding number of pages
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 Content to Clipboard
Code:
<?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 to be displayed for the corresponding number of pages
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.