php,ajax实现分页
程序员文章站
2023-09-09 21:18:14
自己总结了些屁经验 1.用ajax post数据到后台页面后,接着要重新连接数据库,别以为用之前的session连接过就可以了 2.为了处理返回乱码的问题,我添加...
自己总结了些屁经验
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 onclick=\"viewpage(".$first.")\" href='#'>首页</a> | ";
if ($page>1) echo "<a onclick=\"viewpage(".$prev.")\" href='#'>上页</a> | ";
if ($page<$pages) echo "<a onclick=\"viewpage(".$next.")\" href='#'>下页</a> | ";
if ($page<$pages) echo "<a onclick=\"viewpage(".$last.")\" href='#'>尾页</a>";
echo "转到第 <input maxlength=3 size=3 value=1 name=goto_page> 页 <input hidefocus onclick=\"viewpage(document.all.goto_page.value)\" type=button value=go name=cmd_goto>";
echo "</p></div></td></tr></tbody></table>";
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 onclick=\"viewpage(".$first.")\" href='#'>首页</a> | ";
if ($page>1) echo "<a onclick=\"viewpage(".$prev.")\" href='#'>上页</a> | ";
if ($page<$pages) echo "<a onclick=\"viewpage(".$next.")\" href='#'>下页</a> | ";
if ($page<$pages) echo "<a onclick=\"viewpage(".$last.")\" href='#'>尾页</a>";
echo "转到第 <input maxlength=3 size=3 value=1 name=goto_page> 页 <input hidefocus onclick=\"viewpage(document.all.goto_page.value)\" type=button value=go name=cmd_goto>";
echo "</p></div></td></tr></tbody></table>";
上一篇: linux进行硬盘分区挂载的实现方法