Asesha George Asesha George - 7 months ago 21
PHP Question

Infinite scroll repeate same id again from database

friends i am trying to load data on scroll using php and jquery. my problem is database is loading from last id but last loaded id's are repeating. i am unable to find weather its because of jquery conflict or my code error. i am using jquery 1.11.1 version. please see the below image for better understanding.

enter image description here

and my jquery to get data from php is

$(window).scroll(function(){
if ($(window).scrollTop() == $(document).height() - $(window).height()){
loadData();
}
});

function loadData()
{
$('div.postloader').html('<img src="http://www.zesteve.com/img/loader.gif">');
$.post("/getData.php?lastID=" + $(".post-list:last").attr("id"),
function(data){
if (data != "") {
$(".post-list:last").after(data);
}
$('div.postloader').empty();
});

};


and in getData.php i am using

$last_id = $_GET['lastID'];
$stmt = $dbh->prepare('SELECT * FROM review WHERE id < :lastID AND user_id = :user_id order by id DESC LIMIT 5');
$stmt->execute(array(':lastID' => $last_id,':user_id'=>$user_id));


friends if you not understand anything here please comment. please help me where i am doing wrong?

Answer

I think you're pulling the data from the server too fast, try adding a flag that will prevent pulling the items while the request is running:

var flagPulling = false;
function loadData() { 
    if( flagPulling ) {
        return;
    }
    flagPulling = true;
    $('div.postloader').html('<img src="http://www.zesteve.com/img/loader.gif">');
    $.post("/getData.php?lastID=" + $(".post-list:last").attr("id"), function(data){
        if (data != "") {
            $(".post-list:last").after(data);            
        }
       $('div.postloader').empty();
       flagPulling = false;
    });
}; 
Comments