Ajax perfect implementation of two pages paging function instance code _php instance

Source: Internet
Author: User
Tags prev

Home page of pagination

Copy Code code as follows:

<meta http-equiv= "Content-type" content= "text/html; Charset=utf8 "/>
&LT;H1 align= "center" > wuxia Novels page <br/>
<script src= "Jquery-1.4.2.min.js" ></script>
<script>
var page= ';
function init (page) {
document.getElementById ("Tables"). Innerhtml= ';
var xhr;
if (window. XMLHttpRequest) {
XHR = new XMLHttpRequest ();
}else if (window. ActiveXObject) {
XHR =new ActiveXObject ("Microsoft.XMLHTTP")
}
var url= "fenye.php";
Xhr.open ("POST", url,true);
Xhr.onreadystatechange=callback;
Xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
if (page) {
Xhr.send ("page=" +page);
}
else {xhr.send ("page=1");}
var content;
function callback () {
if (xhr.readystate==4) {
if (xhr.status==200) {
var json =eval (' (' +xhr.responsetext+ ') ');
alert (Xhr.responsetext);
var fenye=json.str;
alert (fenye);
document.getElementById (' div '). Innerhtml=fenye;
content= "<th>ID</th><th> name </th><th> author </th><th> Publishing house </th><th &GT;ISBN </th><th> type </th><th> price </th> ";
for (Var i=0;i<json.info.length;i++) {
content+= "<tr><td>" +json.info[i].id+ "</td><td>" +json.info[i].name+ "</td><td > "+json.info[i].author+" </td><td> "+json.info[i].publisher+" </td><td> "+json.info[i". isbn+ "</td><td>" +json.info[i].type+ "</td><td>" +json.info[i].price+ "</tr>";
document.getElementById ("Tables"). Innerhtml=content;
}
alert (fenye);
}
}
}
}

</script>


<body onload= "init ()" >

<table id= "Tables" style= "width:500px; height:100px; Text-align:center "align=" Center "border=" 5 "bordercolor=" #FF6600 ">
<th>ID</th><th> name </th><th> author </th><th> Publishing house </TH><TH>ISBN </th><th> type </th><th> Price </th>
</table>
<table align= "center" style= "margin-top:15px" >
<tr><td>
<TD style= "Colspan:3;height:20" >
<div id= "div" style= "position:absolute; left:447px; top:218px; width:411px; height:22px; " ></div>
</td>
</tr>
</table>
<br/>
</body>


Pagination of the PHP essence code

Copy Code code as follows:

<?php
Command model layer Process data
$link =mysql_connect (' localhost ', ' root ', ') ' or Die ("failed");
mysql_select_db (' books ', $link) or Die ("failed to connect to the database!") ");
Show number of records per page
$pagesize = 2;
Find out the total number of records
$sql = "SELECT count (*) as total_rows from books";
$result = mysql_query ($sql);
$total _rows = mysql_fetch_array ($result);
Find total number of pages
$pages = ceil ($total _rows[0]/$pagesize);
Current first few pages
$page = $_post[' page '];
$strtext = "current paragraph". $page. " Page "." Total ". $pages." Page "." Altogether ". $total _rows[0]." Record ";
Var_dump ($STR);
Next, I'm going to figure out the corresponding data based on the page number I'm currently clicking on.
$offset = $pagesize * ($page-1);
$sql = "SELECT * from books limit $offset, $pagesize";
mysql_query ("Set names UTF8");
$res =mysql_query ($sql);

$rows =array ();
while ($row =mysql_fetch_assoc ($res)) {
$rows []= $row;
}
$pageInfo = $rows;
echo Json_encode ($pageInfo);
Var_dump ($pageInfo);
will get the data link, and then return
$first = 1;
$prev = $page-1;
$next = $page +1;
$last = $pages;

Command View layer display data
$first _a = "<a onclick= ' init (". $first. ") ' href= ' # ' ></a> ";
if ($page >1) {
$prev _a = "<a onclick= ' init (". $prev. ") ' href= ' # ' ></a> ";
}
if ($page < $pages) {
$next _a = "<a onclick= ' init (". $next. ") ' href= ' # ' ></a> ";
}
$last _a = "<a onclick= ' init (". $last. ") ' href= ' # ' ></a> ";
@ $str = $strtext. $first _a. $prev _a. $next _a. $last _a;
Var_dump ($STR);
$info = Array (' str ' => $str, ' info ' => $pageInfo);
echo Json_encode ($info);

Related Article

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.