博主信息
博文 19
粉丝 0
评论 0
访问量 16092
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
Ajax分页技术---2019年4月19号
倪偌卟離
原创
884人浏览过

get_mov.php代码

实例

<?php
$dsn='mysql:host=127.0.0.1;dbname=xuexi;charset=utf8;port=3306';
$dbname='root';
$password='root';
try{
    $pdo=new PDO($dsn,$dbname,$password);
}catch(PDOException $e){
    print_r($e->getFile());
    exit;
}
//print_r($pdo);
//返回总页数
$sql='SELECT `mov_id` FROM `movies`';
$stmt=$pdo->prepare($sql);
$stmt->execute();
$movies=$stmt->fetchAll(PDO::FETCH_ASSOC);
$num=5;
$pages=CEIL(count($movies)/$num);

//返回每页的数据
$page=$_GET['p'];
$offset=($page-1)*$num;

$sql="SELECT `mov_id`,`mov_name`,`detail` FROM `movies` LIMIT {$offset},{$num}";
$stmt=$pdo->prepare($sql);
$stmt->execute();
$movies=$stmt->fetchAll(PDO::FETCH_ASSOC);
//print_r($movies);
//转换为JSON数据对象
echo json_encode([$pages,$movies]);
?>

运行实例 »

点击 "运行实例" 按钮查看在线实例

show.php代码

实例

<?php

?>
<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>分页技术</title>
</head>
<style>
    .clear{clear:both;}
    table{border-collapse: collapse;width: 50%;margin:50px auto;text-align: center;}
    thead{font-weight: bold;}
    td{border:1px solid #ccc;}
    ul{width: 50%;margin:0 auto;}
    li{list-style: none;float:left;width: 20px;font-size: 16px;cursor: pointer;}
</style>
<body>
<table>
    <thead>
        <tr>
            <td style="width:20%">编码</td>
            <td style="width: 30%">名称</td>
            <td style="width: 50%">介绍</td>
        </tr>
    </thead>
    <tbody></tbody>
</table>
<ul>
    <div class="clear"></div>
</ul>
    <script>

        var tbody=document.getElementsByTagName('tbody')[0];
        // console.log(tbody);
        var request=new XMLHttpRequest();
        request.open('GET','get_mov.php?p=<?=$_GET["p"]?:1;?>',true);
        request.send(null);

        request.onreadystatechange=function(){
            if(request.readyState===4){
                //代入数据
                var date=JSON.parse(request.responseText);
                console.log(date);
                date[1].forEach(function (value) {
                    console.log(value);
                    var tr=document.createElement("tr");
                    for(var key in value){
                        var td=document.createElement("td");
                        td.innerText=value[key];
                        tr.appendChild(td);
                    }
                    tbody.appendChild(tr);
                });
                //分页
                var ul=document.getElementsByTagName('ul')[0];
                for(var i=0;i<date[0];i++){
                    var li=document.createElement('li');
                    li.innerText=i+1;
                    li.onclick=function(){
                        var search = location.search.slice(0,3) + this.innerText;
                        location.replace(search);
                    };
                    ul.appendChild(li);
                }

            }

        }


    </script>

</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


批改状态:未批改

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学