CodeIgniter Forums
How can i use variable from diffrent foreach to get value by adding 2 variable itself - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: How can i use variable from diffrent foreach to get value by adding 2 variable itself (/showthread.php?tid=63125)



How can i use variable from diffrent foreach to get value by adding 2 variable itself - freddy - 09-28-2015

Hello guys, now i have little problem that spend my time not to hard but don't know how to solved this, here is my view
Code:
<?php
foreach ($sum->result() as $row) {  ?>

$untung=$row->jumlah_item;    

<?php  }  ?>



<?php
foreach ($sumongkir->result() as $row) {  ?>

$ongkir=($row->total);

<?php  }  ?>


$total=$untung+$ongkir; //it doesn't working, what do i wrong here? please guide me to debug it, thanks



Declare variables outside of loop - JayAdra - 09-28-2015

Declare your variables outside of the foreach loop.

PHP Code:
$untung 0;
$ongkir 0;

foreach (
$sum->result() as $row) {
  
$untung=$row->jumlah_item;    
}

foreach (
$sumongkir->result() as $row) {
  
$ongkir=($row->total);
}

$total=$untung+$ongkir



RE: How can i use variable from diffrent foreach to get value ... - pdthinh - 09-28-2015

(09-28-2015, 04:44 PM)freddy Wrote: Hello guys, now i have little problem that spend my time not to hard but don't know how to solved this, here is my view






Code:
<?php
foreach ($sum->result() as $row) {  ?>

$untung=$row->jumlah_item;

<?php  }  ?>



<?php
foreach ($sumongkir->result() as $row) {  ?>

$ongkir=($row->total);

<?php  }  ?>


$total=$untung+$ongkir; //it doesn't working, what do i wrong here? please guide me to debug it, thanks

Assume two query result in the same size

PHP Code:
// assume $sum->num_rows() == $sumongkir->num_rows()
for ($i 0$i $sum->num_rows(); ++$i) {
    $untung=$sum->row($i)->jumlah_item;
    $ongkir=$sumongkir->row($i)->total;
    $total+=$untung+$ongkir;    


Not sure I understand your question correctly but I think $total may be an array $total[$i]


RE: How can i use variable from diffrent foreach to get value by adding 2 variable itself - freddy - 09-28-2015

(09-28-2015, 05:18 PM)JayAdra Wrote: Declare your variables outside of the foreach loop.


PHP Code:
$untung 0;
$ongkir 0;

foreach (
$sum->result() as $row) {
 
 $untung=$row->jumlah_item   
}

foreach (
$sumongkir->result() as $row) {
 
 $ongkir=($row->total);
}

$total=$untung+$ongkir
Smile Thanks jay, i guess also but need someone guide me to debug it ! big thanks and +1 for you, May God bless you


RE: How can i use variable from diffrent foreach to get value by adding 2 variable itself - freddy - 09-28-2015

(09-28-2015, 05:45 PM)pdthinh Wrote:
(09-28-2015, 04:44 PM)freddy Wrote: Hello guys, now i have little problem that spend my time not to hard but don't know how to solved this, here is my view







Code:
<?php
foreach ($sum->result() as $row) {  ?>

$untung=$row->jumlah_item;

<?php  }  ?>



<?php
foreach ($sumongkir->result() as $row) {  ?>

$ongkir=($row->total);

<?php  }  ?>


$total=$untung+$ongkir; //it doesn't working, what do i wrong here? please guide me to debug it, thanks

Assume two query result in the same size


PHP Code:
// assume $sum->num_rows() == $sumongkir->num_rows()
for ($i 0$i $sum->num_rows(); ++$i) {
    $untung=$sum->row($i)->jumlah_item;
    $ongkir=$sumongkir->row($i)->total;
    $total+=$untung+$ongkir;    


Not sure I understand your question correctly but I think $total may be an array $total[$i]

Thanks also pdt it give same result too, May God Bless you, +1 for you Big Grin 


RE: How can i use variable from diffrent foreach to get value by adding 2 variable itself - pdthinh - 09-28-2015

(09-28-2015, 06:27 PM)freddy Wrote: Thanks also pdt it give same result too, May God Bless you, +1 for you Big Grin 

Maybe I misunderstand something. If you use foreach your way, you only get the last row of the result set. So it something similar to following without the foreach:

PHP Code:
$untung $sum->last_row()->jumlah_item;
$ongkir $sumongkir->last_row()->total;
$total $untung $ongkir