2

我想根据我当前的计算机时间在列中显示来自 MySQL 数据库的当前帖子。

如果您在下面看到,[POST0] 到 POST[4] 是在 6:00 到 7:00 之间上传的,在第一列中我想根据我当前的时间显示最新的帖子。

示例:我的当前时间是 6:50,所以在第一列中我希望它始终显示 6:00 到 7:00 之间上传的最新帖子。我希望它总是根据每小时的帖子显示在下一列中的旧帖子,所以 [POST5] 到 POST[9] 是 5:00 到 6:00 之间的帖子,依此类推..

我不知道如何让第一列始终根据我的时间显示帖子,所以我想要一些建议..

+------------------------ +--------------+
|            |            |
|   [POST0]  |   [POST5]  |
|   [6:59]   |   [5:59]   |
+------------+------------+--------------+
|            |            |
|   [POST1]  |   [POST6]  |
|   [6:31]   |   [5:58]   |
|            |            |
+------------+------------+
|            |            |
|   [POST2]  |   [POST7]  |
|   [6:08]   |   [5:56]   |
|            |            |
+------------+------------+
|            |            |
|   [POST3]  |   [POST8]  |
|   [6:01]   |   [5:25]   |
|            |            |
+------------+------------+
|            |            |
|   [POST4]  |   [POST9]  |
|   [6:00]   |   [5:11]   |
|            |            |
+----6:00----+----5:00----+--------------+

EDITED

4

1 回答 1

1

Two techniques. Both rely on PHP logic rather than a query, because as the comments say, the two columns are independent of each other (even the number of responses is not equal)

  • Technique 1: run one query, returning posts in time order, latest first. That's your SQL, it's really simple. You could restrict how many you get with a limit, or how far back with where. That's your SQL. Then using PHP, run a loop that goes through the records and displays the hour's posts, until the timestamp indicates a different hour; followed by a second loop to display earlier posts.

  • Technique 2: run two queries. The first will return this hour's posts. The second returns earlier ones, up to whatever row limit or time back you decide. Then display the data of each query by running two successive loops.

Personnally I'd use technique 1. It's more flexible - say you change your mind and decide you want to hightlight the last 1/2 hour's posts, or the last 2 hours' posts. It still allows you to present the results as you like.

Also, don't present the results in a table. You don't know how many rows it will be. You don't know whether the number of older posts (the height of column 2) is more or less than recent posts (the height of column one). You may change your mind later about whether you want these posts presented side by side or the older ones smaller or any other good interaction pattern that you may come up with in future.

So - instead of embedding the presentation in a complicated query that returns two columns of unrelated data, run a simple query that returns your data in time order, then use application logic to present in the most suitable way. It's simple, it's flexible, and it keeps presentation logic apart from data logic.

于 2013-05-17T09:37:40.957 回答