gpt4 book ai didi

PHP Ajax 分页数组不起作用

转载 作者:行者123 更新时间:2023-11-30 00:07:23 27 4
gpt4 key购买 nike

我使用以下代码通过 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); 当我单击分页链接时,它会在页面上加载相同的内容。

如何解决这个问题?

最佳答案

如果你使用

$output = array_slice($results, 0,1);

无论您单击哪个页面链接,数组始终仅限于第一个键=>值对。就像在 sql 查询中一样,您使用限制和每页的位置/项目,您必须对切片函数执行相同的操作。所以它变成了这样的东西

$output = array_slice($results, $position, $items_per_page);

希望这有帮助

关于PHP Ajax 分页数组不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24410915/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com