温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

利用html5 Api 实现分页

发布时间:2020-02-20 22:05:48 来源:网络 阅读:693 作者:xtyz08gu 栏目:移动开发

//htmlapi.php

<?php

    //header("content-type:text/html;charset=utf-8");

    $dsn='mysql:host=127.0.0.1;dbname=edusoho';

    $db=new PDO($dsn,'root','root');

    $page=isset($_GET['p'])?$_GET['p']:'1';

    $pagesize='10';

    $limit=($page-1)*$pagesize;

    $sql='select count(*) as num  from course_lesson_text';

    $rs=$db->query($sql);

    $ar=$rs->fetch();

    $count=$ar['num'];

    $pagecount=ceil($count/$pagesize);

    $sql="select id,title from course_lesson_text limit $limit,$pagesize";

    $rs=$db->query($sql);

    $arr=$rs->fetchAll();

    $next=$page+1>$pagecount?$pagecount:$page+1;

        $up=$page-1<1?1:$page-1;

    $pagea="<a class='pa' href='?p=1'>首页</a><a class='pa' href='?p=$up'>上一页</a><a class='pa' href='?p=$next'>下一页</a><a class='pa' href='?p=$pagecount'>尾页</a>";

    $exit['arr']=$arr;

    $exit['page']=$pagea;

    exit(json_encode($exit));

 ?>

//index.php

<?php

    header("content-type:text/html;charset=utf-8");

    $dsn='mysql:host=127.0.0.1;dbname=edusoho';

    $db=new PDO($dsn,'root','root');

    $page=isset($_GET['p'])?$_GET['p']:'1';

  //$page=1;

    $pagesize='10';

    $limit=($page-1)*$pagesize;

    $sql='select count(*) as num  from course_lesson_text';

    $rs=$db->query($sql);

    $ar=$rs->fetch();

    $count=$ar['num'];

    $pagecount=ceil($count/$pagesize);

    $sql="select id,title from course_lesson_text limit $limit,$pagesize";

    $rs=$db->query($sql);

    $arr=$rs->fetchAll();

    ?>

    <meta charset='utf-8'/>

    <ul class="list">

        <?php foreach ($arr as $key => $value) {

            ?>

            <li><?php echo $value['title'] ?></li>

            <?php

        } ?>

    </ul>

    <?php 

        $next=$page+1>$pagecount?$pagecount:$page+1;

        $up=$page-1<1?1:$page-1;

    ?>

    <div id="pagea">

    <a class='pa' href="?p=1">首页</a>

     <a class='pa' href="?p=<?php echo $up ?>">上一页</a>

    <a class='pa' href="?p=<?php echo $next ?>">下一页</a>

    <?php

        /*for($i=1;$i<$pagecount;$i++){

            echo"<a class='pa' href='?p=$i'>$i</a>";

        }*/

    ?>

    <a class='pa' href="?p=<?php echo $pagecount ?>">尾页</a>

    </div>

    <script src='./jquery-1.8.0.js' ></script>

    <script>

$(function(){

    var ajax,

        currentState;

$(".pa").live("click",function(e){

        e.preventDefault();

        var target = e.target,

           url = $(target).attr("href");

        !$(this).hasClass("current") && $(this).addClass("current").siblings().removeClass("current");

         if(ajax == undefined) {

            currentState = {

                url: document.location.href,

                title: document.title,

                html: $(".list").html()

            };

        }

        ajax = $.ajax({

                type:"get",

                url: 'htmlapi.php'+url,

                dataType:"json",

                success: function(data){

                    var html = "";

                    if(data['arr'].length > 0) {

                        $.each(data['arr'],function(k,v){

                            html += "<li>"+v.title+"</li>"

                        })

                        $(".list").html(html);  

                    }

                    var state = {

                        url: url,

                        title: document.title,

                        html: $(".list").html()

                    };

                    $("#pagea").html(data['page']);

                    history.pushState(state,null,url);

                }

        });

    });

   window.addEventListener("popstate",function(event){

        if(ajax == null){

                return;

            }else if(event && event.state){

                document.title = event.state.title;

                $(".list").html(event.state.html);

            }else{

                document.title = currentState.title;

                $(".list").html(currentState.html);

            }

    });

 });


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI