jquery 无刷新翻页
本例与《jquery 翻页分页》的区别在于本例更实用,翻页数字太多的时候会隐藏中间的部分。数据库与《jquery 翻页分页》的数据库相同
js代码
JavaScript Code复制内容到剪贴板
- <script>
- $(document).ready(function(){
- $(window).bind("load", function() {
- page = 1;
- limit = 2;
- startpoint = (page * limit) - limit;
- statement = 'content where id>0';
- query_string = 'query=' + statement + '&per_page=' + limit + '&page=' + page;
- $.get('function.php', query_string, function(data){
- var sHTML = data;
- $('#pagination_div').html(sHTML);
- });
- query_string = 'query=' + statement + '&startpoint=' + startpoint + '&limit=' + limit;
- $.get('function.php', query_string, function(data){
- var sHTML = data;
- $('.records').html(sHTML);
- });
- }); //end load
- $(document).on("click", "ul.pagination li a", function(){
- //alert($(this).attr('title'));
- if ($(this).attr('title') != 'current')
- {
- page = $(this).attr('title');
- limit = 2;
- startpoint = (page * limit) - limit;
- statement = 'content where id>0';
- query_string = 'query=' + statement + '&per_page=' + limit + '&page=' + page;
- $.get('function.php', query_string, function(data){
- var sHTML = data;
- $('#pagination_div').html(sHTML);
- });
- query_string = 'query=' + statement + '&startpoint=' + startpoint + '&limit=' + limit;
- $.get('function.php', query_string, function(data){
- var sHTML = data;
- $('.records').html(sHTML);
- });
- }
- return false;
- }); //end event
- }); //end document ready
- </script>
html代码
XML/HTML Code复制内容到剪贴板
- <div class="records round">
- </div>
- <div id="pagination_div">
- </div>
function.php
PHP Code复制内容到剪贴板
- <?php
- function pagination($query, $per_page = 10,$page = 1, $url = '?'){
- include 'conn.php';
- $query = "SELECT COUNT(*) as num FROM {$query}";
- $result = mysql_query($query);
- if (!$result)
- {
- echo 'Error ' . mysqi_error($link);
- exit();
- }
- while ($row = mysql_fetch_array($result))
- {
- $total = $row['num'];
- }
- $adjacents = "2";
- $page = ($page == 0 ? 1 : $page);
- $start = ($page - 1) * $per_page;
- $firstPage = 1;
- $prev = $page - 1;
- $next = $page + 1;
- $lastpage = ceil($total/$per_page);
- $lpm1 = $lastpage - 1;
- $pagination = "";
- if($lastpage > 1)
- {
- $pagination .= "<ul class='pagination'>";
- $pagination .= "<li class='details'>$page/$lastpage</li>";
- $prev = ($page == 1)?1:$page - 1;
- //$pagination = '';
- if ($page == 1)
- {
- $pagination.= "<li><a class='current' title='current'>First</a></li>";
- $pagination.= "<li><a class='current' title='current'>Prev</a></li>";
- }
- else
- {
- $pagination.= "<li><a href='{$url}page=$firstPage' title='$firstPage'>First</a></li>";
- $pagination.= "<li><a href='{$url}page=$prev' title='$prev'>Prev</a></li>";
- }
- if ($lastpage < 7 + ($adjacents * 2))
- {
- for ($counter = 1; $counter <= $lastpage; $counter++)
- {
- if ($counter == $page)
- $pagination.= "<li><a class='current' title='current'>$counter</a></li>";
- else
- $pagination.= "<li><a href='{$url}page=$counter' title='$counter'>$counter</a></li>";
- }
- }
- elseif($lastpage > 5 + ($adjacents * 2))
- {
- if($page < 1 + ($adjacents * 2))
- {
- for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
- {
- if ($counter == $page)
- $pagination.= "<li><a class='current' title='current'>$counter</a></li>";
- else
- $pagination.= "<li><a href='{$url}page=$counter' title='$counter'>$counter</a></li>";
- }
- $pagination.= "<li class='dot'>...</li>";
- $pagination.= "<li><a href='{$url}page=$lpm1' title='$lpm1'>$lpm1</a></li>";
- $pagination.= "<li><a href='{$url}page=$lastpage' title='$lastpage'>$lastpage</a></li>";
- }
- elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
- {
- $pagination.= "<li><a href='{$url}page=1' title='1'>1</a></li>";
- $pagination.= "<li><a href='{$url}page=2' title='2'>2</a></li>";
- $pagination.= "<li class='dot'>...</li>";
- for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
- {
- if ($counter == $page)
- $pagination.= "<li><a class='current' title='current'>$counter</a></li>";
- else
- $pagination.= "<li><a href='{$url}page=$counter' title='$counter'>$counter</a></li>";
- }
- $pagination.= "<li class='dot'>..</li>";
- $pagination.= "<li><a href='{$url}page=$lpm1' title='$lpm1'>$lpm1</a></li>";
- $pagination.= "<li><a href='{$url}page=$lastpage' title='$lastpage'>$lastpage</a></li>";
- }
- else
- {
- $pagination.= "<li><a href='{$url}page=1' title='1'>1</a></li>";
- $pagination.= "<li><a href='{$url}page=2' title='2'>2</a></li>";
- $pagination.= "<li class='dot'>..</li>";
- for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
- {
- if ($counter == $page)
- $pagination.= "<li><a class='current' title='current'>$counter</a></li>";
- else
- $pagination.= "<li><a href='{$url}page=$counter' title='$counter'>$counter</a></li>";
- }
- }
- }
- if ($page < $counter - 1){
- $pagination.= "<li><a href='{$url}page=$next' title='$next'>Next</a></li>";
- $pagination.= "<li><a href='{$url}page=$lastpage' title='$lastpage'>Last</a></li>";
- }else{
- $pagination.= "<li><a class='current' title='current'>Next</a></li>";
- $pagination.= "<li><a class='current' title='current'>Last</a></li>";
- }
- $pagination.= "</ul>n";
- }
- return $pagination;
- }
- //code to create pagination links
- if (isset($_GET['query']) and isset($_GET['per_page'])){
- $query = $_GET['query'];
- $per_page = $_GET['per_page'];
- $page = $_GET['page'];
- echo pagination($query, $per_page, $page);
- }
- //code for retrieving the records
- if(isset($_GET['startpoint']))
- {
- include '../../conn.php';
- $records = '';
- $statement = mysql_real_escape_string($_GET['query']);
- $startpoint = mysql_real_escape_string($_GET['startpoint']);
- $limit = mysql_real_escape_string($_GET['limit']);
- //show records
- $query = "SELECT * FROM {$statement} LIMIT {$startpoint} , {$limit}";
- $result = mysql_query($query);
- if (!$result)
- {
- echo 'Error ' . mysql_error($link);
- }
- while ($row = mysql_fetch_array($result)) {
- $records = $records . "<div class='record round'>{$row['id']}#{$row['name']}</div>";
- }
- echo $records;
- }
- ?>
原文地址:http://www.freejs.net/article_fenye_19.html