简体   繁体   中英

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.

CommentID Comment Timestamp
1 Hello 02/08/2022
DocumentID FilePath Timestamp
1 file.php 04/08/2022

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?

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.

If you'd rather stick with separate SQL results for each table, consider using the datatables.net table library. 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.

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:

$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:

$rows = array_merge($comments, $documents);

now all the results are in 1 array. next, create a usort() function to sort all the results by the value of the Timestamp key:

usort($rows, function($a, $b) {
    return (strtotime($a['Timestamp'])) - (strtotime($b['Timestamp']));
});

Now, the $rows variable looks like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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