如何实施YouTube /其他网站等网页功能?

时间:2012-03-22 17:17:31

标签: php javascript jquery

我还是Javascript的新手,想为评论和回复创建“页面”功能。

YouTube视频具有非常好的分页功能,可以很好地分页评论。

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

2 个答案:

答案 0 :(得分:1)

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

您需要了解的基本事项是:

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

    $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);
       });
    });
    

答案 1 :(得分:0)

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