Helly伙计们!我已经构建了一个应用程序,每2-3分钟从多个RSS源中检索新项目,并将它们存储到数据库中。我试图使用'无限滚动'效果(使用jQuery)显示数据库的内容。一切正常,除了几页“页面/滚动”后,它有时会显示重复的条目,因为数据库不断更新新内容。
以下是我正在使用的文件(改编自Mik Ted's solution for Wordpress):
的index.php
<html>
<head>
<title>Scroll Pagination</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="jquery.js"> </script>
<script src="javascript.js"> </script>
<script type="text/javascript" src="//use.typekit.net/vue1oix.js"></script>
<script type="text/javascript">try{Typekit.load();}catch(e){}</script>
<style type="text/css"></style>
<script>
$(document).ready(function() {
$('#content').scrollPagination({
nop : 10, // The number of posts per scroll to be loaded
offset : 0, // Initial offset, begins at 0 in this case
error : 'No More Posts!', // When the user reaches the end this is the message that is
// displayed. You can change this if you want.
delay : 500, // When you scroll down the posts will load after a delayed amount of time.
// This is mainly for usability concerns. You can alter this as you see fit
scroll : true // The main bit, if set to false posts will not load as the user scrolls.
// but will still load if the user clicks.
});
});
</script>
</head>
<body>
<div id="content"></div>
</body>
</html>
Ajax.php
<?php
require_once('inc/conn.php');
$offset = is_numeric($_POST['offset']) ? $_POST['offset'] : die();
$postnumbers = is_numeric($_POST['number']) ? $_POST['number'] : die();
$query = "SELECT * FROM News ORDER BY ID DESC LIMIT ".$postnumbers." OFFSET ".$offset;
if ($result = mysqli_query($mysqli, $query))
{
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result))
{
echo '<h1>' . $row["ID"] . ' - ' . $row["Titlu"] . '(' . $row["Data_citire"] .')</h1>';
echo '<h3>' . $row["Sursa"] . '</h3>';
}
}
/* free result set */
mysqli_free_result($result);
?>
Javascript.js
function($) {
$.fn.scrollPagination = function(options) {
var settings = {
nop : 10, // The number of posts per scroll to be loaded
offset : 0, // Initial offset, begins at 0 in this case
error : 'No More Posts!', // When the user reaches the end this is the message that is
// displayed. You can change this if you want.
delay : 500, // When you scroll down the posts will load after a delayed amount of time.
// This is mainly for usability concerns. You can alter this as you see fit
scroll : true // The main bit, if set to false posts will not load as the user scrolls.
// but will still load if the user clicks.
}
// Extend the options so they work with the plugin
if(options) {
$.extend(settings, options);
}
// For each so that we keep chainability.
return this.each(function() {
// Some variables
$this = $(this);
$settings = settings;
var offset = $settings.offset;
var busy = false; // Checks if the scroll action is happening
// so we don't run it multiple times
// Custom messages based on settings
if($settings.scroll == true) $initmessage = 'Scroll for more or click here';
else $initmessage = 'Click for more';
// Append custom messages and extra UI
$this.append('<div class="content"></div><div class="loading-bar">'+$initmessage+'</div>');
function getData() {
// Post data to ajax.php
$.post('ajax.php', {
action : 'scrollpagination',
number : $settings.nop,
offset : offset,
}, function(data) {
// Change loading bar content (it may have been altered)
$this.find('.loading-bar').html($initmessage);
// If there is no data returned, there are no more posts to be shown. Show error
if(data == "") {
$this.find('.loading-bar').html($settings.error);
}
else {
// Offset increases
offset = offset+$settings.nop;
// Append the data to the content div
$this.find('.content').append(data);
// No longer busy!
busy = false;
}
});
}
getData(); // Run function initially
// If scrolling is enabled
if($settings.scroll == true) {
// .. and the user is scrolling
$(window).scroll(function() {
// Check the user is at the bottom of the element
if($(window).scrollTop() + $(window).height() > $this.height() && !busy) {
// Now we are working, so busy is true
busy = true;
// Tell the user we're loading posts
$this.find('.loading-bar').html('Loading Posts');
// Run the function to fetch the data inside a delay
// This is useful if you have content in a footer you
// want the user to see.
setTimeout(function() {
getData();
}, $settings.delay);
}
});
}
// Also content can be loaded by clicking the loading bar/
$this.find('.loading-bar').click(function() {
if(busy == false) {
busy = true;
getData();
}
});
});
}
})(jQuery);
我认为解决方案可能是删除$ offset查询,而是在我的Ajax.php文件中使用以下内容:
$query = "SELECT * FROM News WHERE ID < ".$id_of_last_item_on_page." ORDER BY ID DESC LIMIT ".$postnumbers; /* No OFFSET*/
这样,查询将使用上次显示的项目的ID作为下一页的开头,按降序排列(向数据库的“底部”前进),而不管是否存储了新条目。数据库的顶部,从而消除了重复(2009年雅虎的a solution)。但我不知道如何修改这三个文件以实现这一点。
关于如何做的任何想法?我花了几天时间调整代码无济于事......非常感谢你!