简体   繁体   English

jQuery Ajax无限滚动阅读PHP

[英]jQuery Ajax infinite scroll reading PHP

I would like to implement the infinite scroll with AJAX. 我想用AJAX实现无限滚动。

This is my current jQuery code: 这是我当前的jQuery代码:

$(document).on("pagebeforeshow", "#page-main", function(){
    get_simple_info();
});
function get_simple_info(){
    var info = "";
    $.getJSON('classes/load.php', function(data){
        $.each(data, function(index, item) 
        {
            info += "<li id='" + item.id + "'><p>" + item.name + "</p></li>";
        });
        $("#listview_A").append(info); 
        $("#listview_A").trigger("change");
        $("#listview_A").listview("refresh");
    });
}

And the function is very simple either. 而且功能也很简单。

$query = $db->prepare("SELECT * FROM people ORDER BY ID DESC LIMIT 15");
$query->execute();
$query->bind_result($id, $name);
$query->store_result();
$rows = array();
while($query->fetch()){
    $rows[] = array("id" => $id, "name" => $name);
}
$query->close();
$db->close();
return json_encode($rows);

I'm trying to guide myself by this tutorial: http://www.w3bees.com/2013/09/jquery-infinite-scroll-with-php-mysql.html , but as you can see it has no ajax.request, it is all done in PHP. 我正在尝试通过本教程进行自我指导: http : //www.w3bees.com/2013/09/jquery-infinite-scroll-with-php-mysql.html ,但是您可以看到它没有ajax.request ,所有操作均在PHP中完成。

My current listview_A code is the following: 我当前的listview_A代码如下:

<ul data-role="listview" id="listview_A" data-inset="true" data-mini="true">
    <li style="height: -2px; background-color: #666666"></li>
</ul>

With the jQuery I do append more items to the <ul> tag further than the one (by default) that is there. 使用jQuery时,我确实将更多项添加到<ul> tag不是在那里。

Said this, I really don't know where to start adapting that tutorial to my code, can you guys give me some lights? 这样说,我真的不知道从哪里开始使该教程适应我的代码,你们能给我一些启发吗?

Thanks. 谢谢。

Edit : Solved. 编辑 :已解决。 See the answer below. 请参阅下面的答案。

Before sending any data to server, check if the page is already loading. 在将任何数据发送到服务器之前,请检查页面是否已加载。 Eg 例如

if(alreadyloading == false){
    alreadyloading = true;
    SEND THE DATA TO SERVER
    ACCEPT JSON RESPONSE
    APPEND TO YOUR HTML
    INCREMENT YOUR PAGE NUMBER
   make alreadyloading = false;
}

Now while sending the data to your server page, add this page-number with it so the query will be like 现在,在将数据发送到服务器页面的同时,添加此page-number ,这样查询将类似于

First calculate the LIMIT values Suppose you are willing to display five results in a scroll, $page_size = 5 $offset = ($page_num - 1)*$page_size; 首先计算LIMIT值假设您愿意滚动显示五个结果, $page_size = 5 $offset = ($page_num - 1)*$page_size; So for page_num = 1 , your offset will be 0 Hence, 因此,对于page_num = 1 ,您的偏移量将为0因此,

SELECT * FROM table_name LIMIT 0,5

For page_num = 2 , $offset = 5 SELECT * FROM table_name LIMIT 5,10 And so on 对于page_num = 2$offset = 5 SELECT * FROM table_name LIMIT 5,10依此类推

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM