Infinite Scroll with MySQL Data

前端 未结 2 1792
名媛妹妹
名媛妹妹 2021-01-03 09:16

I have followed help located in this topic: Using infinite scroll w/ a MySQL Database

And have gotten close to getting this working properly. I have a page that is d

相关标签:
2条回答
  • 2021-01-03 09:56

    Have you tried doing any debugging?

    If you are not already using, I would recommend getting the firebug plugin.

    Does the ajax call return empty? If it does, try echoing the sql and verify that is the correct statement and that all the variables contain the expected information. A lot of things could fail considering there's a lot of communication happening between client, server and db.

    In response to your comment, you are adding the html in this piece of code:

    if(html){
         $(".blockContainer").append(html);
         $('div#ajaxLoader').hide();
    }
    

    I would do a console.log(html) and console.log($(".blockContainer").length) before the if statement.

    0 讨论(0)
  • 2021-01-03 09:57

    I seen your code in another answer, and I would recommend using the LIMIT functionality in MySql instead of offsetting the values. Example:

    SELECT * FROM all_posts ORDER BY post_id DESC LIMIT '".(((int)$page)*5)."',5
    

    This will just take a page number in the AJAX request and get the offset automatically. It's one consistent query, and works independent of the last results on the page. Send something like page=1 or page=2 in your jQuery code. This can be done a couple different ways.

    First, count the number of elements constructed on the page and divide by the number on the page. This will yield a page number.

    Second, you can use jQuery and bind the current page number to the body:

    $(body).data('page', 1)
    

    Increment it by one each page load.

    Doing this is really the better way to go, because it uses one query for all of the operations, and doesn't require a whole lot of information about the data already on the page.

    Only thing to note is that this logic requires the first page request to be 0, not 1. This is because 1*5 will evaluate to 5, skipping the first 5 rows. If its 0, it will evaluate to 0*5 and skip the first 0 rows (since 0*5 is 0).

    Let me know any questions you have!

    0 讨论(0)
提交回复
热议问题