Jump to content

Iterate


rashmi_k28

Recommended Posts

hi,

 

the field names are Name, timestamp and status. This is the records from database and I have to calculate the differnce of timestamp when status is 0 and status is 1

 

 res.in | 2008-03-19 11:00:01 |      0 |

| res.in | 2008-03-19 11:10:01 |      0 |

| res.in | 2008-03-19 11:20:01 |      0 |

| res.in | 2008-03-19 11:30:01 |      0 |

| res.in | 2008-03-19 11:40:01 |      0 |

| res.in | 2008-03-19 11:50:01 |      0 |

| res.in | 2008-03-19 12:00:01 |      0 |

| res.in | 2008-03-19 12:10:01 |      0 |

| res.in | 2008-03-19 12:20:01 |      1 |

 

Here I want to calculate the timestamp between  2008-03-19 11:00:01 and 2008-03-19 12:10:01.

 

At present I have taken the query like display the status=0 and status=1> 'timestamp' when status is 0.

 

All the records when status=0 is displayed.

How to avoid it by iterating and check whether status is 0 of previous record or not

 

Link to comment
https://forums.phpfreaks.com/topic/96881-iterate/
Share on other sites

personally i would grab every result regardless of status, then loop through the rows and if the status in the loop eventually equals 1 then you can break; or do some coding etc.

 

eg:

 

$result = mysql_query(...);
while($row = mysql_fetch_array($result)){
   // if $first variable doesnt exist OR if the last status in the loop was 1 then set a new $first date. Otherwise jsut continue,
   $first = (!isset($first) || $last_stat == 1)? $result['date'] : $first;

   // if $last doesn't exist OR if the last_date in the loop was the same as the current $first date then set a new $last date, Otherwise just Continue,
   $last = (!isset($last) || $last_date == $first)? $result['date'] : $last;
   
   // These two used for the above statement variables
   $last_stat = $result['status'];
   $last_date = $result['date'];
   
   // checks to see if the current date range has ended.
   if($row['status'] == 1){
      $difference = "use some funcs here to calculate difference between $first and $last";
   }else{
      continue;
   }
}

 

you could also wrap this in a function

Link to comment
https://forums.phpfreaks.com/topic/96881-iterate/#findComment-495766
Share on other sites

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.