Jump to content

Sum of Variable inside a while?


Drewser33

Recommended Posts

// 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

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.