问题|从数据库获取随机值,如何不重复它们
我正在创建一个从数据库获取随机视频的网站。 我的问题是,我获取随机视频的代码是这样的:
select id,content from videos order by rand() limit 1
并且我不希望用户看到相同的视频,直到之前播放过其他 3 个视频(至少)。
您对如何做到这一点有什么建议吗? 这就是我的网站目前的运作方式。
- HTML-AJAX(调用视频 URL)
- PHP(返回随机视频 URL) 一个视频。
- 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.
- HTML-AJAX(CALL FOR VIDEO URL)
- PHP(RETURN RANDOM VIDEO URL) One video.
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
视频id可以发给客户端吗?然后客户端(Javascript)从那里请求视频。下面是如何播放的:
在 javascript 中,它可能如下所示:
Could the video id be sent to the client? Then from there the client (Javascript) request the video. Here how that could be played:
In javascript it might look like :
您可以使用(我们将获取十五个,这样我们就不必过多查询服务器):
将这十五个交给浏览器并允许它请求视频。一旦它看到十五个,它就可以向服务器请求另外三个。它可以通过存储已播放的视频 ID 来跳过已播放的视频。
如果您通过 Ajax 和 JSON 传递结果,您只需返回结果数组的串联:
然后在 JS 中:
You could just use (We'll grab fifteen so we don't have to query the server so much):
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:
Then in JS: