使用表格通过php按日期显示记录

4xrmg8kj  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(320)

第一张图像是我数据库中的记录,“enr_biometrics_id”是一个用户id,我只是这样命名。不要输入“日志号”
数据表

现在,我想显示日期为2021-07-01-2021-07-15的enr_biometrics_id='00000000 1'的记录,就像下面的图像一样,在日期为2021-07-01-2021-07-15的无记录上使用并显示空白。提前感谢您的帮助!

我现在有一个密码:

<?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>

现在就得到这样的结果

vnzz0bqm

vnzz0bqm1#

这将是我对您提供的信息的变通方法。这可能是有可能的,以优化这一点,但它提供了一个很好的解决方案的想法。
简而言之,我检查当前行是否与前一行具有相同的字符串,如果不是这种情况,我将创建一个新行

$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>';

相关问题