简体   繁体   English

每隔x分钟PHP / SQL循环项目

[英]PHP/SQL Cycle items every x minutes

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. 他目前在主页上有一个特色项目部分,他希望每X分钟循环一次项目。 The site runs off a MySQL database which I haven't actually seen yet. 该网站运行的是我实际上尚未见过的MySQL数据库。

The current code that he is using is a big, long messy Javascript code that is causing all kinds of errors. 他正在使用的当前代码是一个大而又混乱的Javascript代码,它会导致各种错误。

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? 使用PHP进行此操作的最佳方法是什么?

EDIT: I mean from a backend SQL perspective. 编辑:我的意思是从后端SQL的角度来看。 Not the UI. 不是UI。

Thanks Ben 谢谢本

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. 假设您为特色商品有一个单独的表(可能有一个商品ID引用了主商品表,也许还有其他信息)...在此表中,添加一个last_featured列来表示商品的最后显示时间。 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): 它可能看起来像这样(作为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. 请注意,这需要对数据库的3次调用...可能有一种更有效的方法来完成此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM