PHP/MySQL:如何保存媒体播放器播放列表顺序

发布于 2024-12-12 03:46:50 字数 351 浏览 0 评论 0原文

我目前正在使用 Javascript、PHP 和 MySQL 构建一个 Web 应用程序(媒体播放器)。它将用于上传视频并将其存储到播放列表中,然后用户可以使用该播放列表将视频按所需顺序放置。

上传的文件保存在 MySQL 数据库中(id、标题、文件名和位置,...)。问题是,如何保存用户放置播放列表的顺序?

我认为在数据库中添加“位置”字段不是一个好主意,因为每次更新都会运行太多查询。我想到了一个“下一个”字段,它指向列表中下一个视频的 id,这“仅”需要每次更新最多 3 个查询(对吗?)。

有没有更好的方法来存储播放列表的顺序?它并不特别必须在数据库中,我想它也可以是一个文本文件。

建议?

谢谢!

I am currently building a web application (a media player) using Javascript, PHP and MySQL. It will be used to upload videos and store them into a playlist, which can then be used by the user to place the videos in the desired order.

Uploaded files a saved in a MySQL database (id, title, file name and location, ...). The question is, how can I save the order in which the user puts the playlist?

I don't think adding a "position" field in the database is a good idea, as too many queries will be ran with each update. I thought of a "next" field, which points to the id of the next video in the list, which would "only" require a maximum of 3 queries for each update (right?).

Is there a better way to store the order of the playlist? It doesn't particularly have to be in the database, it could be a text file as well I suppose.

Suggestions?

Thanks!

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

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

发布评论

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

评论(3

冬天旳寂寞 2024-12-19 03:46:50

我认为“位置”字段就可以了。您可以在更改轨道位置后用一条语句更新它,如下所示:

UPDATE track SET position=(CASE 
    WHEN position > :oldPos AND position <= :newPos THEN position - 1 
    WHEN position < :oldPos AND position >= :newPos THEN position + 1
    WHEN position = :oldPos THEN :newPos
    ELSE position
) WHERE playlist_id = :playlist_id

插入新轨道时,只需将 :oldPos 设置为一个大值(大于列表中的任何位置,例如列表长度 + 1)并将 :newPos 设置为插入的轨道位置,删除轨道时将 :oldPos 设置为删除的轨道位置并将 :newPos 设置为一个大值。

I think a 'position' field would be OK. You could update it with one statement after changing a track position, like this:

UPDATE track SET position=(CASE 
    WHEN position > :oldPos AND position <= :newPos THEN position - 1 
    WHEN position < :oldPos AND position >= :newPos THEN position + 1
    WHEN position = :oldPos THEN :newPos
    ELSE position
) WHERE playlist_id = :playlist_id

When inserting a new track just set :oldPos to a big value (bigger than any position on the list, so for example list length + 1) and :newPos to inserted track position, when removing a track set :oldPos to removed track position and :newPos to a big value.

2024-12-19 03:46:50

堆栈方法(即使用下一个字段)不是一个好主意,因为如果您需要在播放列表中间插入一个视频,那么您需要追逐下一个视频。

最简单的方法是仅使用位置字段。当您重新排序时,您将更改与堆栈方法相同数量的信息,但所需的计算量会少得多。

The stack approach (i.e. using A next field) isn't a good idea because if you needed to insert a video in the middle of a playlist then you need to chase through your next.

The easiest method would be to just use a position field. when you re-order you will change the same amount of information as a stack approach but there will be much less computation needed.

靑春怀旧 2024-12-19 03:46:50

我想我会为每个用户创建一个目录,并保存一个包含播放列表的 XML 文件(存储文件的路径和名称)或将它们存储在数据库中(用户、播放列表(id、id_user、名称)、媒体(id、描述) ,路径),x_playlists_media(id,id_playlist,id_media,位置)。

I think I would make a directory for each user and save a XML files (storing the path and name of the file) containing playlists or store them in a DB (users,playlists(id,id_user,name),media(id,description,path), x_playlists_media(id,id_playlist,id_media,position).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文