zoukankan      html  css  js  c++  java
  • php AJAX 分页显示数据

    调试用的文件
    <html><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", "Example8_7_1.php", true);
    xmlReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlReq.send(formData);
    return false;
    }
    </script>
    <head>
    <body onLoad="viewpage(1)">
    <img src=top.gif width="100%">
    <h3 align="center">数据分页显示</h3>
    <div id="content2"></div>

    </body>

    调用的另外一个文件 Example8_7_1.php
    <?php
    header("Content-Type:text/html;charset=GB2312");
    $pagesize=10;
    //echo $_POST['page'];
    $db=mysql_connect("localhost","root",""); //创建数据库连接
    mysql_query("set character set utf-8");
    mysql_select_db("optics");
    $result = mysql_query("Select count(DISTINCT articleid) FROM product");
    $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 `id` , count( * ) from ".book." GROUP BY `id` order by id desc limit $offset,$pagesize");
    $result=mysql_query("select * from product GROUP BY `articleid` order by articleid desc limit $offset,$pagesize");
    $num = @mysql_num_rows($result);

    while ($row = @mysql_fetch_array($result,MYSQL_NUM)) {
    $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>";
    echo "<TD style=\"PADDING-TOP: 5px\" align=left><img src=".$smallpic[$a]."></a></TD>";
    echo "</TR>";
    }
    echo "</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>";


    ?>

    本文首发于http://www.corange.cn/archives/2009/04/3123.html

  • 相关阅读:
    腾讯云发布“创新成长快线”,首期向创业者赠送10亿分钟实时音视频时长
    Tencent Kona JDK11正式开源,腾讯大数据将持续贡献Java生态发展
    腾讯视频云勇夺云端视频转码大赛多项第一
    分享一些常用的开源博客社区网站
    分享些发表技术类文章的平台
    Linux之ps命令基本使用
    彻底卸载 Oracle11g r2 教程(亲测有效,已重装过)
    Oracle11g R2 安装教程(非常详细 )
    苹果CMS搭建影视网站教程
    JSON 基本使用
  • 原文地址:https://www.cnblogs.com/zerogo/p/1559051.html
Copyright © 2011-2022 走看看