php - Showing Sum of amount based on another row value on SESSION -
i have view cart session this
id name disc qty -- ---- ------ ------ 1 aaaa d1 2 1 aaaa d5 1 2 bbbb d1 1 1 aaaa d1 1
what want is, showing query session result this
name total ---- ------ aaaa 4 bbbb 1
how can this?
i have query show cart:
<?php if(count($_session[data1][id])>0) for($i=0;$i<count($_session[data1][id]);$i++) { if($_session[data1][id][$i]!='') { ?> <td ><?=$_session[data1][id][$i]?></td> <td ><?=$_session[data1][name][$i]?></td> <td ><?=$_session[data1][disc][$i]?></td> <td ><?=$_session[data1][qty][$i]?></td> <?php } } ?>
you can loop through data adding quantities array indexed name.
quick'n'dirty example
<?php $names = array("aaa", "bbb", "aaa", "bbb"); $qts = array(1, 2, 3, 4); ($i=0; $i<count($names); $i++) { $res[$names[$i]] += $qts[$i]; } $k = array_keys($res); ($i=0; $i<count($k); $i++) { echo $k[$i] . ":" . $res[$k[$i]] . "<br/>"; } ?>
aaa:4
bbb:6
Comments
Post a Comment