jquery ajax无刷新翻页 分页
freejs.net已经发了不少无刷新翻页的代码,都比较实用,这边这个非常简单,适合学习或者评论使用
搜索本站可以找到更多分页代码
注意本例jq版本为1.4.3
XML/HTML Code
- <script>
- $(function(){
- $("#content").load("view-comments.php");
- //PAGE NUMBER onClick FUNCTION
- $(".page").live("click", function(){
- var page = $(this).attr("id");
- $("#content").load("view-comments.php?page="+page);
- });
- });
- </script>
- <style>
- .page:hover {
- cursor: pointer;
- text-decoration: underline;
- }
- .pagenum {
- color: #32baed;
- }
- .content {
- width: 600px;
- }
- .content td {
- width: 50%;
- }
- </style>
- <div id="content" style="padding-left:100px;"></div>
view-comments.php
PHP Code
- <?php
- //FILE WITH THE IMPORTANT MySQL INFO
- include 'conn.php';
- //PAGE NUMBER, RESULTS PER PAGE, AND OFFSET OF THE RESULTS
- if($_GET["page"]){
- $pagenum = $_GET["page"];
- } else {
- $pagenum = 1;
- }
- $rowsperpage = 3; //MAXIMUM RESULTS PER PAGE
- $offset = ($pagenum - 1) * $rowsperpage; //WHERE THE RESULTS START FROM
- $table="content";
- //FOR RESULTS OF THE PAGE
- $q = mysql_query("SELECT * FROM $table ORDER BY id LIMIT $offset, $rowsperpage");
- $page_nums = mysql_num_rows($q); //NUMBER OF RESULTS FOR THE PAGE
- $total_q = mysql_query("SELECT * FROM $table"); //FOR THE ALL RESULTS
- $total_nums = mysql_num_rows($total_q); //TOTAL NUMBER OF RESULTS
- $total_pages = ceil($total_nums/$rowsperpage); //NUMBER OF PAGES
- //IF PAGE NUMBER IS WITHIN THE FIRST AND LAST PAGES
- if($pagenum>=1&&$pagenum<=$total_pages)
- {
- while($r=mysql_fetch_array($q))
- {
- $content = $r["name"];
- echo '<table class="content"><tr><td>'.$content.'</td></tr></table>';
- }
- echo '<br>';
- //IF ANY RESULTS
- if($total_nums)
- {
- $range = 2; //NUMBER OF PAGES TO BE SHOWN BEFORE AND AFTER THE CURRENT PAGE NUMBER
- //FIRST, PREVIOUS, NEXT, AND LAST LINKS
- if($pagenum>1)
- {
- $page = $pagenum - 1;
- $first = '<a class="page" id="1">First</a> ';
- $prev = '<a class="page" id="'.$page.'">Prev</a> ';
- }
- if($pagenum<$total_pages)
- {
- $page = $pagenum + 1;
- $next = '<a class="page" id="'.$page.'">Next</a> ';
- $last = '<a class="page" id="'.$total_pages.'">Last</a> ';
- }
- //PAGINATION
- for($page=($pagenum-$range); $page<=($pagenum+$range); $page++)
- {
- if($page>=1&&$page<=$total_pages)
- {
- if($page==$pagenum)
- {
- $nav .= '<span class="pagenum">'.$page.'</span> ';
- }
- else
- {
- $nav .= '<a class="page" id="'.$page.'">'.$page.'</a> ';
- }
- }
- }
- }
- //DISPAYS IN HTML
- echo $first . $prev . $nav . $next . $last;
- } else {
- //OTHERWISE...
- header("Location: view-comments.php"); //WILL REDIRECT TO THE FIRST PAGE OF RESULTS
- }
- ?>
原文地址:http://www.freejs.net/article_fenye_101.html