簡體   English   中英

PHP / Ajax加載更多按鈕

[英]PHP/Ajax load more button

我想顯示5個項目,當用戶單擊“加載更多”按鈕時,將從數據庫中拉出5個項目。

我的物品被這樣檢索:

$res = mysql_query("SELECT * FROM `posts` ORDER BY `id` DESC LIMIT 5") or die(mysql_error());

    if(mysql_num_rows($res) > 0){
        while($row = mysql_fetch_assoc($res)){
            $id = $row['id'];
            $user_id = $row['user_id'];

然后我有ajax代碼和按鈕:

<script type="text/javascript">
    $(document).ready(function(){
        $(".load_more").click(function (){
            $('.load_more').html('<img src="images/ajax-loader.gif" />');
            $.ajax({
                url: "loadmore.php?lastid=" + ("<?php echo $id; ?>"),
                success: function(html){
                    if(html){
                        $(".main_page").append(html);
                        $('.load_more').html('Load More');
                    }else{
                        $('.load_more').replaceWith('<center>No more posts to show.</center>');
                    }
                }
            });
        });
    });
</script>



    <button class="load_more">Load More</button>

最后,單擊按鈕時將調用的loadmore.php文件:

    $res17 = mysql_query("SELECT * FROM `posts` WHERE id < '".addslashes($_GET['lastid'])."' LIMIT 0, 25 LIMIT 10"); 
while($row17 = mysql_fetch_assoc($res17)){
            $id = $row17['id'];
            $user_id = $row17['user_id'];

我怎么稱呼正確的ID? 我知道網址:“ loadmore.php?lastid =“ +(”“),可能是錯誤的,但不確定如何解決

我不知道這是否是問題,但您忘了關閉一些左花括號,我已在此處修復。

$res = mysql_query("SELECT * FROM `posts` ORDER BY `id` DESC LIMIT 5") or die(mysql_error());

    if(mysql_num_rows($res) > 0){
        while($row = mysql_fetch_assoc($res)){
            $id = $row['id'];
            $user_id = $row['user_id'];
}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM