Home  >  Article  >  Backend Development  >  Ajax example code to perfectly implement the paging function of two web pages_PHP tutorial

Ajax example code to perfectly implement the paging function of two web pages_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:11:49662browse

分页的首页

复制代码 代码如下:


武侠小说分页



<script><br>var page='';<br>  function init(page){<br>  document.getElementById("tables").innerHTML='';<br>   var xhr;<br>   if(window.XMLHttpRequest){<br> xhr = new XMLHttpRequest();<br>}else if(window.ActiveXObject){<br> xhr =new ActiveXObject("Microsoft.XMLHTTP")<br>}<br>var url="fenye.php";<br> xhr.open("POST",url,true);<br> xhr.onreadystatechange=callback;<br> xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");<br> if(page){<br> xhr.send("page="+page);<br> }<br> else {xhr.send("page=1");}<br> var content;<br> function callback(){<br>   if(xhr.readyState==4){<br>     if(xhr.status==200){<br>      var json =eval('('+xhr.responseText+')');<br>   //alert(xhr.responseText);<br>  var fenye=json.str;<br> //  alert(fenye);<br>     document.getElementById('div').innerHTML=fenye;<br>  content="<th>ID</th><th>名称</th><th>作者</th><th>出版社</th><th>ISBN号</th><th>类型</th><th>价格</th>";<br>  for(var i=0;i<json.info.length;i++){<BR>  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>";<br>  document.getElementById("tables").innerHTML=content;<br>  }<br>    // alert(fenye);<br>     }<br>   }<br>   }<br> }<br><br></script>



 

jquery实现$.ajax的分页




ID名称作者出版社ISBN号类型价格


 


 

 

 





 

分页的php精华代码

复制代码 代码如下:

//Command the model layer to process data
$link=mysql_connect('localhost','root','') or die("failure");
mysql_select_db('books',$link) or die("Error connecting to the database!");
//Display the number of records per page
$pagesize = 2;
//Find the total number of records
$sql = "select count(*) as total_rows from books";
$result = mysql_query($sql);
$total_rows = mysql_fetch_array($result);
//Find the total Number of pages
$pages = ceil($total_rows[0]/$pagesize);
//The current page
$page = $_POST['page'];
$strtext = " Current page ".$page."."Total".$pages."Page"."Total".$total_rows[0]."Record";
//var_dump($str);
//Next, I need to find the corresponding data based on the currently clicked page number
$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) ;
//The data link will be obtained and then returned to
$first=1;
$prev=$page-1;
$next=$page+1;
$last=$ pages;

//Command the view layer to display data
$first_a = "";
if($page>1){
$prev_a = "";
}
if($page<$pages){
$next_a = "";
}
$last_a = "";
@$str = $strtext.$first_a.$prev_a.$next_a.$last_a;
//var_dump($str);
$info = array('str'=>$str,'info'=> $pageInfo);
echo json_encode($info);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326843.htmlTechArticle Copy the code on the homepage of the page as follows: meta http-equiv="Content-Type" content="text/html ; charset=utf8" / h1 align="center"Martial arts novel paging/h br/ script src="jquery-1.4.2.min...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn