本文是一个php+ajax实现的分页示例程序,感兴趣的同学研究一下他的分页思路学习下.
1.用ajax post数据到后台页面后,接着要重新连接数据库,别以为用之前的session连接过就可以了
2.为了处理返回乱码的问题,我添加了header("Content-Type:text/html;charset=GB2312");就可以正常显示了,后来在firefox下检验,却提示我下载这个网页,上网搜了不少资料,得到一个模糊的认识就是网页代码有语法错误,firefox为了安全起见不会直接显示而是提示下载,我重新检查了刚才那条语句,发现自己多写了个“”,把它去掉后问题就解决了,哈哈,所以遇到这样的问题,好好检查一下html tag吧,毕竟firefox可不像ie那样smart
3.最后说一句,做web site的开发者,要负责任,别以为在ie下测试通过就万事大吉,毕竟不是所有人都用ie,还得要在别的浏览器下多做测试,这样才显示出你的专业水准
ajax脚本:
<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", "hotel_list.php", true);
xmlReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlReq.send(formData);
return false;
}
</script>
调用:
header("Content-Type:text/html;charset=GB2312");
$pagesize=10;
//echo $_POST['page'];
$result = mysql_query("Select count(DISTINCT hotelname) FROM ".TBL_HOTELS);
$myrow = mysql_fetch_array($result);
$numrows=$myrow[0];
$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 `hotelname` , count( * ) from ".TBL_HOTELS." GROUP BY `hotelname` order by id desc limit $offset,$pagesize");
$num = mysql_num_rows($result);
while ($row = mysql_fetch_array($result,MYSQL_NUM)) {
$hotelname[] = $row[0];
$countpeople[] = $row[1];
}
for($a=0;$a<$num;$a++)
{
//$result=mysql_query("select count(title) from " . TBL_Comments ." where `title`="".$title[$a].""");
//$row = mysql_fetch_row($result);
echo "<TABLE style="MARGIN-BOTTOM: 20px" cellSpacing=0 cellPadding=0 width=100% border=0>n";
echo "<TBODY>n";
echo "<TR>n";
echo "<TD style="PADDING-TOP: 5px" vAlign=top align=left width=80>n";
//rating_bar($title[$a],5);
echo "</TD>n";
echo "<TD style="PADDING-TOP: 5px" align=left width=100%><A title=$hotelname[$a] style="FONT-SIZE: 14px" href=#>$hotelname[$a]</A>n";
echo "</TD></TR>n";
echo " <TR>n";
echo "<TD></TD>n";
echo "<TD style="PADDING-LEFT: 0px">n";
echo "<IMG src="images/comment.gif" border=0> 推荐人数:($countpeople[$a]) |n";
echo "<SPAN>平均分:<STRONG></STRONG> (".$count."票) | 评论数:()</SPAN>n";
echo "</TD></TR></TBODY></TABLE>n";
}
echo "<TABLE style="MARGIN-TOP: 30px" cellSpacing=0 cellPadding=0 width="100%"";
echo "border=0>";
echo "<TBODY><TR><TD colSpan=3 height=20>";
echo "<DIV align=center>";
echo "<P align=left><FONT color=red>第".$page."页/总".$pages."页 | 总".$numrows."条</FONT> | ";
if ($page>1) echo "<a οnclick="viewpage(".$first.")" href='#'>首页</a> | ";
if ($page>1) echo "<a οnclick="viewpage(".$prev.")" href='#'>上页</a> | ";
if ($page<$pages) echo "<a οnclick="viewpage(".$next.")" href='#'>下页</a> | ";
if ($page<$pages) echo "<a οnclick="viewpage(".$last.")" href='#'>尾页</a>";
echo "转到第 <INPUT maxLength=3 size=3 value=1 name=goto_page> 页 <INPUT hideFocus οnclick="viewpage(document.all.goto_page.value)" type=button value=Go name=cmd_goto>";
echo "</P></DIV></TD></TR></TBODY></TABLE>";
$pagesize=10;
//echo $_POST['page'];
$result = mysql_query("Select count(DISTINCT hotelname) FROM ".TBL_HOTELS);
$myrow = mysql_fetch_array($result);
$numrows=$myrow[0];
$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 `hotelname` , count( * ) from ".TBL_HOTELS." GROUP BY `hotelname` order by id desc limit $offset,$pagesize");
$num = mysql_num_rows($result);
while ($row = mysql_fetch_array($result,MYSQL_NUM)) {
$hotelname[] = $row[0];
$countpeople[] = $row[1];
}
for($a=0;$a<$num;$a++)
{
//$result=mysql_query("select count(title) from " . TBL_Comments ." where `title`="".$title[$a].""");
//$row = mysql_fetch_row($result);
echo "<TABLE style="MARGIN-BOTTOM: 20px" cellSpacing=0 cellPadding=0 width=100% border=0>n";
echo "<TBODY>n";
echo "<TR>n";
echo "<TD style="PADDING-TOP: 5px" vAlign=top align=left width=80>n";
//rating_bar($title[$a],5);
echo "</TD>n";
echo "<TD style="PADDING-TOP: 5px" align=left width=100%><A title=$hotelname[$a] style="FONT-SIZE: 14px" href=#>$hotelname[$a]</A>n";
echo "</TD></TR>n";
echo " <TR>n";
echo "<TD></TD>n";
echo "<TD style="PADDING-LEFT: 0px">n";
echo "<IMG src="images/comment.gif" border=0> 推荐人数:($countpeople[$a]) |n";
echo "<SPAN>平均分:<STRONG></STRONG> (".$count."票) | 评论数:()</SPAN>n";
echo "</TD></TR></TBODY></TABLE>n";
}
echo "<TABLE style="MARGIN-TOP: 30px" cellSpacing=0 cellPadding=0 width="100%"";
echo "border=0>";
echo "<TBODY><TR><TD colSpan=3 height=20>";
echo "<DIV align=center>";
echo "<P align=left><FONT color=red>第".$page."页/总".$pages."页 | 总".$numrows."条</FONT> | ";
if ($page>1) echo "<a οnclick="viewpage(".$first.")" href='#'>首页</a> | ";
if ($page>1) echo "<a οnclick="viewpage(".$prev.")" href='#'>上页</a> | ";
if ($page<$pages) echo "<a οnclick="viewpage(".$next.")" href='#'>下页</a> | ";
if ($page<$pages) echo "<a οnclick="viewpage(".$last.")" href='#'>尾页</a>";
echo "转到第 <INPUT maxLength=3 size=3 value=1 name=goto_page> 页 <INPUT hideFocus οnclick="viewpage(document.all.goto_page.value)" type=button value=Go name=cmd_goto>";
echo "</P></DIV></TD></TR></TBODY></TABLE>";
原文地址:http://www.adminso.com/articles/view/92231
(站长搜索- http://www.adminso.com/articles -资讯,中国最具专业的站长资讯新闻频道,报道国内外动态权威的站长资讯动向,关注新闻,透视事件热点资讯。)