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;
    };

}

原文地址:https://www.cnblogs.com/haohaosky/p/6379882.html