ajax php 點擊加載更多


前端部分:

js:

$(function ()
{
    $('.get_more').click(function ()
    {
        if($(this).text()=='沒有更多了'){return false;} //停止加載
        var list_num = $('.list').length;  //獲取當前總條數
        var amount = 3 ; //每次點擊加載條數
        $(this).text('').append("<img src='images/loader.gif'>");
        $.post('more.php',{list_num:list_num,amount:amount},function (result){ if(result=='not_more'){$('.get_more').text('沒有更多了');}else{$('.get_more').text('查看更多記錄'); $('#record_box').append(result);}})
        
        
    })
})

 

html:

<div class="title">
<a href="javascript:history.go(-1)"><img src="images/back.png"></a>資金明細
</div>
<div id="record_box">

    <%foreach from=$userInfo_arr key=key item=value%>
    <div class="list">
        <div class="left"><%$value.type_ch%><br><%$value.time%></div>
        <div class="right"><span class="<%$value.type_en%>"><%if $value.type_en=='plus'%>+<%elseif $value.type_en=='reduce'%>-<%/if%><%$value.amount%>元</span></div>
    </div>
    <%/foreach%>
    <%if $userInfo_arr%>
    <div class="get_more">查看更多記錄</div>
    <%else%>
    <div class="null">沒有相關記錄</div>
    <%/if%>
</div>

 

后端部分:

php:

$list_num = $_POST['list_num'];  //記錄條數
$amount = $_POST['amount'];      //一次查詢多少條

$sql = 'SELECT * FROM '.$GLOBALS['ecs']->table('app_user_payrecord')."WHERE mobile = '$mobile' ORDER BY id DESC LIMIT $list_num,$amount";
$userInfo = $GLOBALS['db']->getAll($sql);

if(empty($userInfo))
{
    echo 'not_more';
}else
{
    for($i=0;$i<count($userInfo);$i++)
    {
    $type_ch = $userInfo[$i]['type_ch'];
    $time = $userInfo[$i]['time'];
    $type_en = $userInfo[$i]['type_en'];
    $amount = $userInfo[$i]['amount'];
    switch ($type_en) {
        case 'reduce':
            $symbol = '-';
            break;
        case 'plus':
            $symbol = '+';
            break;
    }
    echo <<<Eof
    <div class="list">
        <div class="left">{$type_ch}<br>{$time}</div>
        <div class="right"><span class="{$type_en}">{$symbol}{$amount}元</span></div>
    </div>
Eof;
    };

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM