matthewst Posted May 22, 2007 Share Posted May 22, 2007 In the following code I have a loop that gives me a $total_time for a set series of actions. example: action_start = 10:00 action_end = 10:05 $total_time = 5 min then it loops and goes on to the next set of actions action_start = 12:00 action_end = 12:15 $total_time = 15 min I need to keep a running tab of $complete_total_time example: $complete_total_time = $total_time(loop 1) + $total_time(loop 2) + $total_time(loop 3) and so on and so on $query4="SELECT * FROM job_log WHERE $table_id=table_id"; $last_value = ""; $result4=mysql_query($query4); while($row4 = mysql_fetch_assoc($result4)) { if ($action6=="Start Creation" || $action6=="Start Table PTG" || $action6=="Cutting - Begin" || $action6=="Begin 1st Proof" || $action6=="Proof Print - Begin" || $action6=="Start In-House Fix" || $action6=="Final Print - Begin" || $action6=="Start Outside Change"){ $start_action = $time_table4; $start_count++;} if ($action6=="Pause"){ $pause_time = $time_table4; $pause_count++; $start_action1 = $pause_time - $start_action;} if ($action6=="Resume"){ $resume_time = $time_table4; $resume_count++;} if ($action6=="End Creation" || $action6=="Finish Table PTG" || $action6=="Cutting - End" || $action6=="End 1st Proof -OK" || $action6=="End 1st Proof - FIX" || $action6=="End In-House Fix" || $action6=="Final Print - End" || $action6=="End Outside Change"){ $end_action = $time_table4; $end_count++; if ($end_count > $start_count){ $start_action = "0"; $start_count--;} if ($resume_count < $pause_count){ $sec = ($end_action - $start_action) + $start_action1; $hr = floor($sec/60/60); $sec = $sec - $hr * 60 * 60; $min = floor($sec/60); $sec = $sec - $min * 60; $total_time = sprintf("%02d:%02d:%02d", $hr, $min, $sec); $start_count = "0"; $end_count = "0"; $pause_count = "0"; $resume_count = "0"; $pause_time = "0"; $resume_time = "0"; if ($total_time > "05:00:00" || $total_time <= "00:00:00"){echo "";} else {echo "<center><table border = '0' cellspaceing = '0' cellpadding = '-1' width = '10%' bgcolor = '#999999'>"; echo "<tr><td align = 'center' width = '100%'>$total_time</td>"; echo "</tr></table></center>";}} $sec = ($end_action - $resume_time) + ($pause_time - $start_action); $hr = floor($sec/60/60); $sec = $sec - $hr * 60 * 60; $min = floor($sec/60); $sec = $sec - $min * 60; $total_time = sprintf("%02d:%02d:%02d", $hr, $min, $sec); $start_count = "0"; $end_count = "0"; $pause_count = "0"; $resume_count = "0"; $pause_time = "0"; $resume_time = "0"; if ($total_time > "05:00:00" || $total_time <= "00:00:00"){echo "";} else {echo "<center><table border = '0' cellspaceing = '0' cellpadding = '-1' width = '10%' bgcolor = '#999999'>"; echo "<tr><td align = 'center' width = '100%'>$total_time</td>"; echo "</tr></table></center>";}} else{ echo "";} Link to comment https://forums.phpfreaks.com/topic/52513-solved-add-variable-from-loop-to-same-variable-in-next-loop/ Share on other sites More sharing options...
Wildbug Posted May 22, 2007 Share Posted May 22, 2007 So, initialize a variable before you start the loop and continue to add the time to it at the end of the loop. (Also, this is shorter than your if statements:) if (preg_match('/Start|Begin/',$action6)) { // .... if (preg_match('/End|Finish/',$action6)) { // .... Link to comment https://forums.phpfreaks.com/topic/52513-solved-add-variable-from-loop-to-same-variable-in-next-loop/#findComment-259234 Share on other sites More sharing options...
matthewst Posted May 22, 2007 Author Share Posted May 22, 2007 thanks for the preg_match, i've already put it in the code however, do to my unleetness, I have no idea how to initialize a variable then add to it on successive loops Link to comment https://forums.phpfreaks.com/topic/52513-solved-add-variable-from-loop-to-same-variable-in-next-loop/#findComment-259237 Share on other sites More sharing options...
Wildbug Posted May 22, 2007 Share Posted May 22, 2007 Oh. (Excuse the pseudo-code; I don't know exactly how you'll set it up.) $complete_total_time = 0; while ( /* adding individual times */) { $start = 0; $end = /* something */; $total = $end - $start; $complete_total_time += $total; echo "Time for this segment was: $total"; } echo "Total time, overall, was: $complete_total_time"; So, you've made the $complete_total_time equal to 0 before you enter the loop, then keep adding to it during each loop. It will contain the sum total by the loop's exit. Link to comment https://forums.phpfreaks.com/topic/52513-solved-add-variable-from-loop-to-same-variable-in-next-loop/#findComment-259269 Share on other sites More sharing options...
matthewst Posted May 22, 2007 Author Share Posted May 22, 2007 now it displays the number of $times's instead of adding them Link to comment https://forums.phpfreaks.com/topic/52513-solved-add-variable-from-loop-to-same-variable-in-next-loop/#findComment-259326 Share on other sites More sharing options...
Wildbug Posted May 23, 2007 Share Posted May 23, 2007 Hmm. You've been converting the integer number of seconds to an HH:MM:SS string, right? If you just add that via +=, the string might be evaluating to 1 and just adding 1. PHP doesn't "know" how to add your strings. I might suggest writing a function that adds HH:MM:SS-string time and using that instead of += addition. Link to comment https://forums.phpfreaks.com/topic/52513-solved-add-variable-from-loop-to-same-variable-in-next-loop/#findComment-259750 Share on other sites More sharing options...
matthewst Posted May 23, 2007 Author Share Posted May 23, 2007 Got it! thanks man $complete_total_time = 0 //then the loop //in the loop I have this $total_seconds = $sec; $complete_total_time += $total_seconds; echo "$complete_total_time"; //at this point I can actually see the seconds and they ARE being add to one another //end loop $sec = $complete_total_time; $hr = floor($sec/60/60); $sec = $sec - $hr * 60 * 60; $min = floor($sec/60); $sec = $sec - $min * 60; $complete_total_time = sprintf("%02d:%02d:%02d", $hr, $min, $sec); echo "$complete_total_time"; Link to comment https://forums.phpfreaks.com/topic/52513-solved-add-variable-from-loop-to-same-variable-in-next-loop/#findComment-259818 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.