Drewser33 Posted March 7, 2008 Share Posted March 7, 2008 All, I am returning a variable inside a while so this variable and be different everytime and could appear any number of times including zero. How can I add all the values that are produced for this variable? Thanks, Drew Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted March 7, 2008 Share Posted March 7, 2008 it would be better to use an array instead of a single variable, so for example: $x = 0; $var = array(); while(something){ $var[$x] = number; $x++; } $sum = implode(+, $var); echo $sum; Quote Link to comment Share on other sites More sharing options...
Drewser33 Posted March 7, 2008 Author Share Posted March 7, 2008 // Set initial values $lastcode = ""; $diff = 0; $ptime=0; $otime=0; $sql = "SELECT EventMessage AS action, EventCode AS code, TimeOccured AS strtime FROM EventLogger WHERE Eventcode IN ('OID','POP') ORDER BY TimeOccured ASC"; $res = mysql_query($sql) or die(mysql_error()); while($r = mysql_fetch_assoc($res)){ if($r['code'] != $lastcode){ if($r['code'] == "OID"){ $otime = $r['strtime']; $ptime = $ptime; } else { $otime = $otime; $ptime = $r['strtime']; } $timeocc = strtotime($r['strtime']); $dateocc = date('m-d-Y H:i:s' , $timeocc); echo "<p>Message = ".$r['action']." at ".$dateocc."</p>\n"; if($r['code'] == "POP"){ $difftime = $ptime - $otime; $diff = strtotime($ptime) - strtotime($otime); echo "<p>$diff</p>\n"; $diff = datediff($otime,$ptime); echo "<p>$diff</p>\n"; $ptime = 0; $otime = 0; } $ptime = $r['strtime']; $otime = $r['strtime']; } $lastcode = $r['code']; } This is the code i am currently using. I have the $diff as just a number, which now is a variable but I don't know how I can turn this into an array like you say? So from this code can you tell me more specifically how to turn that variable into an array? Thanks, Drew Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted March 7, 2008 Share Posted March 7, 2008 is $diff the variable you are trying to add? Quote Link to comment Share on other sites More sharing options...
Drewser33 Posted March 7, 2008 Author Share Posted March 7, 2008 yep Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 7, 2008 Share Posted March 7, 2008 You have two instances of $diff, do the following output the samething? $diff = strtotime($ptime) - strtotime($otime); echo "<p>$diff</p>\n"; $diff = datediff($otime,$ptime); echo "<p>$diff</p>\n"; If they do then, change the above lines to: $diff[] = strtotime($ptime) - strtotime($otime); Then after the while loop add the following: echo 'total difference = ' . array_sum($diff); Quote Link to comment Share on other sites More sharing options...
Drewser33 Posted March 7, 2008 Author Share Posted March 7, 2008 Awesome, thank you. Works great!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.