我们能算出来自foreach的具体价值吗? 例如:
--total--
2
3
4
--total 2--
5
6
7
总计应为:9
总计2 承担:18
这是我的观点:
<table class="table table-bordered">
<thead>
<tr>
<th rowspan="2">Kode</th>
<th rowspan="2">Uraian</th>
<th colspan="3">Rincian Perhitungan</th>
<th rowspan="2">Jumlah</th>
<th rowspan="2"></th>
</tr>
<tr>
<th>Volume</th>
<th>Satuan</th>
<th>Harga Satuan</th>
</tr>
</thead>
<tbody>
<?php
if($belanja)
foreach (@$belanja as $kode_rek1 => $rek2):?>
<?php $kode = explode('|', $kode_rek1) ?>
<tr>
<td><strong><?php echo $kode[0]; ?> </strong></td>
<td><strong><?php echo $kode[1]; ?> </strong></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<?php foreach ($rek2 as $kode_rek2 => $rek3):?>
<?php $kode = explode('|', $kode_rek2) ?>
<tr>
<td><strong><?php echo $kode[0]; ?> </strong></td>
<td><strong><?php echo $kode[1]; ?> </strong></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<?php foreach ($rek3 as $kode_rek3 => $rek4):?>
<?php $kode = explode('|', $kode_rek3) ?>
<tr>
<td><strong><?php echo $kode[0]; ?> </strong></td>
<td><strong><?php echo $kode[1]; ?> </strong></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<?php foreach ($rek4 as $kode_rek4 => $rek5):?>
<?php $kode = explode('|', $kode_rek4) ?>
<tr>
<td><?php echo $kode[0]; ?></td>
<td><?php echo $kode[1]; ?></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<?php foreach ($rek5 as $kode_rek5 => $belanja):?>
<?php $kode = explode('|', $kode_rek5) ?>
<tr>
<td><?php echo $kode[0]; ?> </strong></td>
<td><?php echo $kode[1]; ?></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<?php foreach ($belanja as $group => $item):?>
<?php $kode = explode('|', $group) ;
if($kode[1]):
?>
<tr>
<td></td>
<td><?php echo $kode[1]; ?></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td><?php echo anchor('master/kegiatan/delgroup/'.$kode[0], 'DEL', 'class="del"'); ?></td>
</tr>
<?php
endif;
//var_dump($item);
foreach ($item as $key => $val):
if(!is_null($val->id)):?>
<tr>
<td></td>
<td><?php echo $val->keterangan;?></td>
<td><?php echo $val->jumlah_satuan;?></td>
<td><?php echo $val->satuan;?></td>
<td><?php echo number_format($val->nilai_satuan,2,',','.');?></td>
<td><?php echo number_format($val->nilai_satuan*$val->jumlah_satuan,2,',','.');?></td>
<td><?php echo anchor('master/kegiatan/delrinc/'.$val->id, 'DEL', 'class="del"'); ?></td>
</tr>
<?php
endif;
endforeach; ?>
<tr>
<form class="detil_belanja">
<td><?php echo form_hidden('belanja_id', $kode[0]); ?></td>
<td><input name="keterangan" placeholder="Keterangan Belanja"></td>
<td><input name="volume" placeholder="Jumlah/Volume"></td>
<td><input name="satuan" placeholder="Satuan"></td>
<td><input name="harga_satuan" placeholder="Harga Satuan"></td>
<td><input type="submit" name="" value="ADD" class="btn_submit"></td>
</form>
</tr>
<?php endforeach; ?>
<?php endforeach; ?>
<?php endforeach; ?>
<?php endforeach; ?>
<?php endforeach; ?>
<?php endforeach; ?>
</tbody>
这是我的控制器
function detail_belanja($id){
$this->load->model('kegiatans');
$belanja = $this->kegiatans->get_belanja($id);
foreach ($belanja as $key => $v) {
$hasil[$v->kd_rek_1_gab][$v->kd_rek_2_gab][$v->kd_rek_3_gab][$v->kd_rek_4_gab][$v->kd_rek_5_gab][$v->kelompok_id.'|'.$v->kelompok][] = $v;
}
$data['belanja'] = $hasil;
echo '<pre>';
var_dump($data['belanja']);
echo '</pre>';
$this->load->view("bidang/belanja_list",$data);
}
这是我的模特:
function get_belanja($id){
$sql="
select
concat( a.kd_rek_1,'.|',f.nm_rek_1) as kd_rek_1_gab,
concat( a.kd_rek_1, '.' , a.kd_rek_2,'.|', e.nm_rek_2) as kd_rek_2_gab,
concat( a.kd_rek_1, '.' , a.kd_rek_2, '.' , a.kd_rek_3,'.|', d.nm_rek_3) as kd_rek_3_gab,
concat( a.kd_rek_1, '.' , a.kd_rek_2, '.' , a.kd_rek_3, '.' , right(concat('0', a.kd_rek_4), 2),'.|',c.nm_rek_4) as kd_rek_4_gab,
concat( a.kd_rek_1, '.' , a.kd_rek_2, '.' , a.kd_rek_3, '.' , right(concat('0', a.kd_rek_4), 2) , '.' , right(concat('0', a.kd_rek_5), 2),'.|',b.nm_rek_5) as kd_rek_5_gab,
concat( a.kd_rek_1, '.' , a.kd_rek_2, '.' , a.kd_rek_3, '.' , right(concat('0', a.kd_rek_4), 2) , '.' , right(concat('0', a.kd_rek_5), 2)) as kode_rekening,
a.id kelompok_id,a.keterangan kelompok,
belanja_rinc.*
from belanja a left join
belanja_rinc on belanja_rinc.belanja_id = a.id inner join
ref_rek_5 b on a.kd_rek_1 = b.kd_rek_1 and a.kd_rek_2 = b.kd_rek_2 and a.kd_rek_3 = b.kd_rek_3 and a.kd_rek_4 = b.kd_rek_4 and a.kd_rek_5 = b.kd_rek_5 inner join
ref_rek_4 c on b.kd_rek_1 = c.kd_rek_1 and b.kd_rek_2 = c.kd_rek_2 and b.kd_rek_3 = c.kd_rek_3 and b.kd_rek_4 = c.kd_rek_4 inner join
ref_rek_3 d on c.kd_rek_1 = d.kd_rek_1 and c.kd_rek_2 = d.kd_rek_2 and c.kd_rek_3 = d.kd_rek_3 inner join
ref_rek_2 e on d.kd_rek_1 = e.kd_rek_1 and d.kd_rek_2 = e.kd_rek_2 inner join
ref_rek_1 f在e.kd_rek_1 = f.kd_rek_1,其中a.kegiatan_id =?&#34 ;;
$q = $this->db->query($sql,array($id));
return $q->result();
}
答案 0 :(得分:0)
在你的情况下,如果你想使用foreach循环,那么:
$sum = 0;
foreach($group as $key => $value) {
$sum += $value;
}
echo $sum;
但是如果你想使用数组的直接总和而不是下面的解决方案:
$total = array_sum($group);