David
David

Reputation: 35

Ajax infinity scroll returning same results

I have this function which detects when the user gets to the bottom of the page, which then sends an ajax request to the server...

$(window).scroll(function()
    {
      // if at bottom, add new content:
      if  ($(window).scrollTop() == $(document).height() - $(window).height())
      {
        var data = { mode: "bottom" };

        $.ajax({
            type: 'POST',
            url: 'server/script/infinity.php',
            data: data,

            success: function(data){
              $("#load").append(data);              
            }
        });
      }

    }); 

This then outputs the data from the request in the <div id="load"></div> box. My problem is that the returned results are the same as the results already on the page. I need to get the NEXT 5 results in the database.

This is my current SQL query:

mysqli_query($mysqli,"SELECT * FROM posts ORDER BY id DESC LIMIT 5"); 
while($post = mysqli_fetch_array($result)) { ... }

I have tried...

mysqli_query($mysqli,"SELECT * FROM posts ORDER BY id DESC LIMIT 5, 5");

See LIMIT 5,5, this solves the problem for the next 5 results however when the user keeps scrolling the same problem occurs, I need a way to loop through this but increasing the limit each time. Any ideas?

Upvotes: 0

Views: 101

Answers (1)

Gurkan İlleez
Gurkan İlleez

Reputation: 1583

"SELECT * FROM posts ORDER BY id DESC LIMIT 5(X), 5")

X value has to be parametric.

javascript:

var data = {page : 10};
$.ajax({
            type: 'POST',
            url: 'server/script/infinity.php',
            data: data,

            success: function(data){
              $("#load").append(data);              
            }
});

php :

    $starting_limit = $_REQUEST['page'];
mysqli_query($mysqli,"SELECT * FROM posts ORDER BY id DESC LIMIT ". $starting_limit.", 5");

You have to track starting_limit of query

Upvotes: 1

Related Questions