Drewser33 Posted March 7, 2008 Share Posted March 7, 2008 $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"){ $diff = strtotime($ptime) - strtotime($otime); $diff = datediff($otime,$ptime); echo "<p>$diff</p>\n"; $ptime = 0; $otime = 0; } $ptime = $r['strtime']; $otime = $r['strtime']; } $lastcode = $r['code']; } Can I add the $diff, the amount of them depends on the results of the query so I would like to add all the variables that do show up. Thanks, Drew Link to comment https://forums.phpfreaks.com/topic/94927-add-in-a-variable-inside-while/ Share on other sites More sharing options...
corbin Posted March 8, 2008 Share Posted March 8, 2008 Errrr you just want to get the total from the $diffs? How about: $lastcode = ""; $diff = 0; $total_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"){ $diff = strtotime($ptime) - strtotime($otime); $diff = datediff($otime,$ptime); $total_diff += $diff; echo "<p>$diff</p>\n"; $ptime = 0; $otime = 0; } $ptime = $r['strtime']; $otime = $r['strtime']; } $lastcode = $r['code']; } Link to comment https://forums.phpfreaks.com/topic/94927-add-in-a-variable-inside-while/#findComment-487243 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.