I have an html table which is used for viewing the timelog of a user from the userlist. Once a view button is clicked, it will redirect to a page where the first and last timelog of a specific day will appear inside the <table>
.
Here is an example of the data inside tbl_timelog of db_time
id | user_id | date | time | am_pm
1 | literal01 | Apr 5, 2023 | 09:49 | AM
2 | adlaon05 | Apr 5, 2023 | 09:50 | AM
3 | ong07 | Apr 5, 2023 | 10:01 | AM
4 | literal01 | Apr 5, 2023 | 17:20 | PM
5 | adlaon05 | Apr 5, 2023 | 17:51 | PM
6 | ong07 | Apr 5, 2023 | 18:13 | PM
7 | ong07 | Apr 7, 2023 | 10:11 | AM
8 | literal01 | Apr 7, 2023 | 10:11 | AM
9 | adlaon05 | Apr 7, 2023 | 10:11 | AM
10 | literal01 | Apr 7, 2023 | 10:16 | AM
11 | literal01 | Apr 7, 2023 | 17:12 | PM
12 | ong07 | Apr 7, 2023 | 17:12 | PM
13 | adlaon05 | Apr 7, 2023 | 17:15 | PM
Expected output:
user_id: literal01
+-------+---------------+---------------+
|Date | Apr 5, 2023 | Apr 7, 2023 |
+-------+-------+-------+-------+-------+
|Time | 09:49 | 17:20 | 10:11 | 17:12 |
+-------+-------+-------+-------+-------+
user_id: adlaon05
+-------+---------------+---------------+
|Date | Apr 5, 2023 | Apr 7, 2023 |
+-------+-------+-------+-------+-------+
|Time | 09:50 | 17:51 | 10:11 | 17:15 |
+-------+-------+-------+-------+-------+
user_id: ong07
+-------+---------------+---------------+
|Date | Apr 5, 2023 | Apr 7, 2023 |
+-------+-------+-------+-------+-------+
|Time | 10:01 | 18:13 | 10:11 | 17:12 |
+-------+-------+-------+-------+-------+
database connection:
$server_name = "localhost";
$db_username = "root";
$db_password = "";
$db_name = "db_time";
$connection = mysqli_connect($server_name,$db_username,$db_password,$db_name);
query:
$query = "SELECT * FROM `tbl_timelog` WHERE `user_id` = '$user_id'";
$query_run = mysqli_query($connection, $query);
if($query_run){
$_SESSION['id'] = $user_id;
}
table:
<table>
<thead>
<tr>
<th scope="col">Date</th>
<?php
$dates = array();
foreach($query_run as $row) {
if (in_array($row['date'], $dates)) {
continue;
}
$dates[] = $row['date'];
{ ?>
<th scope="col" colspan="2"><?php echo $row['date'];?></th>
<?php } }?>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Time</th>
<?php
$out = array();
foreach($query_run as $row)
{
if (in_array($row['am_pm'], $out))
{
continue;
}
$out[] = $row['am_pm'];
{?>
<td><?php echo $row["time"];?></td>
<?php } }?>
</tr>
</tbody>
</table>
Here is the result for each user_id.
user_id: literal01
+-------+---------------+-------------+
|Date | Apr 5, 2023 | Apr 7, 2023 |
+-------+---------------+-------------+
|Time | 09:49 | 17:20 |
+-------+---------------+
user_id: adlaon05
+-------+---------------+-------------+
|Date | Apr 5, 2023 | Apr 7, 2023 |
+-------+---------------+-------------+
|Time | 09:50 | 17:51 |
+-------+---------------+
user_id: ong07
+-------+---------------+-------------+
|Date | Apr 5, 2023 | Apr 7, 2023 |
+-------+---------------+-------------+
|Time | 10:01 | 18:13 |
+-------+---------------+
Timelog is not reflected on Apr 7, 2023.