首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何根据数据库中包含的信息量显示日期数据?

如何根据数据库中包含的信息量显示日期数据?
EN

Stack Overflow用户
提问于 2018-06-04 23:02:18
回答 1查看 49关注 0票数 -1

当我按日期显示描述的数量时,我遇到了一个问题。也就是说,描述的数量与我的数据库中的内容不匹配。

这是我的数据库中的表

这是结果:

----------------------------------------------------
|      Tanggal      |   A  |   I   |   S   |   H   |
----------------------------------------------------
|     2018-01-01    |   1  |   1   |   3   |   1   |  
|     2018-01-02    |   1  |   1   |   3   |   1   |
----------------------------------------------------

应该是这样的:

----------------------------------------------------
|      Tanggal      |   A  |   I   |   S   |   H   |
----------------------------------------------------
|     2018-01-01    |   1  |   1   |   0   |   1   |  
|     2018-01-02    |   0  |   0   |   3   |   0   |
----------------------------------------------------

这是我的控制器:

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);
}

这是我的模型:

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();
}

这是我的观点:

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

回答 1

Stack Overflow用户

发布于 2018-06-05 05:26:31

对于这类任务,您只需要一个查询

你的模型

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();
}

您的控制器

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));
}

你的观点是

<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50683515

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档