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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。