此代码完美无缺。但我无法获得该表的最后一行。 例如:我的表有9行,但这只有8行,缺少1行。当我使用ASC或DESC时,缺少的行是最后一行。 这是我的代码.. 抱歉我的英语不好。 谢谢!
的index.php
<?php
$db_username = 'root';
$db_password = '';
$db_name = 'ad_man';
$db_host = 'localhost';
$item_per_page = 2;
$connecDB = mysqli_connect($db_host, $db_username, $db_password,$db_name)or die('could not connect to database');
if($check_ad = mysqli_query($connecDB,"SELECT ad_uid FROM fullbanner WHERE ad_uid='501'")){
$countr=mysqli_num_rows($check_ad);
if($countr>=1){
$pages = $countr/$item_per_page;
//create pagination
if($pages > 1)
{
$pagination = '';
$pagination .= '<ul class="paginate">';
for($i = 1; $i<$pages; $i++)
{
$pagination .= '<li><a href="#" class="paginate_click" id="'.$i.'-page">'.$i.'</a></li>';
}
$pagination .= '</ul>';
}
}else {$pagination="<ul class='paginate'>Empty ADs</ul>";}}else {$pagination="Empty ADs";}//mama
?><!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ajax Pagination</title>
<script type="text/javascript" src="js/jquery-1.9.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#results").load("fetch_pages.php", {'page':0}, function() {$("#1-page").addClass('active');}); //initial page number to load
$(".paginate_click").click(function (e) {
$("#results").prepend('<div class="loading-indication"><img src="ajax-loader.gif" /> Loading...</div>');
var clicked_id = $(this).attr("id").split("-"); //ID of clicked element, split() to get page number.
var page_num = parseInt(clicked_id[0]); //clicked_id[0] holds the page number we need
$('.paginate_click').removeClass('active'); //remove any active class
//post page number and load returned data into result element
//notice (page_num-1), subtract 1 to get actual starting point
$("#results").load("fetch_pages.php", {'page':(page_num-0)}, function(){
});
$(this).addClass('active'); //add active class to currently clicked element (style purpose)
return false; //prevent going to herf link
});
});
</script>
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="results"></div>
<?php echo $pagination; ?>
</body>
</html>
fetch_pages.php
<?php
include("config.inc.php"); //include config file
//sanitize post value
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
//validate page number is really numaric
if(!is_numeric($page_number)){die('Invalid page number!');}
//get current starting point of records
$position = ($page_number * $item_per_page);
//Limit our results within a specified range.
$results = mysqli_query($connecDB,"SELECT * FROM fullbanner WHERE ad_uid='501' ORDER BY ad_count ASC LIMIT $position, $item_per_page");
//output results from database
echo '<ul class="page_result">';
while($row = mysqli_fetch_array($results))
{
echo '<li id="item_'.$row["ad_count"].'">'.$row["ad_code"].'. <span class="page_name">'.$row["ad_title"].'</span><span class="page_message"><a href="../new/image_ads/'.$row["image_url"].'" target="_new"><< Viwe AD image >></a></span></li>';
}
echo '</ul>';
?>
谢谢你!
答案 0 :(得分:0)
检查页码是否等于0,如果不简单地将$ position除以2
//get current starting point of records
if ($position === 0) $position = $position;
else $position = ($position/2)
这对我有用