PHP Ajax Paging Implementation program code sharing

Source: Internet
Author: User
The code is as follows Copy Code

CREATE TABLE ' Tb_user ' (
' ID ' int (a) not NULL auto_increment,
' username ' varchar not NULL,
PRIMARY KEY (' id ')
) Engine=myisam DEFAULT Charset=utf8 auto_increment=10;


INSERT into ' Tb_user ' VALUES (1, ' AAA ');
INSERT into ' Tb_user ' VALUES (2, ' BBB ');
INSERT into ' Tb_user ' VALUES (3, ' CCC ');
INSERT into ' Tb_user ' VALUES (4, ' ddd ');
INSERT into ' Tb_user ' VALUES (5, ' Eee ');
INSERT into ' Tb_user ' VALUES (6, ' FFF ');
INSERT into ' Tb_user ' VALUES (7, ' GGG ');
INSERT into ' Tb_user ' VALUES (8, ' hhh ');
INSERT into ' Tb_user ' VALUES (9, ');

Ajaxpage.js ~ 1KB

The code is as follows Copy 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 ...";
Reobj = obj;
Send_request (URL);
}

PHP calling code

The code is as follows Copy Code

<title>php+ajax Pagination Demo </title>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<script language= "javascript" src= "Ajaxpage.js" ></script>

<div id= "Result" >
<?php
$terry =mysql_connect ("localhost", "root", "") or Die ("Connection Database failed:". Mysql_error ());
mysql_select_db ("Ajaxtest", $terry);
mysql_query ("Set NAMES ' UTF8 '");
$result =mysql_query ("SELECT * from Tb_user");

$total =mysql_num_rows ($result) or Die (Mysql_error ());

$page =isset ($_get[' page ')? Intval ($_get[' page '): 1;
$page _size=3;
$url = ' index.php ';

$pagenum =ceil ($total/$page _size);
$page =min ($pagenum, $page);
$prepage = $page-1;
$nextpage = ($page = = $pagenum 0: $page + 1);
$pageset = ($page-1) * $page _size;
$pagenav = ';
$pagenav. = "Show <font color= ' Red ' >". ($total? ($pageset + 1): 0). " -". Min ($pageset +5, $total)." </font> Records &nbsp; total <b><font color= ' yellow ' > '. $total. " </font></b> Records &nbsp; is now the first &nbsp;<b><font color= ' Blue ' > '. $page. " </font></b>&nbsp; page &nbsp; ";
if ($page <=1)
$pagenav. = "<a style=cursor:not-allowed;> home </a>&nbsp;";
Else
$pagenav. = "<a onclick=javascript:dopage (' result ', ' $url? page=1 ') style=cursor:pointer;> home </a>& nbsp; ";
if ($prepage)
$pagenav. = "<a onclick=javascript:dopage (' result ', ' $url? page= $prepage ') style=cursor:pointer;> previous page </a> &nbsp; ";
Else
$pagenav. = "<a style=cursor:not-allowed;> prev </a>&nbsp;";
if ($nextpage)
$pagenav. = "<a onclick=javascript:dopage (' result ', ' $url? page= $nextpage ') style=cursor:pointer;> next page </a >&nbsp; ";
Else
$pagenav. = "<a style=cursor:not-allowed;> next page </a>&nbsp;";
if ($pagenum)
$pagenav. = "<a onclick=javascript:dopage (' result ', ' $url? page= $pagenum ') style=cursor:pointer;> last </a> &nbsp; ";
Else
$pagenav. = "<a style=cursor:not-allowed;> last </a>&nbsp;";
$pagenav. = "Total". $pagenum. " Page ";

if ($page > $pagenum) {
echo "Error: no this page". $page;
Exit ();
}
?>
<table align= "center" border= "2" width= ">"
<tr bgcolor= "#cccccc" align= "Center" >
<td> User name </td>
<td> User Password </td>
</tr>
<?php
$info =mysql_query ("SELECT * from Tb_user" desc limit $pageset, $page _size ");
while ($array =mysql_fetch_array ($info)) {
?>
<TR align= "center" >
<td><?php echo $array [' id '];? ></td>
<td><?php echo $array [' username '];? ></td>
</tr>
<?php
}
?>
</table>
<?php
echo "<p align=center> $pagenav </p>";
?>
</div>

We use only in a section of code to save the face of a single file can be achieved OH.

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.