问题|从数据库获取随机值,如何不重复它们

发布于 2024-12-02 09:42:39 字数 878 浏览 0 评论 0原文

我正在创建一个从数据库获取随机视频的网站。 我的问题是,我获取随机视频的代码是这样的:

select id,content from videos order by rand() limit 1

并且我不希望用户看到相同的视频,直到之前播放过其他 3 个视频(至少)。

您对如何做到这一点有什么建议吗? 这就是我的网站目前的运作方式。

  1. HTML-AJAX(调用视频 URL
  2. PHP(返回随机视频 URL) 一个视频。
  3. AJAX(显示视频

[已编辑] 我面临的另一个问题是我只需要返回一个视频网址, 因为这就是我的 ajax 调用的样子:

success: function(data){

            $('#content').html('<div id="ytapiplayer">You need Flash player 8+ and JavaScript enabled to view this video.</div>');
            var params = { allowScriptAccess: "always" };
            var atts = { id: "ytapiplayer" };
            swfobject.embedSWF(data.vidData+"&enablejsapi=1&playerapiid=ytapiplayer?autoplay=1", "ytapiplayer", "500", "405", "8", null, null, params, atts);
}

提前致谢。

I am creating a website that gets random videos from the db.
My problem is that my code to get the random video is this:

select id,content from videos order by rand() limit 1

And I don't want the user to see the same video until 3 other videos (at least) were played before.

Do you have any suggestion on how to do that?
This is how my site works currently.

  1. HTML-AJAX(CALL FOR VIDEO URL)
  2. PHP(RETURN RANDOM VIDEO URL) One video.
  3. AJAX(DISPLAY VIDEO)

[EDITED]
Another problem I am facing is that I need to return only one video url,
because this is how my ajax call look like:

success: function(data){

            $('#content').html('<div id="ytapiplayer">You need Flash player 8+ and JavaScript enabled to view this video.</div>');
            var params = { allowScriptAccess: "always" };
            var atts = { id: "ytapiplayer" };
            swfobject.embedSWF(data.vidData+"&enablejsapi=1&playerapiid=ytapiplayer?autoplay=1", "ytapiplayer", "500", "405", "8", null, null, params, atts);
}

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扶醉桌前 2024-12-09 09:42:39

视频id可以发给客户端吗?然后客户端(Javascript)从那里请求视频。下面是如何播放的:

  1. Ajax 视频列表 ids
  2. 将它们插入 javascript 中的数组中 (var toWatch)
  3. 随机数组
  4. 获取第一个视频
  5. 从第一个数组中删除 id。您可能想在其他数组中保留该 id 的踪迹
  6. 重复 4-5

在 javascript 中,它可能如下所示:

$.post("getVideoId.php",function(videoId){
   var aVideoToWatch = videoId.split(',').sort(randOrd);

   for(var x=0; x<aVideoToWatch.length;x++){
       $.post("getAVideo(aVideoToWatch[x])",function(){
           //play the video
       })
   }
})

// source : http://javascript.about.com/library/blsort2.htm
function randOrd(){
 return (Math.round(Math.random())-0.5); }

Could the video id be sent to the client? Then from there the client (Javascript) request the video. Here how that could be played:

  1. Ajax the the list of video ids
  2. insert them in an Array in javascript (var toWatch)
  3. Random the array
  4. Get the first video
  5. Remove the id from the first array. You might want to keep a trace of that id in an other array
  6. repeat 4-5

In javascript it might look like :

$.post("getVideoId.php",function(videoId){
   var aVideoToWatch = videoId.split(',').sort(randOrd);

   for(var x=0; x<aVideoToWatch.length;x++){
       $.post("getAVideo(aVideoToWatch[x])",function(){
           //play the video
       })
   }
})

// source : http://javascript.about.com/library/blsort2.htm
function randOrd(){
 return (Math.round(Math.random())-0.5); }
黯淡〆 2024-12-09 09:42:39

您可以使用(我们将获取十五个,这样我们就不必过多查询服务器):

SELECT id, content FROM videos ORDER BY RAND() LIMIT 15

将这十五个交给浏览器并允许它请求视频。一旦它看到十五个,它就可以向服务器请求另外三个。它可以通过存储已播放的视频 ID 来跳过已播放的视频。

如果您通过 Ajax 和 JSON 传递结果,您只需返回结果数组的串联:

<?php
$query = $pdo->query('SELECT id, content FROM videos ORDER BY RAND() LIMIT 10');
$videos = $query->fetchAll();
echo json_encode($videos);
?>

然后在 JS 中:

(function playRandomVideos() {  //Closures are cool
    $.getJSON('getRandomVideos.php', {success: function(videos) {
        for(video in videos) {
            if(video.id in playRandomVideos.played) {
                continue;
            }
            play(video);
            playRandomVideos.played.push(video.id);
        }
        playRandomVideos();
    }});
})();
playedRandomVideos.played = [];

You could just use (We'll grab fifteen so we don't have to query the server so much):

SELECT id, content FROM videos ORDER BY RAND() LIMIT 15

Hand those fifteen to the browser and allow it to request the videos. Once it has seen the fifteen, it can ask the server for three more. It can skip ones it's already played by storing played video ids.

If you are delivering results via Ajax and JSON you can just return the concatenation of the results arrays:

<?php
$query = $pdo->query('SELECT id, content FROM videos ORDER BY RAND() LIMIT 10');
$videos = $query->fetchAll();
echo json_encode($videos);
?>

Then in JS:

(function playRandomVideos() {  //Closures are cool
    $.getJSON('getRandomVideos.php', {success: function(videos) {
        for(video in videos) {
            if(video.id in playRandomVideos.played) {
                continue;
            }
            play(video);
            playRandomVideos.played.push(video.id);
        }
        playRandomVideos();
    }});
})();
playedRandomVideos.played = [];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文