SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Apr 2007
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding variable from a loop

    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


    Code:
    $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 "";}

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Create a var before your loop , call it say,

    $sum_total_time= 0;

    Then in your loop whenever you add a row add to that $sum_total_time;

    At the end of the loop echo $sum_total_time.

    Is that what you mean? Or is your question about adding time values together?

  3. #3
    SitePoint Addict
    Join Date
    Apr 2007
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know how to do what it is your telling me to do.


    What I really need is:

    ___________________
    query = blah blah

    while $result=mysql
    get time for one condition
    get time for another condition
    $time = condition one plus condition two
    echo $time
    ___________________

    Then it loops again and displays $time

    But I need to add all the $time's after the loop is completly finished

    ____________________
    $sum_total_time = $time from loop1 + $time from loop2 + $time from loop3 and so on and so on

  4. #4
    SitePoint Addict
    Join Date
    Apr 2007
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've added this:
    Code:
    $complete_total_time = 0;
    
    begin loop
    do stuff
    $complete_total_time += $total_time;
    end loop
    
    echo "$complete_total_time";
    but that just displays the number of times the script had to loop and not the cumulative time

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    How about you echo out $sec, can you get it so that $sec echoes onto the screen (anywhere will do for now)?

    Perhaps that's the place you should add it to $complete_total_time, and work from there.

  6. #6
    SitePoint Addict
    Join Date
    Apr 2007
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    O.K. a step in the right direction!

    here's what I've got:
    Code:
    $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 = $total_seconds;
    $hr = floor($sec/60/60);
    $sec = $sec - $hr * 60 * 60; 
    $min = floor($sec/60); 
    $sec = $sec - $min * 60;
    $complete_total_time = sprintf("&#37;02d:%02d:%02d", $hr, $min, $sec);
    echo "$complete_total_time";
    the echo after the loop (last echo in the code) only displays the amount of time for the last loop

    example:
    in my page the number of seconds is 137447 but the last echo only displays 58 min or 3480 seconds

  7. #7
    SitePoint Addict
    Join Date
    Apr 2007
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nevermind i'm an idiot

    i changed:
    $sec = $total_seconds;
    to
    $sec = $complete_total_time;


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •