ajax-分页实例

<?php
header ( "Content-Type: text/html; charset=UTF-8" );
?>
<html>
<head>
<title>ajax分页</title>
<script src="ajax.js"></script>
<style>
.d {
	font-size: 13px
}

table {
	font-size: 14px
}

a {
	text-decoration: none
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div id="result" class="d">
   <?php
			$page = isset ( $_GET ["page"] ) ? intval ( $_GET ["page"] ) : 1;
			$num = 3; //每页显示3条`	1`数据
			echo $page;
			$link = mysql_connect ( "localhost", "root", "huochao" ) or die ( "数据库连接失败" . mysql_error () );
			mysql_select_db ( "mysql" ) or die ( "数据库选择失败" . mysql_error () );
			
			$result = mysql_query ( "select * from help_relation" );
			$total = mysql_num_rows ( $result ); //查询所有数据
			$url = "ajax.php"; //获取本页url
			//页码计算
			$pagenum = ceil ( $total / $num ); //获得总页数,也是最后一页
			$page = min ( $pagenum, $page ); //获得首页
			$prepg = $page - 1; //上一页
			$nextpg = ($page == $pagenum ? 0 : $page + 1); //下一页
			$offset = ($page - 1) * $num;
			//开始分页导航代码
			$pagenav = "显示第<b>" . ($total ? ($offset + 1) : 0) . "<b>-<b>" . min ( $offset + 3, $total ) . "<b>条记录,共" . $total . "条记录$nbsp;";
			//如果只有一页则跳出函数
			if ($pagenum <= 1)
				return false;
			$pagenav .= "<a href=javascript:dopage('result','$url?page=1');>首页</a>--";
			if ($prepg)
				$pagenav .= "<a href=javascript:dopage('result','$url?page=$prepg');>前页--</a>";
			else
				$pagenav .= "前页--";
			if ($nextpg)
				$pagenav .= "<a href=javascript:dopage('result','$url?page=$nextpg');>后页--</a>";
			else
				$pagenav .= "后页--";
			$pagenav .= "<a href=javascript:dopage('result','$url?page=$pagenum');>尾页</a> ";
			$pagenav .= "</select>共" . $pagenum . "页";
			if ($page > $pagenum) { //假如传入的页面参数大于总页数  则现实错误信息
				echo "can not found the page" . $page;
				exit ();
			}
			$info = mysql_query ( "select * from help_relation limit $offset,$num" );
			echo "<table border=3>";
			while ( $it = mysql_fetch_array ( $info ) ) {
				echo "<tr>";
				echo "<td>" . $it ["help_topic_id"] . "</td>";
				echo "<td>" . $it ["help_keyword_id"] . "</td>";
				
				echo "</tr>";
			}
			echo "</table>";
			echo "<br>";
			echo $pagenav;
			?>
 </div>
</body>
</html>

 

 

var http_request=false;
function send_request(url)
{
 http_request=false;
 //开始初始化XMLHttp对象
 if(window.XMLHttpRequest){//MOILLA浏览器
  
    http_request=new XMLHttpRequest();
    if(http_request.overrideMimeType){
       http_request.overrideMimeType("text/xml");
     
     }
  
  }
 else if(window.ActiveXObject){//IE 浏览器
    try{
       http_request=new ActiveXObject("Msxml2.XMLHttp");
     }catch(e){
        try{
        http_request=new ActiveXObject("Microsoft.XMLHttp");
      }catch(e){}
      
      }
  
  }if(!http_request){
    window.alert("创建XMLHttp对象失败");
    return false;
   
   }
   http_request.onreadystatechange=processrequest;
   //确定发送请求方式,URL,及是否同步执行下段代码
   http_request.open("GET",url,true);
   http_request.send(null);
 
}
//处理返回信息函数
function processrequest(){
    if(http_request.readyState==4){
       if(http_request.status==200){
       document.getElementById(reobj).innerHTML=http_request.responseText;
     }else{
        alert("您所请求的页面不正常");
      }
     }
 }
 function dopage(obj,url){
     document.getElementById(obj).innerHTML="正在读取数据.....";
     send_request(url);
     reobj=obj;
  }

 

你可能感兴趣的:(Ajax)