日期:2014-05-17  浏览次数:20440 次

PHP+AJAX分页
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
</head> 
<script> 
function viewpage(p){ 
if(window.XMLHttpRequest){ 
var xmlReq = new XMLHttpRequest(); 
} else if(window.ActiveXObject) { 
var xmlReq = new ActiveXObject('Microsoft.XMLHTTP'); 

var formData = "page="+p; 
xmlReq.onreadystatechange = function(){ 
if(xmlReq.readyState == 4){ 
document.getElementById('content2').innerHTML = xmlReq.responseText; 


xmlReq.open("post", "d.php", true); 
xmlReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
xmlReq.send(formData); 
return false; 

</script> 
<?php 
#header("Content-Type:text/html;charset=GB2312"); 
$pagesize=10; 
//echo $_POST['page']; 
$db=mysql_connect("localhost","root",""); //创建数据库连接 
mysql_query("set names gbk"); 
mysql_select_db("pagelist"); 
$result = mysql_query("select * FROM sheet1",$db); 
$myrow = mysql_fetch_array($result); 
$numrows=mysql_num_rows($result); 
$pages=intval($numrows/$pagesize); 
if ($numrows%$pagesize) 
$pages++; 
if (isset($_POST['page'])){ 
$page=intval($_POST['page']); 

else{ 
//设置为第一页 
$page=1; 

$first=1; 
$prev=$page-1; 
$next=$page+1; 
$last=$pages; 
//计算记录偏移量 
$offset=$pagesize*($page - 1); 
//读取指定记录数 

//$result=mysql_query("select `id` , count( * ) from ".book." GROUP BY `id` order by id desc limit $offset,$pagesize"); 
$result=mysql_query("select * from sheet1 limit $offset,$pagesize"); 
$num = mysql_num_rows($result); 
while ($row = mysql_fetch_array($result,$db)) { 
$hotelname[] = $row[0]; 
$name[]=$row[1]; 
$author[]=$row[2]; 
$publisher[]=$row[3]; 
$isbn[]=$row[4]; 
$type[]=$row[5]; 
$smallpic[]=$row[6]; 
//$countpeople[] = $row[1]; 

echo "<TABLE style=\"MARGIN-BOTTOM: 20px\" cellSpacing=0 cellPadding=0 width=90% border=1 align=center>\n"; 
echo "<TBODY>\n"; 
echo "<th>ID编号</th><th>名称</th><th>作者</th><th>出版社</th><th>ISBN号</th><th>类型</th><th>价格</th>"; 
for($a=0;$a<$num;$a++) 

echo "<TR>\n"; 
echo "<TD style=\"PADDING-TOP: 5px\" align=left>".$hotelname[$a]."</TD>"; 
echo "<TD style=\"PADDING-TOP: 5px\" align=left>".$name[$a]."</TD>"; 
echo "<TD style=\"PADDING-TOP: 5px\" align=left>".$author[$a]."</TD>"; 
echo "<TD style=\"PADDING-TOP: 5px\" align=left>".$publisher[$a]."</TD>"; 
echo "<TD style=\"PADDING-TOP: 5px\" align=left>".$isbn[$a]."</TD>"; 
echo "<TD style=\"PADDING-TOP: 5px\" align=left>".$type[$a]."</TD