Jump to content

[SOLVED] add variable from loop to same variable in next loop


matthewst

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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