When I display the number of descriptions by date, I encounter a problem. That is, the number of descriptions does not match what is in my database.
This is the table from My database
This is result :
----------------------------------------------------
| Tanggal | A | I | S | H |
----------------------------------------------------
| 2018-01-01 | 1 | 1 | 3 | 1 |
| 2018-01-02 | 1 | 1 | 3 | 1 |
----------------------------------------------------
It should be like this :
----------------------------------------------------
| Tanggal | A | I | S | H |
----------------------------------------------------
| 2018-01-01 | 1 | 1 | 0 | 1 |
| 2018-01-02 | 0 | 0 | 3 | 0 |
----------------------------------------------------
This is My Controllers :
public function absen()
{
$data['getall'] = $this->m_absen->getall();
$data['alpa'] = $this->m_absen->alpa();
$data['izin'] = $this->m_absen->izin();
$data['sakit'] = $this->m_absen->sakit();
$data['hadir'] = $this->m_absen->hadir();
$this->load->view('admin/absen/v_absen', $data);
}
This is My Models :
public function getall()
{
$query = $this->db->query("SELECT * FROM tbl_absen_siswa group by tanggal");
return $query->result();
}
public function alpa()
{
$query = $this->db->query("select count(*) as total_alpa from tbl_absen_siswa where keterangan='A'");
return $query->row();
}
public function izin()
{
$query = $this->db->query("SELECT COUNT(*) AS total_izin FROM tbl_absen_siswa WHERE keterangan='I'");
return $query->row();
}
public function sakit()
{
$query = $this->db->query("select count(*) as total_sakit, nama_siswa from tbl_absen_siswa where keterangan='S'");
return $query->row();
}
public function hadir()
{
$query = $this->db->query("SELECT COUNT(*) AS total_hadir FROM tbl_absen_siswa WHERE keterangan='H'");
return $query->row();
}
This is My Views :
<table class="table table-striped table-bordered table-hover dt-responsive" width="100%" id="sample_1">
<thead>
<tr>
<th width="5%">Tanggal</th>
<th width="3%">A</th>
<th width="3%">I</th>
<th width="3%">S</th>
<th width="3%">H</th>
</tr>
</thead>
<tbody>
<?php foreach($getall as $g){ ?>
<tr>
<td><?php echo $g->tanggal; ?></td>
<td><?php echo $alpa->total_alpa; ?></td>
<td><?php echo $izin->total_izin; ?></td>
<td><?php echo $sakit->total_sakit; ?>
<td><?php echo $hadir->total_hadir; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
you just need one query for that kind of task
your model
public function getall()
{
$query = $this->db
->select('tanggal, count(keterangan) AS count, keterangan')
->from('tbl_absen_siswa')
->group_by('tanggal, keterangan')
->get();
return $query->result();
}
your controller
public function absen()
{
$arrResult = $this->m_absen->getall();
$arrData = [];
foreach($arrResult AS $objItem)
{
$arrData[$objItem->tanggal][$objItem->keterangan] = $objItem->count;
}
$this->load->view('so/so50683515', array('arrData' => $arrData));
}
and your view
<table class="table table-striped table-bordered table-hover dt-responsive" width="100%" id="sample_1">
<thead>
<tr>
<th width="5%">Tanggal</th>
<th width="3%">A</th>
<th width="3%">I</th>
<th width="3%">S</th>
<th width="3%">H</th>
</tr>
</thead>
<tbody>
<?php foreach($arrData as $date => $arrItem)
{
?>
<tr>
<td><?php echo $date; ?></td>
<td><?php echo (isset($arrItem['A'])) ? $arrItem['A'] : 0; ?></td>
<td><?php echo (isset($arrItem['I'])) ? $arrItem['I'] : 0; ?></td>
<td><?php echo (isset($arrItem['S'])) ? $arrItem['S'] : 0; ?></td>
<td><?php echo (isset($arrItem['H'])) ? $arrItem['H'] : 0; ?></td>
</tr>
<?php } ?>
</tbody>
</table>