0

How can i load pages dynamically in case of an infinite Scroll.

The issue is that i have to manually create the php files. The Script searches for 2.php and then 3.php and then 4.php so on... I don't want to create these pages manually , i want it to be automatic ? :)

Some how i feel i have to create some type of loop to get the new images. Currently my code is as

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <!--[if lt IE 9]><script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
  <title>Adi</title>

  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Adi</title>

<link rel="stylesheet" type="text/css" href="style.css" />


<script src="jquery-1.7.1.min.js"></script>
<script src="jquery.masonry.min.js"></script>
<script src="jquery.infinitescroll.min.js"></script>
<script src="modernizr-transitions.js"></script>
</head>

<body>
<div id="container" class="transitions-enabled infinite-scroll clearfix">
<?php
// Make a MySQL Connection
mysql_connect("localhost", "ID", "Pass") or die(mysql_error());
mysql_select_db("DB") or die(mysql_error());

// Retrieve all the data from the "test " table
$result = mysql_query("SELECT * FROM test limit 10")
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">';
echo "<img src=\"images/".$row['image']."\" alt=\"name\" />";
echo '</div>';
}
?>
</div>
<p>Hi</p>
<p>&nbsp;</p>

<nav id="page-nav">
  <a href="2.php"></a>
</nav>

<script >
  $(function(){
    var $container = $('#container');

    $container.imagesLoaded(function(){
      $container.masonry({
        itemSelector: '.box',
        columnWidth: 60
      });
    });

    $container.infinitescroll({
      navSelector  : '#page-nav',    // selector for the paged navigation 
      nextSelector : '#page-nav a',  // selector for the NEXT link (to page 2)
      itemSelector : '.box',     // selector for all items you'll retrieve
      loading: {
          finishedMsg: 'No more pages to load.',
          img: 'http://i.imgur.com/6RMhx.gif'
        }
      },
      // trigger Masonry as a callback
      function( newElements ) {
        // hide new items while they are loading
        var $newElems = $( newElements ).css({ opacity: 0 });
        // ensure that images load before adding to masonry layout
        $newElems.imagesLoaded(function(){
          // show elems now they're ready
          $newElems.animate({ opacity: 1 });
          $container.masonry( 'appended', $newElems, true ); 
        });
      }
    );

  });
</script>
</body>
</html>

Page 2.php

<?php
    // Make a MySQL Connection
    mysql_connect("localhost", "ID", "Pass") or die(mysql_error());
    mysql_select_db("DB") or die(mysql_error());

    // Retrieve all the data from the "test " table
    $result = mysql_query("SELECT * FROM test limit 5")
    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">';
    echo "<img src=\"images/".$row['image']."\" alt=\"name\" />";
    echo '</div>';
    }
    ?>
4

1 回答 1

1

制作一个带有类似参数的页面,view.php?start=100&end=150并更改您的查询以反映:

SELECT * FROM test LIMIT <escaped and sanitized "start">, <difference between "start" and "end">

start 和 end 之间的差异也可能是传递的某个值,以反映在每个页面上显示多少元素。

然后,您可以使用 JQuery 来感知页面底部何时在用户的视口中可见,并且您可以使用 AJAX 请求服务器为您提供下一组图像。

所以,顺序是这样的:

  • 用户加载页面 ( view.php)。您的脚本返回页面的完整 HTML、元素 0-10 或其他内容
  • 用户滚动到页面底部。您的 JQuery 看到这种情况并说$.post('view.php?ajax=true', { start: end+1, end: end+11 }, someFunctionThatResetsEndToReflectTheNewEndAndUpdateThePage );
  • 您的脚本发现它是一个 AJAX 请求(注意:ajax=true)并返回 XML 或 JSON 以及需要显示的数据。您也可以只制作一个单独的脚本来执行此操作。
  • 您的 javascript 函数(上面的名字很长)处理数据并将其处理成用户可以理解的内容
于 2012-04-22T05:47:08.693 回答