Infinte滚动功能未在PHP MySQL中获取数据

时间:2018-08-12 16:55:10

标签: javascript php jquery html mysql

我正在尝试在php中进行无限滚动,在该处我有索引页,在该索引页中提取了数据并显示了结果,而在getrecord页中则有mysql代码。

现在我正在从 index 页向 getrecord 页发送数据' anotherID ',但 getrecord 页却没有提取 anotherID 的数据,则未找到结果。请帮助我,并预先感谢。这是我的代码

索引页

   


 <div class="col-lg-9 col-lg-offset-3">
       

        <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 busy = false;
      var limit = 15;
      var offset = 0;
	  var anotherID = 0;

      function displayRecords(lim, off, anotherID) {
        $.ajax({
          type: "GET",
          async: false,
          url: "getrecords.php",
          data: "limit=" + lim + "&offset=" + off + "&anotherID=" + anotherID,
          cache: false,
          beforeSend: function() {
            $("#loader_message").html("").hide();
            $('#loader_image').show();
          },
          success: function(html) {
            $("#results").append(html);
            $('#loader_image').hide();
            if (html == "") {
              $("#loader_message").html('<button class="btn btn-default" type="button">No more records.</button>').show()
            } else {
              $("#loader_message").html('<button class="btn btn-default" type="button">Loading please wait...</button>').show();
            }
            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) {
            busy = true;
            offset = limit + offset;

            // this is optional just to delay the loading of data
            setTimeout(function() { displayRecords(limit, offset); }, 500);

            // you can remove the above code and can use directly this function
            // displayRecords(limit, offset);

          }
        });

      });

    </script>


    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="bootstrap/js/bootstrap.min.js"></script>

getrecords页面

<?php

require_once("config.php");

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

$anotherID = $_GET['anotherID'];

$sql = "SELECT countries_name,cat FROM countries WHERE  category = '$anotherID'  ORDER BY countries_name 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['countries_name'] . '</h3>';
  }
}
?>

0 个答案:

没有答案