如何实现像YouTube/其他网站这样的页面功能

How to Implement Pages Feature like YouTube / Other sites?

本文关键字:网站 功能 其他 何实现 实现 YouTube      更新时间:2023-09-26

我仍然是Javascript的新手,想为评论和回复创建一个'页面'功能。

YouTube视频有一个非常好的分页功能,可以很好地对评论进行页面。

我该怎么做?我目前的设置是PHP,Javascript和MySQL。

Youtube 将 ajax 请求发送回服务器,并获取更新到当前查看页面的结果。其他网站也使用类似的技术。

您需要了解的基本内容是:

  1. 分页需要两个主要变量。 $page$itemsPerPage .
  2. 使用上述变量,您将创建一个查询限制
  3. 您将在查询中使用此限制来获取下一页的结果。

例:

$page = (int)$_POST['page'];
$itemsPerPage = 10; //Generally this might be static
$limit = (($page-1)*$itemsPerPage).",".$itemsPerPage;
// For the Page 1, this give 0,10
$query = "SELECT ... LIMIT $limit";
   //Translates to LIMIT 0,10 so takes out the first 10 records or in other words first page
//get the records, creat the markup and echo them

现在,作为Javascript的一部分,这里有一个jQuery Post Request的例子

$("#yourpageonelink").click(function() {
   $.post("yourpage.php", { 
     page: 1 // I am using static value for demo, but you should get this dynamically
   }, function(data)  { 
      //Now data will get the content returned from the php file
     // So update the div
     $("#mainContainer").html(data);
   });
});

如果您正在谈论YouTube如何允许您在不离开实际页面的情况下浏览评论,那么这是通过AJAX完成的。javascript请求被发送到服务器,请求一个新的注释页面。 服务器响应数据,然后javsacript使用新信息更新注释区域。所有这些都可以在不将用户重定向到新页面的情况下完成。