Arqetech
Arqetech

Reputation: 493

PHP/JS Unlimited scroll loading all data from MySQL in one scroll

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 } ?>

Upvotes: 0

Views: 135

Answers (1)

Andrew Coder
Andrew Coder

Reputation: 1058

Looks to me like your LIMIT syntax is backwards

LIMIT 15,".$countBlop.

should be

LIMIT ".$lastIndex.",15

As is now it would be starting at record 15 and then continuing to load until you reach the total count(as defined by $countBlop)

You will also need to have a way for jQuery to pass the last index to the ajax call.

Upvotes: 1

Related Questions