如何根据数据库 ci3 中的值查看数据

How to view data based on values ​from database ci3

我想对缺失的数据进行概括,我在 CI 中使用 table 显示这些数据。我遇到的问题是 table 中出现的数据不是按日期顺序排列的。

我想做这样的报告 enter image description here

根据table

上方的日期数据,缺勤数据不匹配

如何根据我从数据库调用的日期数据显示数据?

这是我的代码:

<tbody>
    <tr class="text-center">
        <td>Nip</td>
        <td>Nama Lengkap</td>
         <?php
        foreach ($tampiltgl as $data) :?> 
          <td>
              <?php echo $data['waktu']; ?>
      </td>
         <?php endforeach; ?>  
</thead>
  
<tbody>
     <?php
    $no=1; 
    foreach ($tampil as $data) :?>
      
         <tr>
                <td><?php echo $data['nip']; ?> </td>
              
            <td>
                <?php echo $data['nm_lengkap']; ?> 
            </td>
          
            <?php
            
             date_default_timezone_set("Asia/Jakarta");
             $timestamp = date('m');
             $opd=$this->session->userdata('opd');
             $tgl=$this->db->query ("SELECT DISTINCT waktu FROM absen_pagi where opd='$opd'AND MONTH(waktu) ='$timestamp'")->result_array();
        
             $absen = $this->db->query ("SELECT DISTINCT waktu, status from absen_pagi where nip='".$data['nip']."' AND opd='$opd' AND  MONTH(waktu) ='$timestamp'")->result_array();
             
            
             foreach ($absen as $key) :?>

           <td><?php echo $key['status']; ?>  </td>
        </td>
        <?php endforeach; ?>
     <?php endforeach; ?>
</tbody>
</table>

我想你正在寻找这个:

<table>
<thead>
    <tr class="text-center">
        <td>Nip</td>
        <td>Nama Lengkap</td>
         <?php
        foreach ($tampiltgl as $data) :?> 
          <td>
              <?php echo $data['waktu']; ?>
      </td>
         <?php endforeach; ?>  
    </tr>
</thead>
  
<tbody>
     <?php
    $no=1; 
    foreach ($tampil as $data) :?>
      
         <tr>
                <td><?php echo $data['nip']; ?> </td>
              
            <td>
                <?php echo $data['nm_lengkap']; ?> 
            </td>
          
            <?php
            
             date_default_timezone_set("Asia/Jakarta");
             $timestamp = date('m');
             $opd=$this->session->userdata('opd');
             $tgl=$this->db->query ("SELECT DISTINCT waktu FROM absen_pagi where opd='$opd'AND MONTH(waktu) ='$timestamp'")->result_array();
        
             $absen = $this->db->query ("SELECT DISTINCT waktu, status from absen_pagi where nip='".$data['nip']."' AND opd='$opd' AND  MONTH(waktu) ='$timestamp'")->result_array();
             
            foreach ($tampiltgl as $tampiltgldata) {
                $celldata = '';
                foreach ($absen as $key) {
                    if ($tampiltgldata['waktu'] == $key['waktu']) {
                        $celldata = $key['status'];
                        break;
                    }
                }
                echo "<td>$celldata</td>";
            } ?>
        </tr>
    <?php endforeach; ?>
</tbody>
</table>

当显示来自数据库的 $absen 数据时,循环遍历用于在 header 中打印日期的 $tampiltgl 数组。

对于 $tampiltgl 数组中的每个日期,检查 $absen 数组中是否有相同日期的条目。如果找到匹配项,则打印状态。如果未找到匹配项,则打印一个空的 table 单元格。