一尘不染

如何对Infinite Scroll的查询结果进行分页?

mysql

我有一个示例页面,该页面使用2个PHP文件和一个MySql数据库,并且我正在尝试在其上使用jQuery无限滚动。如何从数据库加载下一组数据?例如,我的图片数据库中有100条记录,我想显示20条,然后在滚动后显示下20条。

这是我目前的核心:

index.php

<?php
ob_start();
require_once('images.html'); 
ob_end_flush();
?>

images.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title> trata tata</title>
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <!--[if lt IE 9]><script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
  <link rel="stylesheet" href="./css/style.css" />
   <!-- scripts at bottom of page -->
</head>
<body class="demos ">
  <nav id="site-nav">
    <h1><a href="./index.html">ole tu jest HOME</a></h1>
    <h2>Menu 1</h2>
    <ul class="docs-list">
        <li><a href="../docs/intro.html">11111111</a>
    </ul>
    <h2>Menu 2</h2>
    <ul class="demos-list">
        <li><a href="../demos/basic-single-column.html">222222222</a>
        <li class="current"><a href="#content">3333333333</a></li>
    </ul>
  </nav> <!-- #site-nav -->

  <section id="content">
     <h1>cos tam ....</h1>
     <div id="container" class="clearfix">
     <?php
     // Make a MySQL Connection
     mysql_connect("localhost", "root", "") or die(mysql_error());
     mysql_select_db("test") or die(mysql_error());

     // Retrieve all the data from the "test " table
     $result = mysql_query("SELECT * FROM foto")
     or die(mysql_error());

     // store the records of the "TABLENAME" table into $row array and loop through
     while ( $row = mysql_fetch_array( $result, MYSQL_ASSOC ) ) {

     // Print out the contents of the entry

     //echo "details: ".$row['id'];
     echo '<div class="box photo col3">';
     echo "<img src=\"".$row['src']."\" alt=\"wwwwww\" />";
     echo '</div>';
     }
     ?>
     </div> <!-- #container -->

     <script src="./js/jquery-1.7.1.min.js"></script>
     <script src="./jquery.masonry.min.js"></script>
     <script>
       $(function(){
         var $container = $('#container');
         $container.imagesLoaded( function(){
           $container.masonry({
             itemSelector : '.box'
           });
         });

       });
     </script>
     <footer id="site-footer">
       szaki <a href="http://desandro.com">sdfsdfdsfsdf</a>
     </footer>

  </section> <!-- #content -->

</body>
</html>

阅读 239

收藏
2020-05-17

共1个答案

一尘不染

您需要添加LIMIT到查询中。MySQL手册中的示例:

SELECT * FROM tbl LIMIT 5,10;  # Retrieve rows 6-15

在这里,5是起始偏移量,10是您要获取的行数。

对于客户端,您将需要无限滚动jQuery插件

2020-05-17