I was working on Unlimited scroll for my website using PHP and JavaScript. I almost made it but there was a problem In my code, after scrolling to the bottom of the page my script should load 15 articles every time I scroll to the bottom and after its done It stops loading more posts, but instead it loads all of the articles and then stops the script when all of the posts are loaded.
My JavaScript code:
<script>
// Unlimited Scroll
$(document).ready(function() {
$('.post-load').hide();
var load = 0;
var nbr = "<?php echo mysql_num_rows($sql); ?>";
$(window).scroll(function() {
if ($(window).scrollTop() == $(document).height() - $(window).height()) {
$('.post-load').show();
load++;
if (load * 15 > nbr) {
$('.post-load').hide();
} else {
$.post("extensions/ajax.php", {
load: load
}, function(data) {
$('.lb').append(data);
$('.post-load').hide();
});
}
}
});
});
extensions/ajax.php File for loading the posts through PHP:
<?php
include 'connect.php';
$load = htmlentities(strip_tags($_POST['load'])) * 2;
$sql = mysql_query("SELECT * FROM articles ORDER BY postNum DESC");
$countBlop = mysql_num_rows($sql);
$queryL = mysql_query("SELECT * FROM articles ORDER BY postNum DESC LIMIT 15,".$countBlop."");
while ($posts = mysql_fetch_assoc($queryL)) {
?>
<div class="article">Echo the posts...</div>
<?php } ?>