当前位置:Gxlcms > html代码 > 利用html5Api实现分页的示例代码分享

利用html5Api实现分页的示例代码分享

时间:2021-07-01 10:21:17 帮助过:19人阅读

利用html5 Api实现分页的示例代码分享

  1. //htmlapi.php
  2. <?php
  3. //header("content-type:text/html;charset=utf-8");
  4. $dsn='mysql:host=127.0.0.1;dbname=edusoho';
  5. $db=new PDO($dsn,'root','root');
  6. $page=isset($_GET['p'])?$_GET['p']:'1';
  7. $pagesize='10';
  8. $limit=($page-1)*$pagesize;
  9. $sql='select count(*) as num from course_lesson_text';
  10. $rs=$db->query($sql);
  11. $ar=$rs->fetch();
  12. $count=$ar['num'];
  13. $pagecount=ceil($count/$pagesize);
  14. $sql="select id,title from course_lesson_text limit $limit,$pagesize";
  15. $rs=$db->query($sql);
  16. $arr=$rs->fetchAll();
  17. $next=$page+1>$pagecount?$pagecount:$page+1;
  18. $up=$page-1<1?1:$page-1;
  19. $pagea="<a class='pa' href='?p=1'>首页</a><a class='pa' href='?p=$up'>上一页</a>
  20. <a class='pa' href='?p=$next'>下一页</a><a class='pa' href='?p=$pagecount'>尾页</a>";
  21. $exit['arr']=$arr;
  22. $exit['page']=$pagea;
  23. exit(json_encode($exit));
  24. ?>
  25. //index.php
  26. <?php
  27. header("content-type:text/html;charset=utf-8");
  28. $dsn='mysql:host=127.0.0.1;dbname=edusoho';
  29. $db=new PDO($dsn,'root','root');
  30. $page=isset($_GET['p'])?$_GET['p']:'1';
  31. //$page=1;
  32. $pagesize='10';
  33. $limit=($page-1)*$pagesize;
  34. $sql='select count(*) as num from course_lesson_text';
  35. $rs=$db->query($sql);
  36. $ar=$rs->fetch();
  37. $count=$ar['num'];
  38. $pagecount=ceil($count/$pagesize);
  39. $sql="select id,title from course_lesson_text limit $limit,$pagesize";
  40. $rs=$db->query($sql);
  41. $arr=$rs->fetchAll();
  42. ?>
  43. <meta charset='utf-8'/>
  44. <ul class="list">
  45. <?php foreach ($arr as $key => $value) {
  46. ?>
  47. <li><?php echo $value['title'] ?></li>
  48. <?php
  49. } ?>
  50. </ul>
  51. <?php
  52. $next=$page+1>$pagecount?$pagecount:$page+1;
  53. $up=$page-1<1?1:$page-1;
  54. ?>
  55. <div id="pagea">
  56. <a class='pa' href="?p=1">首页</a>
  57. <a class='pa' href="?p=<?php echo $up ?>">上一页</a>
  58. <a class='pa' href="?p=<?php echo $next ?>">下一页</a>
  59. <?php
  60. /*for($i=1;$i<$pagecount;$i++){
  61. echo"<a class='pa' href='?p=$i'>$i</a>";
  62. }*/
  63. ?>
  64. <a class='pa' href="?p=<?php echo $pagecount ?>">尾页</a>
  65. </div>
  66. <script src='./jquery-1.8.0.js' ></script>
  67. <script>
  68. $(function(){
  69. var ajax,
  70. currentState;
  71. $(".pa").live("click",function(e){
  72. e.preventDefault();
  73. var target = e.target,
  74. url = $(target).attr("href");
  75. !$(this).hasClass("current") && $(this).addClass("current").siblings().removeClass("current");
  76. if(ajax == undefined) {
  77. currentState = {
  78. url: document.location.href,
  79. title: document.title,
  80. html: $(".list").html()
  81. };
  82. }
  83. ajax = $.ajax({
  84. type:"get",
  85. url: 'htmlapi.php'+url,
  86. dataType:"json",
  87. success: function(data){
  88. var html = "";
  89. if(data['arr'].length > 0) {
  90. $.each(data['arr'],function(k,v){
  91. html += "<li>"+v.title+"</li>"
  92. })
  93. $(".list").html(html);
  94. }
  95. var state = {
  96. url: url,
  97. title: document.title,
  98. html: $(".list").html()
  99. };
  100. $("#pagea").html(data['page']);
  101. history.pushState(state,null,url);
  102. }
  103. });
  104. });
  105. window.addEventListener("popstate",function(event){
  106. if(ajax == null){
  107. return;
  108. }else if(event && event.state){
  109. document.title = event.state.title;
  110. $(".list").html(event.state.html);
  111. }else{
  112. document.title = currentState.title;
  113. $(".list").html(currentState.html);
  114. }
  115. });
  116. });

以上就是利用html5 Api实现分页的示例代码分享的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行