Skip to content
Advertisement

How to view data based on values ​from database ci3 [closed]

I want to make a recapitulation of absent data, which I display using a table in CI. The problem that occurs to me is that the data that appears in the table is not in date order.

enter image description here

I want to make a report like this enter image description here

The absence data does not match based on the date data above the table

How to display the data according to the date data that I called from the database?

Here is my code:

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

Advertisement

Answer

I think you’re looking for this:

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

When displaying the $absen data from the database, loop over the $tampiltgl array that you used to print the dates in the header.

For each date in the $tampiltgl array, check if there is an entry in the $absen array with the same date. If a match is found, print the status. If no match was found print an empty table cell.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement