简体   繁体   English

根据时间戳获取上一个和下一个网址

[英]get previous and next url based on timestamp

I have a page that displays all projects ordered by dates. 我有一个页面,显示按日期排序的所有项目。 When you click on a project you are brought to a page project-single.php?id=123 where I have a previous and next button. 当您单击一个项目时,将带到页面project-single.php?id=123 ,其中有一个上一个和下一个按钮。 All of the projects have an id (which is in no sequential order aka not auto_incremented), and a date of creation timestamp (in the form of 2012-07-20 00:36:20) in the projects table in the database. 所有projects在数据库的projects表中都有一个id(不按顺序排列,也称为auto_incremented),以及创建时间戳记的日期(格式为2012-07-20 00:36:20)。 I am trying to think of an efficient way to get the id of the project both before and after the id=123 in terms of date for the prev next buttons. 我正在尝试一种有效的方式来获取项目prev next按钮的日期之前和之后的id=123的项目id=123 But I cannot think of an easier way than creating an array with the id and date_created and comparing dates; 但是我想不出一种比使用iddate_created创建数组并比较日期更简单的方法。 which as you can imagine would get quite taxing on the server as this is a page accessed often. 您可以想象,这是一个经常访问的页面,因此会对服务器造成很大的负担。 Does anyone have a cleaner solution? 有人有更清洁的解决方案吗?

You might consider changing your timestamp to a Unix timestamp instead of a string. 您可以考虑将时间戳更改为Unix时间戳,而不是字符串。 Unix timestamps are simple integers, which are easy and efficient for a database to compare. Unix时间戳是简单的整数,对于数据库而言,它们很容易且高效。

With a comparison operator, an index on the timestamp, and a LIMIT 1, you should have a very fast SQL statement. 使用比较运算符,时间戳记上的索引和LIMIT 1,您应该具有非常快速的SQL语句。 Here's some example code: 这是一些示例代码:

-- For getting the next project.
SELECT * FROM projects WHERE date_created > :current_project_date ORDER BY date_created ASC LIMIT 1;
-- For getting the previous project.
SELECT * FROM projects WHERE date_created < :current_project_date ORDER BY date_created DESC LIMIT 1;

I'm not familiar with MySQL, so these results may need some tweaking, but that's the gist. 我对MySQL不熟悉,因此可能需要进行一些调整,但这就是要点。

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

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