我在滚动窗口时难以遍历记录。我最初的想法是加载足够的记录以适应屏幕,然后在窗口滚动到底部时加载一个额外的集合。我尝试使用会话/变量将一些计数器传递给函数,没有运气。下面的代码返回足够的记录以适应窗口高度,但限制为0,10。解决这个问题的简单方法是什么?
另一个问题是我应该使用LIMIT或ID>对Mysql查询+ LIMIT?
$(function(){
setInterval(function(){
var totalHeight, currentScroll, visibleHeight;
if (document.documentElement.scrollTop)
{ currentScroll = document.documentElement.scrollTop; }
else
{ currentScroll = document.body.scrollTop; }
totalHeight = document.body.offsetHeight;
visibleHeight = document.documentElement.clientHeight;
if (totalHeight <= currentScroll + visibleHeight )
{
$.get('infinite_pull.php', function(data) {
$('body').append(data);
//alert('Load was performed.');
});
}
else
{
$('.dd').css('background-color','white');
}
}
, 100);
});
PHP
<?php
session_start();
mysql_connect('localhost','root','');
mysql_select_db('project5');
$query = "select user_email from users limit 0,10;";
$results= mysql_query($query);
while($row = mysql_fetch_array($results)){
echo $row['0'] . '<br/>';
}
?>
答案 0 :(得分:2)
多次调用php文件声音不好, 所有这些开销变得越来越糟,然后立即得到所有。 你不能只计算你需要多少,并要求这个数字?
<?php
$from = (int) $_GET['from'];
$count = (int) $_GET['count'];
mysql_connect('localhost','root','');
mysql_select_db('project5');
$query = "select user_email from users limit {$from},{$count};";
$results= mysql_query($query);
while($row = mysql_fetch_array($results)){
echo $row['0'] . '<br/>';
}
?>
我还认为你应该添加一个订单,以确保结果总是以相同的顺序。
答案 1 :(得分:0)
使用LIMIT
,它结合了偏移量(第一个参数)和要获取的行数(第二个参数)。这是一个简短的分页示例,一次得到100个数字7的第7页。
<?php
$arr = array();
for ( $x = 0; $x < 100; $x += 1 ) {
$arr[] = $x;
};
$page = 7;
$per_page = 7;
$current_page = 1;
for ( $z = 0; $z < count( $arr ); $z += 1 ) {
if ( 0 === ( $z % $per_page ) ) {
$current_page += 1;
}
if ( $current_page === $page ) {
$num_results = $current_page * $per_page;
$query = "select user_email from users limit {$num_results},{$per_page};";
echo "value $z on page $page via $query \n";
}
}
?>
输出:
$ php pagination.php
value 35 on page 7 via select user_email from users limit 49,7;
value 36 on page 7 via select user_email from users limit 49,7;
value 37 on page 7 via select user_email from users limit 49,7;
value 38 on page 7 via select user_email from users limit 49,7;
value 39 on page 7 via select user_email from users limit 49,7;
value 40 on page 7 via select user_email from users limit 49,7;
value 41 on page 7 via select user_email from users limit 49,7;