如何在codeigniter 3的一条记录中显示数据?

Posted

技术标签:

【中文标题】如何在codeigniter 3的一条记录中显示数据?【英文标题】:how to display data in one record in codeigniter 3? 【发布时间】:2021-09-27 07:57:25 【问题描述】:

当我显示来自数据库的数据时,我发现数据重复的错误。我想根据相同的 siswa_id 仅在一条记录中显示数据

这是我的数据库中的表:

这是结果:

-----------------------------------------------------------------------
|  No   |  Nama Siswa   |  NIS      | Kelas   | Jul  | Agu  | Sep |
-----------------------------------------------------------------------
|  1    |Iwan Setiawan  |161710044  |10 RPL   | OK   |  X   |  X  |
|  2    |Iwan Setiawan  |161710044  |10 RPL   | X    | OK   |  X  |
|  3    |Iwan Setiawan  |161710044  |10 RPL   | X    |  X   |  OK |
-----------------------------------------------------------------------

应该是这样的:

-----------------------------------------------------------------------
|  No   |  Nama Siswa   |  NIS      | Kelas   | Jul  | Agu  | Sep |
-----------------------------------------------------------------------
|  1    |Iwan Setiawan  |161710044  |10 RPL   | OK   |  OK  |  OK |
-----------------------------------------------------------------------

这是我的控制器:

public function RekapPembayaran()

    if($this->M_login->logged_id())
       
        $data['judul'] = 'Rekap Pembayaran SPP';

        $data['rekap'] = $this->M_admin->rekap_pembayaran();

        $this->load->view('admin/pembayaran-spp/rekap-pembayaran', $data);
     else 
        //jika session belum terdaftar, maka redirect ke halaman login
        redirect("login");
    

这是我的模特:

public function rekap_pembayaran()

    $this->db->select('*');
    $this->db->from('tbl_siswa');
    $this->db->join('tbl_pembayaran', 'tbl_siswa.id_siswa=tbl_pembayaran.siswa_id');
    $this->db->join('tbl_rombel', 'tbl_siswa.rombel_id=tbl_rombel.id_rombel');      
    $this->db->order_by('nama_rombel, nama_siswa', 'ASC');
    $query = $this->db->get();
    return $query->result();

这是我的观点:

<tbody>
    <?php $no = 1;
        foreach ($rekap as $data)  ?>
            <tr>
                <td><?php echo $no; ?></td>
                <td><?php echo $data->nama_siswa; ?></td>
                <td><?php echo $data->nis; ?></td>
                <td><?php echo $data->nama_rombel; ?></td>
                <?php if($data->bulan_dibayar == 7) 
                    echo '<td><j class="btn btn-primary"><b>OK</b></j></td>';
                else 
                    echo '<td><j class="btn btn-danger"><b>X</b></j></td>';
                ?>
                <?php if($data->bulan_dibayar == 8) 
                    echo '<td><j class="btn btn-primary"><b>OK</b></j></td>';
                else 
                    echo '<td><j class="btn btn-danger"><b>X</b></j></td>';
                ?>
                <?php if($data->bulan_dibayar == 9) 
                    echo '<td><j class="btn btn-primary"><b>OK</b></j></td>';
                else 
                    echo '<td><j class="btn btn-danger"><b>X</b></j></td>';
                ?>
            </tr>
            <?php $no++;
         ?>
    </tbody>

【问题讨论】:

【参考方案1】:

我推荐你,更改查询。假设你的数据库是这样的:

 CREATE TABLE `prueba`.`test` 
(`id_pembayaran` INT NOT NULL , 
`siswa_id` INT NOT NULL , 
`siswa_descrip` VARCHAR(30) NOT NULL , 
`bulan_dibayar` INT NOT NULL , 
`tanggal_bayar` DATE NOT NULL ) 
ENGINE = InnoDB; 
ALTER TABLE `test` ADD PRIMARY KEY( `id_pembayaran`); 

INSERT INTO `test` (`id_pembayaran`, `siswa_id`, `siswa_descrip`, `bulan_dibayar`, `tanggal_bayar`) VALUES 
('4', '2', 'Iwan Setiawan ', '7', '2021-07-14'), 
('5', '2', 'Iwan Setiawan ', '8', '2021-07-15'), 
('6', '2', 'Iwan Setiawan ', '9', '2021-07-16'); 

那么你查询的应该是:

SELECT t.`siswa_id`, t.`siswa_descrip`,
(select  IF(t2.`bulan_dibayar` = 7, "OK", "X") from test t2 where t2.id_pembayaran = t.id_pembayaran)as 'JUL',
(select  IF(t2.`bulan_dibayar` = 7, "OK", "X") from test t2 where t2.id_pembayaran = t.id_pembayaran)as 'AUG',
(select  IF(t2.`bulan_dibayar` = 7, "OK", "X") from test t2 where t2.id_pembayaran = t.id_pembayaran)as 'SEP'
FROM `test` t 
group by  t.`siswa_id`

所以,您的模型应该如下所示:

public function rekap_pembayaran()
  
    $sql = '     SELECT t.`siswa_id`, t.`siswa_descrip`, 
                (select  IF(t2.`bulan_dibayar` = 7, "OK", "X") from test t2 where t2.id_pembayaran = t.id_pembayaran)as 'JUL',
                (select  IF(t2.`bulan_dibayar` = 7, "OK", "X") from test t2 where t2.id_pembayaran = t.id_pembayaran)as 'AUG',
                (select  IF(t2.`bulan_dibayar` = 7, "OK", "X") from test t2 where t2.id_pembayaran = t.id_pembayaran)as 'SEP'
                FROM `test` t 
                group by  t.`siswa_id`
           ';              
    
    $query  =   $this->db->query($sql); 
    
    if($query->num_rows()>0)  
        return $query->result();        
    
    else 
        return null;
              

【讨论】:

是的,如果只需要三个月就可以了。但在这里我需要十二个月(一月 - 十二月)。所以查询条件不只取“7”,这里有“1”代表“Jan”,“2”代表“Feb”,“3”代表“Mar”等等。 在这种情况下,你应该设置十二个条件。 是的,我已经添加了,但是对于另一个“siswa_descrip”,即使我们添加了它,该值仍然是“x” 已解决。我使用 max case 语句进行查询。这是查询: SELECT siswa_id, MAX(CASE WHEN bulan_dibayar = 1 THEN 'OK' END) AS JAN, MAX(CASE WHEN bulan_dibayar = 2 THEN 'OK' END) AS FEB, MAX(CASE WHEN bulan_dibayar = 3 THEN 'OK ' END) AS MAR, MAX(CASE WHEN bulan_dibayar = 4 THEN 'OK' END) AS APR, MAX(CASE WHEN bulan_dibayar = 5 THEN 'OK' END) AS MEI, MAX(CASE WHEN bulan_dibayar = 6 THEN 'OK' END ) AS JUN, MAX(CASE WHEN bulan_dibayar = 7 THEN 'OK' END) AS JUL, etc .. FROM tbl_pembayaran GROUP BY siswa_id

以上是关于如何在codeigniter 3的一条记录中显示数据?的主要内容,如果未能解决你的问题,请参考以下文章

在 codeigniter 中显示 2 个具有活动记录的表中的 3 个最新数据

一个表中的一条记录显示在另一个表上的所有记录中

使用 NSFetchedResultsController 时如何获取每个部分的一条记录

Codeigniter 3 分页错误:第二页显示记录 3 到 13 而不是 11 到 20

如何将多条记录中的有序数据返回到 MySQL 中的一条记录中?

如何使用连接表向数据库添加记录 - codeigniter