Zeus
Zeus

Reputation: 21

Display Records by date via PHP using table

The first image are my records in my database, 'enr_biometrics_id' is a user_id i just name it like that. nevermind the 'log_no'

datatable datatable

Now, I want to display the records of enr_biometrics_id = '000000001' from date 2021-07-01 - 2021-07-15, just like the image below using and display blanks on no record on the date 2021-07-01 - 2021-07-15. Thanks for the help in advance!

2nd image

I have a code right now:

    <?php include('config/config.php'); ?>
<table border="1">
<tr>
    <th>Log Date</th>
    <th>Logs</th>
    <th>Logs</th>
    <th>Logs</th>
    <th>Logs</th>
    <th>Logs</th>
</tr>
<?php
$start_period = "2021-07-01";
$end_period = "2021-07-15";

date_default_timezone_set('UTC');

while (strtotime($start_period) <= strtotime($end_period)) {
    
?>
<tr>
    <td><?php echo "$start_period"; ?></td>
    <?php
    $logs_query = mysqli_query($conn, "SELECT * FROM tb_daily_time_record WHERE date_added BETWEEN '$start_period' AND '$end_period' AND enr_biometrics_id='000000001' ")or(die(mysql_error()));
        
            while($row_logs = mysqli_fetch_assoc($logs_query)){
                $date_logs = $row_logs['date_added'];
                $t_logs = $row_logs['time_log'];
    ?>
        <td>
        <?php 
            echo $t_logs;
        ?>
    </td>
    <?php 
        }
    ?>
</tr>
<?php 
 $start_period = date ("Y/m/d", strtotime("+1 days", strtotime($start_period)));
}
?>
<tr>
</table>

Getting a result like this MyCoderightnow

Upvotes: 2

Views: 307

Answers (1)

Kaede
Kaede

Reputation: 198

This would be my workaround on the information you are giving. It's probably posible to optimise thise but it gives a good idea of a solution.

Short sum, i check if the current row has the same string as the row before, if this isnt the case i create a new row

$result = 'SELECT * FROM table'; // query .. 

$checkdate='';

echo '<table>';
foreach ($result as $row) {
  if ($row['date_added'] != $checkdate) {
    $checkdate = $row['date_added'];

    echo '</tr>
    <tr>
    <td>'.$row['time_log'].'</td>
    <td>'.$row['time_log'].'</td>';
    continue;
  }
  echo '<td>'.$row['time_log'].'</td>';
} 
echo '</tr></table>';

Upvotes: 1

Related Questions