我使用以下代码进行ajax和php的分页。
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 id,name,message FROM paginate ORDER BY id ASC LIMIT $position, $item_per_page");
$results=array(array('id'=>1,'name'=>'joe','age'=>'22','job'=>'dev'),array('id'=>2,'name'=>'g','age'=>'21','job'=>'se'),array('id'=>3,'name'=>'gt','age'=>'21','job'=>'se'));
$output = array_slice($results, 0,1);
//output results from database
echo '<ul class="page_result">';
//while($row = mysqli_fetch_array($results))
foreach ($output as $row)
{
echo '<li id="item_'.$row["name"].'">'.$row["age"].'. <span class="page_name">'.$row["job"].'</span><span class="page_message">'.$row["name"].'</span></li>';
}
echo '</ul>';
?>
的index.php
<?php
include("config.inc.php");
//$results = mysqli_query($connecDB,"SELECT COUNT(*) FROM paginate");
$results=array(array('id'=>1,'name'=>'joe','age'=>'22','job'=>'dev'),array('id'=>2,'name'=>'g','age'=>'21','job'=>'se'),array('id'=>3,'name'=>'gt','age'=>'21','job'=>'se'));
//$get_total_rows = mysqli_fetch_array($results); //total records
//break total records into pages
//$pages = ceil($get_total_rows[0]/$item_per_page);
$pages = ceil(3/$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>';
}
?><!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-1)}, 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>
我希望数据源是代码中定义的数组,而不是mysql数据库,因此您将看到$results=array(array('name'=>'gowrie','age'=>'22','job'=>'dev'),array('name'=>'g','age'=>'21','job'=>'se'));
而不是mysql查询。
在fetchespages.php中应该有一个查询$results = mysqli_query($connecDB,"SELECT id,name,message FROM paginate ORDER BY id ASC LIMIT $position, $item_per_page");
,这会限制页面的结果,但我将其替换为$results=array(array('name'=>'joe','age'=>'22','job'=>'dev'),array('name'=>'g','age'=>'21','job'=>'se'));
页面显示正确的分页链接数,但是当我点击它们时,它们不起作用,它只是继续加载。我不确定我的index.php中的ajax是否存在问题,或者我尝试在fetch_pages.php中限制数组中的结果的方式,即$output = array_slice($results, 0,1);
当我点击分页链接时,它在页面上加载相同的东西。
我该如何解决这个问题?
答案 0 :(得分:0)
好吧,如果你使用
$ output = array_slice($ results,0,1);
无论您点击哪个页面链接,数组始终限于第一个key =&gt;值对。就像在sql查询中一样,每页使用limit和position / items,你必须对slice函数执行相同的操作。所以它变得像
$output = array_slice($results, $position, $items_per_page);
希望这有帮助