douxian9706 2011-08-31 11:08
浏览 28
已采纳

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

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.

  • 写回答

2条回答 默认 最新

  • dongyu3967 2011-08-31 11:14
    关注

    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); }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog