PHP/SQL 每 x 分钟循环一次项目

发布于 2024-08-12 06:50:05 字数 254 浏览 6 评论 0原文

我有一个经营在线拍卖网站的朋友。目前,他在主页上有一个特色项目部分,他希望每 X 分钟循环一次项目。该网站运行一个 MySQL 数据库,但我还没有真正见过它。

他当前使用的代码是一段又大又长、混乱的 Javascript 代码,会导致各种错误。

这些项目必须按顺序循环,当到达终点时,返回并再次重复。

使用 PHP 实现此目的的最佳方法是什么?

编辑:我的意思是从后端 SQL 的角度来看。不是用户界面。

谢谢 本

I have a friend who runs an online auction website. He currently has a featured items section on the homepage that he wants to have cycle an item every X amount of minute. The site runs off a MySQL database which I haven't actually seen yet.

The current code that he is using is a big, long messy Javascript code that is causing all kinds of errors.

The items would have to cycle in order and when they get to the end, go back and repeat again.

What would be the best approach to take to this using PHP?

EDIT: I mean from a backend SQL perspective. Not the UI.

Thanks
Ben

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

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

发布评论

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

评论(1

故人爱我别走 2024-08-19 06:50:05

假设您有一个单独的特色项目表(可能有一个引用主项目表的项目 ID,也可能有其他信息)...在此表中,添加一个 last_featured 列来表示该项目的时间最后显示。从那里,您可以操纵查询来获取特色项目的轮换列表。

它可能看起来像这样(作为 PHP 和 MYSQL 之间奇怪的伪代码混合):

// select the most recent item in the list, considered the current item
$item = SELECT * FROM featured_items ORDER BY last_featured DESC LIMIT 1;

if ($item['last_featured'] > X_minutes_ago) {
    // select the oldest item in the list, based on when it was last featured
    $item = SELECT * FROM featured_items ORDER BY last_featured ASC LIMIT 1;

    // update the selected item so it shows as the current item next request
    UPDATE featured_items SET last_featured=NOW() WHERE item_id = $item['item_id'];
}

请注意,这需要 3 次调用数据库...可能有更有效的方法来完成此操作。

Assuming you have a separate table for the featured items (probably has an item ID referencing the main items table and maybe other info)... In this table, add a last_featured column to represent the time the item was last shown. From there, you can manipulate your queries to get a rotating list of featured items.

It might look something like this (as a weird pseudocode mix between PHP & MYSQL):

// select the most recent item in the list, considered the current item
$item = SELECT * FROM featured_items ORDER BY last_featured DESC LIMIT 1;

if ($item['last_featured'] > X_minutes_ago) {
    // select the oldest item in the list, based on when it was last featured
    $item = SELECT * FROM featured_items ORDER BY last_featured ASC LIMIT 1;

    // update the selected item so it shows as the current item next request
    UPDATE featured_items SET last_featured=NOW() WHERE item_id = $item['item_id'];
}

Note that this requires 3 call to the database... There may be a more efficient way to accomplish this.

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