亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用Jquery或Ajax從數據庫加載更多內容

如何使用Jquery或Ajax從數據庫加載更多內容

PHP
智慧大石 2022-08-05 10:01:22
我正在開發一個像quora這樣的社交網站,當你拖到頁面底部時,新內容就會加載。但是在我的應用程序中,我的頁面將具有不同的按鈕而不是滾動。每當用戶點擊“更多”按鈕時,新內容都會在底部加載。在我的PHP代碼中,我無限制地從數據庫中獲取所有內容,然后使用jQuery來切片,并使我的頁面加載更多項目,就像這樣。$("#loadMore").on('click', function (e) {        e.preventDefault();        $(".more:hidden").slice(0, 10).slideDown();        if ($(".more:hidden").length == 0) {            $("#loadMore").fadeOut('slow');        }    });但我不知道這是否是最佳實踐,盡管它現在對我很好,但我相信將來我可能會遇到加載頁面的困難,我認為我的頁面在加載時可能會變得非常慢。我使用 twig 模板引擎,因此我無法從后端腳本中回顯數據并使用 ajax 進行顯示因為我的前端看起來像這樣。<div class="card blogBox moreBox" id="single_user_card"><div class="card-header card-header-borderless d-flex justify-content-between">  <div class="text-small ">      <ul class="list-inline">        <li class="list-inline-item">          <img alt="Image" src="{{ p.author_avatar }}" class="avatar avatar-sm" />        </li>          <li class="list-inline-item font-weight-bold"> {{ p.author_username |title |raw }}            <p class="text-muted text-small post-time font-weight-light">{{ p.post_date |time_diff }}</p>          </li>          <li class=""></li>      </ul>  </div>  <div class="d-lg-flex justify-content-end">    <div class="dropdown">      <a class="dark-link" href="#" id="share_dropdown_menu" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">        <i class="icon-menu"></i>      </a>      <div class="dropdown-menu" aria-labelledby="share_dropdown_menu">        <span postid="{{APPURL}}/article/{{p.id}}/{{p.post_name}}/{{ base64_encode('this token is valid for one hour') }}">          <a href="javascript:void(0);" class="dropdown-item copy_link">            <i class="icon-share"></i> Copy link          </a>        </span>在這種情況下,我可以獲得任何幫助或想法嗎?我已經有應用程序在線運行,你可以看看這里,以更好地理解我的意思。
查看完整描述

1 回答

?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

添加一個$lastid的上一個 id 變量,并在加載帖子時覆蓋它,并將$lastid設置為最后一個帖子的數據庫 ID,然后選擇一個 id 大于該$lastid的帖子。這種方法也是快速有效的。


<script>

        var lastid = 1;

        function fetchpost() {

              $.ajax({

                url:"getPosts.php?lastid="+lastid, //the page containing getting post code

                type: "post",

                dataType: 'json',

                data: '',

                success:function(result){  

                  $("#posts_parent").after(result);

                  //Getting id of last post using :last

                  lastid = $("posts_parent:last").attr("id");

               }

             });

         }

    <script>

    <div id="posts_parent">


    </div>

<button onlclick="fetchpost()">Fetch New Posts</button> //Button fetches new posts

菲律賓比索:


<?php

$lastidofpost = $GET['lastid'];

$sql=mysql_query("SELECT * FROM posts ORDER BY id DESC WHERE id > $lastidofpost  LIMIT 10"); // Limit 10, only 10 posts will be fetched

while($row=mysql_fetch_array($sql))

{

    $id= $row['id'];

    $postDiscription = $row['disc'];

?>

    <div id="<?php echo $id; ?>"> 

       <?php echo $postDiscription; ?>

    </div> 

<?php

?>


查看完整回答
反對 回復 2022-08-05
  • 1 回答
  • 0 關注
  • 120 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號