Hello,

I am trying to add a total of some items in a while.

My while produces list with names and with times that that person has worked.
Like this:

John 8:00
John 5:00
John 6:00
<<here the total of john>>
Lars 9:00
Lars 4:00
<<here the total of lars>>
Yvonn 7:00
Yvonn 9:00
Yvonn 3:00
Yvonn 5:00
<<here the total of Yvonn>>

How to perform the totals in an while? I get a real big pain in my head from it :-)

Anyone, thanks in advance?

Convert the time into minutes(or seconds), add them in the loop, display it converting it back to the way you want !

I think I was not clear enough.
I shall post my code, to show the problem. My endgoal is written in an example list in my first post.

<?php
// While loop to fetch the result
while ($aG = mysql_fetch_assoc($qG)){
	// Much data, times, names
	// First row is for name John for example
	// Second too
	// Thirt too
	// Fourt is for the name Yvonn, thus first _here_  the totals 
	// for name John, then posting the result for Yvonn
	// And so on
}
?>

Note: I want to show the times worked _as listed_ and then the totals.

My code is in Dutch, but I shall post it.

//////////////////////////////
////// RAPPORT OUTPUT ///////
//////////////////////////////
	// Query maken
	$qG = mysql_query($sG);
	// While loop om gegevens weer te geven
	while ($aG = mysql_fetch_assoc($qG)){
		// Kleur van de rij definieren
		$trKleur = ($a++ % 2) ? "oneven" : "";
		
		echo '
		<tr class="'.$trKleur.'">';
			if ($vestiging){ echo '<td>'.$aG['locatie'].'</td>';}
			if ($naam){ echo '<td>'.$aG['voornaam'].' '.$aG['achternaam'].'</td>';}
			if ($datum){ echo '<td>'.$aG['datum'].'</td>';}
			if ($begin){ echo '<td>'.$aG['begin'].'</td>';}
			if ($eind){ echo '<td>'.$aG['eind'].'</td>';}
			if ($totaal){echo '<td>'.$aG['totaal'].'</td>';}
			if ($totaal_ziek_weergeven) { 
				echo '<td>';
				// Totaal minuten van de contracttijd berekenen
				list($uur_contr, $minuut_contr) = explode(':', $aG['contracttijd']);
				$minuut_contr += $uur_contr * 60;
				$minuut_contr = $minuut_contr/5;
				// Totaal minuten van de nog gewerkte tijd berekenen
				list($uur_totaal, $minuut_totaal) = explode(':', $aG['totaal']);
				$minuut_totaal += $uur_totaal * 60;
				// Totalen optellen, en omzetten naar tijd
				$totaal_ziek = $minuut_contr - $minuut_totaal;
				echo date("H:i",mktime(0,$totaal_ziek,0,0,0,0));
				echo '</td>';
			}
			if ($opmerkingen){ echo '<td>'.$aG['opmerkingen'].'</td>';}
			if ($referentie){ echo '<td>'.$aG['refentie'].'</td>';}
			
		echo '
		</tr>';
		
			// Totalen weergeven 
			if ($naam != ($aG['voornaam'].' '.$aG['achternaam'])){
				echo '
				<tr>
					<td />
					<td style="font-weight:bold; align:right;">Totaal</td>
					<td></td>
					<td></td>
				</tr>
				<tr>
					<td>&nbsp</td>
				</tr>';
			}
			
			// $naam weergeven indien men totalen wil zien, met $naam wordt 
			// gecontroleerd wanneer het einde van de reeks uren van die naam bereikt is
			$naam = $aG['voornaam'].' '.$aG['achternaam'];
			
	}
} else {
	echo 'Geen resultaten om weer te geven';
	
}

Note: This code does not work correct, the last name does not have the total of his worked time, as expected.

How can you differentiate users ? I think you need 2 separate queries. One query to get all the user ids and in the while loop, have another query to calculate the time.

$query = "select distinct(userid) from table"; //query to each user's id
$result = mysql_query($query);

while($row  = mysql_fetch_array($result)) {
  $id = $row['userid'];
$total = 0;
 $query2 = "select * from table where userid = '$id'"; //query to get the relevant records of this user
$result2 = mysql_query($query2);
while($row2 = mysql_fetch_array($result2)) {
//display the records as you are doing now
//also calculate the total_time in this loop
$total = $total + $time; 
} //by the end of this loop, you will have $total (total time) for that particular user
echo "total : ".$total;
} //process next user

You can do it this way :)

Comments
Again he did the job, in the topic: "Show a total in a while". Thanks!

I shall implement the code, and tell you when it works, maybe I do that tomorrow.

Nav33n, I want to thank you for it. It took a longer time than I expected, before I realy implement it.
But anyway thanks.

Topic solved.

This question has already been answered. Start a new discussion instead.