0

我在滚动窗口时难以遍历记录。我最初的想法是加载足够的记录以适应屏幕,然后在窗口滚动到底部时加载额外的一组。我尝试使用会话/变量将一些计数器传递给函数,但没有运气。下面的代码返回足够的记录以适应窗口高度,但限制为 0,10。解决这个问题的简单方法是什么?

另一个问题是我应该在 Mysql 查询中使用 LIMIT 还是 ID > + 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/>';
    }

?>
4

2 回答 2

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/>';
    }

?>

我还认为您应该添加一个顺序,以确保结果始终以相同的顺序排列。

于 2012-07-04T18:33:55.140 回答
0

使用LIMIT,它结合了偏移量(第一个参数)和要获取的行数(第二个参数)。这是一个简短的分页示例,每次获取 7 个 100 个数字的第 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; 
于 2012-07-04T18:37:48.683 回答