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

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.