如果无限滚动,我如何动态加载页面。
问题是我必须手动创建php文件。脚本搜索2.php然后3.php然后4.php等等...我不想手动创建这些页面,我希望它是自动的? :)
我觉得我必须创建某种类型的循环来获取新图像。 目前我的代码是
<!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> </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>';
}
?>
答案 0 :(得分:1)
创建一个单独的页面,其中包含view.php?start=100&end=150
之类的参数,并更改您的查询以反映:
SELECT * FROM test LIMIT <escaped and sanitized "start">, <difference between "start" and "end">
开始和结束之间的差异也可以是传递的一些值,以反映每页上显示的元素数量。
然后,您可以使用JQuery来感知页面底部何时在用户的视口中可见,并且您可以使用AJAX请求服务器为您提供下一组图像。
所以,序列是这样的:
view.php
)。您的脚本返回页面的完整HTML,元素0-10或其他内容$.post('view.php?ajax=true', { start: end+1, end: end+11 }, someFunctionThatResetsEndToReflectTheNewEndAndUpdateThePage );