我是一个案例,我创建了一个社交网络应用程序但是有一些用户可以访问大量数据。例如:Rudi可以访问数据" 1"," 2",& " 3&#34 ;; andi可以访问数据" 2" &安培; " 3&#34 ;; Didi只访问数据" 3"。所以我让tb_akses
来区分访问权限。但是当我想要对数组求和时,我不知道如何。请帮帮我。
这是我在这个问题中的表格
tb_akses
CREATE TABLE IF NOT EXISTS `tb_akses` (
`id_perespon` smallint(4) NOT NULL,
`id_skpd_aduan` smallint(10) NOT NULL,
PRIMARY KEY (`id_perespon`,`id_skpd_aduan`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `tb_akses` (`id_perespon`, `id_skpd_aduan`) VALUES
(2, 2),
(2, 3);
tb_pengaduan
CREATE TABLE IF NOT EXISTS `tb_pengaduan` (
`no_hp_pengadu` smallint(12) NOT NULL,
`id_skpd_aduan` varchar(100) NOT NULL DEFAULT '',
`tgl_aduan` date NOT NULL,
`nama_pengadu` varchar(100) NOT NULL,
`judul_pengaduan` varchar(20) NOT NULL,
`isi_aduan` varchar(200) NOT NULL,
PRIMARY KEY (`no_hp_pengadu`,`id_skpd_aduan`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `tb_pengaduan` (`no_hp_pengadu`, `id_skpd_aduan`, `tgl_aduan`, `nama_pengadu`, `judul_pengaduan`, `isi_aduan`) VALUES
(0, '2', '0000-00-00', 'sss', 'newss', 'hai'),
(32767, '2', '0000-00-00', 'karlota', 'lamu ngnoi', 'stop lala mulu\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n'),
(32, '3', '2015-11-20', 'ss', 'lamu banget', 'sss'),
(323, '3', '0000-00-00', 'ss', 'lamu banget', 'sss'),
(32767, '70', '0000-00-00', 'santoooo', 'somo pulang', 'lama lech echhhhhhhh\r\n'),
(20, '2', '0000-00-00', '..', 'lanjut', '..'),
(1111, '79', '0000-00-00', 'aaaa', 'aaaaa', 'aaaaa');
这是我的模特:
function select_akses_perespon(){
$id_perespon=$this->session->userdata('id_perespon');
$this->db->select('*');
$this->db->from('tb_akses');
$this->db->where('id_perespon',$id_perespon);
return $this->db->get();
}
这是我的控制者:
$data['daftar_akses'] = $this->pengaduan_model->select_akses_perespon()->result();
这是我的观点
// I want to sum $jumlah in this Array
<h4>Anda Memiliki<?php foreach ($daftar_akses as $akses){
$this->db->select('*');
$this->db->from('tb_pengaduan');
$this->db->where('id_skpd_aduan',$akses->id_skpd_aduan);
$query = $this->db->get();
$jumlah = $query->num_rows();
echo $jumlah;}?>Pemberitahuan</h4>
我的结果到现在为止:
"Anda Memiliki 32 Pemberitahuan"
但我想总结"3"+"2" become "5"
。
请帮帮我......
答案 0 :(得分:0)
您只需要$this->db->select_sum()
:
参考:
https://ellislab.com/codeigniter/user-guide/database/active_record.html
在您看来:
<h4>Anda Memiliki<?php foreach ($daftar_akses as $akses)
{
$this->db->from('tb_pengaduan');
$this->db->select_sum('id_skpd_aduan');
$this->db->where('id_skpd_aduan',$akses->id_skpd_aduan);
$query = $this->db->get();
$obj = $query->row();
$jumlah = $obj->id_skpd_aduan;
echo $jumlah;
}
?>
Pemberitahuan</h4>
答案 1 :(得分:0)
<h4>Anda Memiliki <?php
$id_perespon=$this->session->userdata('id_perespon');
$this->db->select('*');
$this->db->from('tb_pengaduan');
$this->db->where('id_perespon',$id_perespon );
$this->db->join('tb_akses', 'tb_akses.id_skpd_aduan = tb_pengaduan.id_skpd_aduan', 'right');
$query = $this->db->get();
$jumlah = $query->num_rows();
echo $jumlah;
?>
Pemberitahuan
</h4>