[英]How can I loop through 2 MySQL tables but display based on each rows timestamp?
I have 2 MySQL tables, one that I used to store comments and one that I use to store document file paths etc.我有 2 个 MySQL 表,一个用于存储注释,一个用于存储文档文件路径等。
CommentID评论ID | Comment评论 | Timestamp时间戳 |
---|---|---|
1 1 | Hello你好 | 02/08/2022 2022 年 2 月 8 日 |
DocumentID文档编号 | FilePath文件路径 | Timestamp时间戳 |
---|---|---|
1 1 | file.php文件.php | 04/08/2022 2022 年 4 月 8 日 |
The above is a rough breakdown of my structure for each table and I want to loop through each table to display data but I want to be able to sort it by Timestamp and have it displayed like the image below so the comments and documents merge into 1 HTML table, how would I do this?以上是我对每个表的结构的粗略分解,我想遍历每个表以显示数据,但我希望能够按时间戳对其进行排序并使其显示如下图所示,以便评论和文档合并为 1 HTML 表,我该怎么做?
If you want to have the SQL give you all the results already ordered, you could adjust your query to use UNION on the 2 tables, and ORDER BY Timestamp.如果您想让 SQL 为您提供所有已排序的结果,您可以调整查询以在 2 个表上使用 UNION,并按时间戳排序。
If you'd rather stick with separate SQL results for each table, consider using the datatables.net table library.如果您更愿意为每个表使用单独的 SQL 结果,请考虑使用 datatables.net 表库。 It's free, easy to use, and you can set it to automatically sort your table by any column you specify.它是免费的、易于使用的,您可以将其设置为按您指定的任何列自动对表格进行排序。 Has a lot of other useful features as well.还有很多其他有用的功能。 I'd recommend using datatables.net tables even if you decide to get your results ordered in the SQL.即使您决定在 SQL 中订购结果,我也建议您使用 datatables.net 表。
ALTERNATIVELY... if you dont like either of these, and would like to sort the 2 result sets manually in your php, you can merge the 2 SQL result arrays, then use a usort()
function... ALTERNATIVELY... if you dont like either of these, and would like to sort the 2 result sets manually in your php, you can merge the 2 SQL result arrays, then use a usort()
function...
So assuming your 2 SQL results look something like this:因此,假设您的 2 SQL 结果如下所示:
$comments = array(
array(
'CommentID' => 1,
'Comment' => 'One',
'Timestamp' => '02/08/2022'
),
array(
'CommentID' => 2,
'Comment' => 'Two',
'Timestamp' => '05/08/2022'
)
);
$documents = array(
array(
'DocumentID' => 1,
'FilePath' => 'File 1',
'Timestamp' => '04/08/2022'
),
array(
'DocumentID' => 2,
'FilePath' => 'File 2',
'Timestamp' => '03/08/2022'
)
);
first merge the 2 arrays:首先合并2个arrays:
$rows = array_merge($comments, $documents);
now all the results are in 1 array.现在所有结果都在 1 个数组中。 next, create a usort()
function to sort all the results by the value of the Timestamp
key:接下来,创建一个usort()
function 以按Timestamp
键的值对所有结果进行排序:
usort($rows, function($a, $b) {
return (strtotime($a['Timestamp'])) - (strtotime($b['Timestamp']));
});
Now, the $rows
variable looks like this:现在, $rows
变量如下所示:
Array
(
[0] => Array
(
[CommentID] => 1
[Comment] => One
[Timestamp] => 02/08/2022
)
[1] => Array
(
[DocumentID] => 2
[FilePath] => File 2
[Timestamp] => 03/08/2022
)
[2] => Array
(
[DocumentID] => 1
[FilePath] => File 1
[Timestamp] => 04/08/2022
)
[3] => Array
(
[CommentID] => 2
[Comment] => Two
[Timestamp] => 05/08/2022
)
)
so all the data from both tables is sorted by timestamp, and you can just foreach through that $rows
array and put each data set in your table因此两个表中的所有数据都按时间戳排序,您可以通过该$rows
数组进行 foreach 并将每个数据集放入表中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.