自动加载结果一遍又一遍地获取相同的结果 [英] Autoload results fetching same results over and over

查看:32
本文介绍了自动加载结果一遍又一遍地获取相同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了以下自动加载代码.它从数据库中获取结果,但是不断重复循环相同的结果.

I have implemented the following autoload code. It fetches result from database but it keeps looping the same result over and over.

php代码

<?php
require_once("config.php");

$limit = (intval($_GET['limit']) != 0 ) ? $_GET['limit'] : 10;
$offset = (intval($_GET['offset']) != 0 ) ? $_GET['offset'] : 0;

$keywords = $_GET['keywords'];

$sql = "SELECT * FROM blog_posts WHERE postCat LIKE '".$keywords."' ORDER BY postID ASC LIMIT $limit OFFSET $offset";
try {
  $stmt = $DB->prepare($sql);
  $stmt->execute();
  $results = $stmt->fetchAll();
} catch (Exception $ex) {
  echo $ex->getMessage();
}
if (count($results) > 0) {
  foreach ($results as $res) {
    echo '<h3>' . $res['postTitle'] . '</h3>';
  }
}
?>

推荐答案

尝试一下

<?php
$keywords = 'events';
?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Test</title>    
    <script src="bootstrap/js/jquery-1.11.1.min.js"></script>
  </head>
  <body>

        <div class="col-lg-12" id="results"></div>
        <div id="loader_image"><img src="loader.gif" alt="" width="24" height="24"> Loading...please wait</div>
        <div class="margin10"></div>
        <div id="loader_message"></div>
        </div>

 <script type="text/javascript">
    var keywords = '<?php echo $keywords; ?>';  // Changed
    var busy = false;
    var limit = 15;
    var offset = 0;
    function displayRecords(lim, off) {

    // Changed/New
    var data = {
        'limit': lim,
        'offset': off,
        'keywords': keywords,
    };
        $.ajax({
        type: "GET",
        async: false,
        url: "getrecords.php",
        data: data, // Changed
        cache: false,
        beforeSend: function() {
            $("#loader_message").html("").hide();
            $('#loader_image').show();
        },
        success: function(html,textStatus,jqHXR) {
            $("#results").append(html);
            $('#loader_image').hide();
            if (html == "") {
                $("#loader_message").html('<button class="btn btn-default" type="button">No records found.</button>').show()
            } else {
                $("#loader_message").css('display','none');
                $("#loader_message").html(html);
            }
            window.busy = false;
        },
        });
    }
    $(document).ready(function() {
        // start to load the first set of data
        if (busy == false) {
            busy = true;
            // start to load the first set of data
            displayRecords(limit, offset);
         }
        $(window).scroll(function() {
            // make sure u give the container id of the data to be loaded in.
            if ($(window).scrollTop() + $(window).height() > $("#results").height() && !busy) {


$('#result').on('scroll', function() { if($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) { busy = true; offset = limit + offset; setTimeout(function() { displayRecords(limit, offset); }, 500); } })
                // you can remove the above code and can use directly this function
                // displayRecords(limit, offset);
            }
        });
    });
</script>

  </body>
</html>

这篇关于自动加载结果一遍又一遍地获取相同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆