简体   繁体   English

如何循环遍历 2 个 MySQL 表但根据每行时间戳显示?

[英]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 表,我该怎么做?

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.

相关问题 MySQL基于时间戳循环遍历数据库? - Mysql loop through database based on timestamp? 如何遍历所有类别以分别显示每个类别帖子? - How can I loop through all categories to display each category post separately? 如何显示来自数据库的结果,用于每个循环来循环遍历ID - How can I display results from database using for each loop to cycle through IDs 如何在循环中的每个文本框中显示值,该值基于在其 combobox 中选择的值? - How can I display value in each textbox in loop that the value based on the value selected in its combobox? 在wordpress中循环浏览ACF中继器行,并分别显示每一行 - Loop through ACF repeater rows in wordpress and display each row separately 如何在每个循环外显示一个数组? - How can i display an array outside the for each loop? 如何将每个记录分别显示为while循环? - How can I display each record individually into a while loop? 如何遍历并一次显示1个DB记录? - How can I loop through and display 1 DB record at a time? 如何使用 for 或 foreach 循环运行 DateTime 以循环每一天并为每个循环添加 +3 天? - How can I run a DateTime with for or foreach loop to loop through each day and add +3 days to each? Mysql-如果只有时间戳,如何按年份选择行? - Mysql - How can I select rows by year if I have only the timestamp?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM