我的PHP MySQL JavaScript加载更多分页问题
这是我的PHP MySQL HTML代码
<div class="postList">
<?php
$page = 1;
if(isset($_POST['page'])) {
$page = filter_input(INPUT_POST, 'page', FILTER_VALIDATE_INT);
if(false === $page) {
$page = 1;
}
}
// set the number of items to display per page
$items_per_page = 5;
// build query
$offset = ($page - 1) * $items_per_page;
$sql = "SELECT id,title,size,time,category,hash FROM data ORDER BY id DESC LIMIT " . $offset . "," . $items_per_page;
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($result)) {
$postID = $row['id'];
?>
<div class="list_item"><?php echo $row['title']; ?></div>
<?php } ?>
<div class="show_more_main" id="show_more_main<?php echo $postID; ?>">
<span id="<?php echo $postID; ?>" class="show_more" title="Load more data">Show more</span>
<span class="loding" style="display: none;"><span class="loding_txt">Loading...</span></span>
</div>
</div>
这是我的JavaScript代码
<script type="text/javascript">
$(document).ready(function(){
$(document).on('click','.show_more',function(){
var ID = $(this).attr('id');
$('.show_more').hide();
$('.loding').show();
$.ajax({
type:'POST',
url:'index.php',
data:'id='+ID +'&page='+<?php echo $offset;?>,
success:function(html){
$('#show_more_main'+ID).remove();
$('.postList').append(html);
}
});
});
});
</script>
默认情况下,我能够在屏幕上获得前5条记录,当我按“加载更多”按钮时,它再次为我提供相同的前5条记录;当我按“加载更多”按钮时,其第二次获得的前5条记录也保持不变像多循环一样反复记录。
我在这里做什么错了?