Jump to content

SUM values inside foreach and while loop


newphpcoder

Recommended Posts

Hi..

 

I have table which has a data so_month:

 

FromMonth : 5

ToMonth : 7

 

and I have table working_days:

 

MonthName

May

Jun

Jul

MonthNumber

05

06

07

WorkingDays

23

24

23

 

Now I have function to get the 3 consecutive months from FromMonth to ToMonth , which as you can see from May to Jul

 

Now I have problem in getting the SUM of Working days.

 

here is my code:

 

<?php
  $sql = "SELECT FromMonth, ToMonth FROM so_month";
$res = mysql_query($sql,$con);

$row = mysql_fetch_assoc($res);
$FromMonth = $row['FromMonth'];
$ToMonth = $row['ToMonth'];

function monthNames($from, $to){
   $range=array();
   for($i=$from; $i<=$to; $i++){
           $range[$i]=date('M', mktime(0,0,0,$i));
   }
    return $range;
}
$month_ = implode("' ', ",monthNames($FromMonth,$ToMonth));

foreach( monthNames($FromMonth, $ToMonth) as $month){ 
$sql = "SELECT MonthName, SUM(WorkingDays) AS WorkingDays FROM working_days WHERE MonthName IN ('$month') GROUP BY MonthName";
$res = mysql_query($sql, $con);

while($row = mysql_fetch_array($res)){

$WorkingDays = $row['WorkingDays'];
}
echo $WorkingDays;
}
?>

 

the output of this code is:

 

232423

 

and when I change this line:

 

$WorkingDays = $row['WorkingDays'];

to

$WorkingDays += $row['WorkingDays'];

 

the output is:

 

234770

 

I correct output should be: 70

 

Any help is highly appreciated.

 

Thank you very much..

 

 

Link to comment
Share on other sites

under most circumstances, if you set $WorkingDays=0; before the loop, and then $WorkingDays+=$row['WorkingDays'] it should work. By setting it to zero first you're initializing it as a number, so there's something to add to (zero at the beginning, going up). If for some weird reason that's not working, you can specifically tell PHP that you're adding a number, not a string to the value by specifying it's an integer.

 

$WorkingDays=0;
while(...) {
   $WorkingDays+=(int)$row['WorkingDays'];
}

Link to comment
Share on other sites

Looking at it more closely, if the output of

$WorkingDays = $row['WorkingDays'];

is 232423 then that is the result you're getting from one row of mysql results, and has nothing to do with PHP. I don't know what so_month looks like, but if it's just three columns like that, why are you using SUM() and GROUP on it?

Link to comment
Share on other sites

Hi..

 

I resolved it now using thid code:

 

//-----Computation of Working Days---//
$sql = "SELECT FromMonth, ToMonth FROM so_month";
$res = mysql_query($sql,$con);

$row = mysql_fetch_assoc($res);
$FromMonth = $row['FromMonth'];
$ToMonth = $row['ToMonth'];

function monthNames($from, $to){
   $range=array();
   for($i=$from; $i<=$to; $i++){
           $range[$i]=date('M', mktime(0,0,0,$i));
   }
    return $range;
}
$month_ = implode("' ', ",monthNames($FromMonth,$ToMonth));

foreach( monthNames($FromMonth, $ToMonth) as $month){ 
$sql = "SELECT MonthName, SUM(WorkingDays) AS WorkingDays FROM working_days WHERE MonthName IN ('$month') GROUP BY MonthName";
$res = mysql_query($sql, $con);
$SumWorkingDays = 0; 
while($row = mysql_fetch_array($res)){

$WorkingDays+=(int)$row['WorkingDays'];
$SumWorkingDays += $WorkingDays;

}
}

 

Thank you

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.