第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使用 Jquery 或 Ajax 從數(shù)據(jù)庫加載更多內(nèi)容

如何使用 Jquery 或 Ajax 從數(shù)據(jù)庫加載更多內(nèi)容

PHP
30秒到達(dá)戰(zhàn)場 2022-07-22 09:40:23
我正在開發(fā)類似 quora 的社交網(wǎng)站,當(dāng)您拖動(dòng)到頁面底部時(shí),將加載新內(nèi)容。但在我的應(yīng)用程序中,會(huì)有所不同,我的頁面將有一個(gè)更多按鈕而不是滾動(dòng)。每當(dāng)用戶單擊“更多”按鈕時(shí),底部都會(huì)加載新內(nèi)容。在我的 PHP 代碼中,我從數(shù)據(jù)庫中無限制地獲取所有內(nèi)容,然后我使用 jQuery 來切片并讓我的頁面加載更多這樣的項(xiàng)目。$("#loadMore").on('click', function (e) {        e.preventDefault();        $(".more:hidden").slice(0, 10).slideDown();        if ($(".more:hidden").length == 0) {            $("#loadMore").fadeOut('slow');        }    });但我不知道這是否是最佳實(shí)踐,雖然它現(xiàn)在對我很有效,但我相信將來我可能會(huì)在加載我的頁面時(shí)遇到困難,我認(rèn)為我的頁面在加載時(shí)會(huì)變得非常慢。我使用 twig 模板引擎,因此我無法從后端腳本中回顯數(shù)據(jù)并使用 ajax 顯示因?yàn)槲业那岸丝雌饋硐襁@樣。<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>        {% for u in user_details %}          {% if u.id == 2 or u.id == 1 %}            <a  href="/deletepost?author={{p.post_author}}&postid={{p.id}}" class="dropdown-item"> <i class="icon-trash"></i> Delete Post</a>          {% endif %}        {% endfor %}      </div>    </div>  </div></div>
查看完整描述

1 回答

?
月關(guān)寶盒

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超5個(gè)贊

添加一個(gè)last id為$lastid的變量,并在post加載時(shí)將其覆蓋,并將$lastid設(shè)置為last post的數(shù)據(jù)庫id,然后選擇一個(gè)id大于$lastid的post。這種方法也快速高效。


<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:


<?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

?>


查看完整回答
反對 回復(fù) 2022-07-22
  • 1 回答
  • 0 關(guān)注
  • 188 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)