Jump to content

add in a variable inside while


Drewser33

Recommended Posts

$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

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'];
}

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.