您如何循环浏览与ajax请求偏移增加的文本文件?

时间:2019-07-02 02:25:13

标签: ajax loops load

我试图遍历其中已存储数据的txt文件。这是我的ajax代码,用于设置需要加载的数据的限制和偏移量。

    // for notification loading
$(document).ready(function() {
  var flag = 0;
  $.ajax({
      type: "GET",
      url: "../Admin/Users/Notification2.php",
      data: {
        'offset': 0,
        'limit': 10
      },
      success: function(data){
        $('#AllNotifications').append(data);
        flag +=3;
      }

  });
  $('#AllNotifications').scroll(function(){
    if($('#AllNotifications').scrollTop() >= $('#AllNotifications').height() - $('#AllNotifications').height()){
      $.ajax({
      type: "GET",
      url: "../Admin/Users/Notification2.php",
      data: {
        'offset': flag,
        'limit': 10
      },
      success: function(data){
        $('#AllNotifications').append(data);
        flag +=3;
      }

  });
    }
  })
})

这是Notifications2.php的外观

$Username = $_SESSION['username'];
echo "";

 function time_elapsed_string($datetime, $full = false) {
    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }

    if (!$full) $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}


     $fn = fopen("../UsersNotifications/".$Username."/Notifications.txt","r");


$offset = $_GET['offset'];
$limit= $_GET['limit'];

  date_default_timezone_set("America/New_York");

echo $offset;
echo "<br/>";
echo $limit;

 while($offset < $limit) {
  $result = fgets($fn);
    if (!feof($fn)) {
  $pieces = explode(",", $result);

  if($pieces[2] == 'Liked'){
    echo "
   <center>
<a href=Post?name=".$pieces[0]."&Post=".$pieces[1]." style='text-decoration:none;color:black'><p class='".$pieces[3]." hoverNotifs'>
    ".$pieces[0]." Liked Your Post</a><span style='margin-left:100px'>".time_elapsed_string($pieces[4])."</span>

    ";
  }
   if($pieces[2] == 'Repost'){
      echo "
     <center>
<a href=Post?name=".$pieces[0]."&Post=".$pieces[1]." style='text-decoration:none;color:black'><p class='".$pieces[3]." hoverNotifs' >
".$pieces[0]." Reposted Your Post</a> <span style='margin-left:100px'>".time_elapsed_string($pieces[4])."</span>

    ";
}
 if($pieces[2] == 'Follow'){
      echo "
     <center>
<p class='".$pieces[3]." hoverNotifs'>
<a href=Account?=".$pieces[0].">".$pieces[0]."</a> Followed you!<span style='margin-left:100px'>".time_elapsed_string($pieces[4])."</span>

    ";
}
if($pieces[2] == 'RequestF'){
      echo "
     <center>
<a href='Notifications' style='text-decoration:none;color:black'><p class='".$pieces[3]." hoverNotifs'>
".$pieces[0]." wants to follow you!</p><span style='margin-left:100px'>".time_elapsed_string($pieces[4])."</a></span>

    ";
}
}



}

我如何做到这一点,所以当偏移量增加时,显示的数据也会增加。因此,它将首先循环浏览第1-10行的数据,然后再滚动显示第11、12和13行。

文件中的示例数据

woof,1,Liked,R,07/01/201908:43:03pm
woof2,1,Liked,R,07/01/201908:43:03pm
woof3,1,Liked,R,07/01/201908:43:03pm
woof4,1,Liked,R,07/01/201908:43:03pm
woof5,1,Liked,R,07/01/201908:43:03pm
woof6,1,Liked,R,07/01/201908:43:03pm
woof7,1,Liked,R,07/01/201908:43:03pm
woof8,1,Liked,R,07/01/201908:43:03pm
woof9,1,Liked,R,07/01/201908:43:03pm
woof10,1,Liked,R,07/01/201908:43:03pm
woof11,1,Liked,R,07/01/201908:43:03pm
woof12,1,Liked,R,07/01/201908:43:03pm
woof13,1,Liked,R,07/01/201908:43:03pm
woof14,1,Liked,R,07/01/201908:43:03pm
woof15,1,Liked,R,07/01/201908:43:03pm
woof16,1,Liked,R,07/01/201908:43:03pm
woof17,1,Liked,R,07/01/201908:43:03pm

0 个答案:

没有答案