0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

13 Comments

How is the $lastIndex variable getting set? How are you passing it to the ajax call?
I used the $countBlop not $lastIndex
I don't have a variable named $lastIndex
Right, but your code appears to be incorrect in that aspect, as there is no limit on the query giving data to $countBlop $sql = mysql_query("SELECT * FROM articles ORDER BY postNum DESC"); $countBlop = mysql_num_rows($sql);
Sorry, to clarify, you need to create a variable to the effect of $lastIndex, since $countBlop is a total number of records as it's currently written.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.