繁体   English   中英

使用php,数据源JSON在页面滚动上加载更多数据

[英]Load more data on page scroll Using php, Data source JSON

我开发了一个交易和优惠券网站,我的数据源是JSON。 我的json文件很大,大约4MB。 我想显示前30个数据,并在滚动加载后显示30​​个数据。所以请告诉我如何使用JSON进行此工作。 这是我的代码:

<?php $json = file_get_contents('offers.json');
$json_decode = json_decode($json,true);
foreach($json_decode as $data){
?>
<div class="ad-box">
<div class="ad-category"><?php echo $data['category'];?></div>
<div class="ad-image"><img class="lazy" data-src="<?php echo $data['imageUrl'];?>" src="" width="150" height="140" border="0" alt="lazy Image"/></div>
<div class="ad-title"><a href="<?php echo $data['url'];?>" target="_blank"><?php echo $data['title'] . " : " . $data['description'];?></a></div>
<div class="ad-url"><a href="<?php echo $data['url'];?>" target="_blank">Goto Offer</a></div>

</div>
<?php }
?>

您的Json数组是未知的,但是:

主页:

<div id="loadbox">

    <?php
    $json = file_get_contents('offers.json');
    $json_decode = json_decode($json, true);
    for ($i = 0; $i < 29; $i++):
        $data = $json_decode[$i];
        ?>
        <div class="ad-box">
            <div class="ad-category"><?php echo $data['category']; ?></div>
            <div class="ad-image"><img class="lazy" data-src="<?php echo $data['imageUrl']; ?>" src="" width="150" height="140" border="0" alt="lazy Image"/></div>
            <div class="ad-title"><a href="<?php echo $data['url']; ?>" target="_blank"><?php echo $data['title'] . " : " . $data['description']; ?></a></div>
            <div class="ad-url"><a href="<?php echo $data['url']; ?>" target="_blank">Goto Offer</a></div>

        </div>
        <?php
    endfor;
    ?>

</div>

<script type="text/javascript">
    var last = 30;

    function getData(lst) {
        $.get("loader.php", {'last': lst}, function (data) {
            $("#loadbox").append(data);
            last += 30 ;
        });
    }
    $(function () {
        $(window).scroll(function () {
            buffer = 40 // # of pixels from bottom of scroll to fire your function. Can be 0
            if ($("html").prop('scrollHeight') - $("html").scrollTop() <= $("html").height() + buffer) {
                getDate(last);
            }
        });
    });
</script>

在主页中,您将加载前30条记录,并在滚动端加载活动的ajax。

当一个加载器请求ajax时:

<?php
$json = file_get_contents('offers.json');
$json_decode = json_decode($json, true);
$start = (int) $_GET['last'] ;
for ($i = $start ; $i < ($start+30) ; $i++):
    $data = $json_decode[$i];
    ?>
    <div class="ad-box">
        <div class="ad-category"><?php echo $data['category']; ?></div>
        <div class="ad-image"><img class="lazy" data-src="<?php echo $data['imageUrl']; ?>" src="" width="150" height="140" border="0" alt="lazy Image"/></div>
        <div class="ad-title"><a href="<?php echo $data['url']; ?>" target="_blank"><?php echo $data['title'] . " : " . $data['description']; ?></a></div>
        <div class="ad-url"><a href="<?php echo $data['url']; ?>" target="_blank">Goto Offer</a></div>

    </div>
    <?php
endfor;
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM