Jump to content

[SOLVED] wrong sum?


techker

Recommended Posts

hey guys so i got this going but there sum is wrong??

 

$query2 = "SELECT COUNT( `to_t1` )-( `from_t1`)AS TotalNumber, SUM( `to_t1` )-( `from_t1`) AS Totalhours FROM `week1`; "; 
$result2 = mysql_query($query2) or die(mysql_error());

while($row = mysql_fetch_array($result2)) {

  echo $row['Totalhours'] ." H";
}

21H

 

to_t1 is 17:30 and from_t1 is 9:30

 

how does it get 21?i need 8h

 

Link to comment
https://forums.phpfreaks.com/topic/122042-solved-wrong-sum/
Share on other sites

Retrieve the data, then use PHP to do it:

 

$color = ($Totalhours > 40) ? 'red' : 'blue';  // $Totalhours var is value retrieved from table result

 

echo 'Total hours: <font color="', $color, '">', $Totalhours, '</font>';  // Better to use CSS - this is just an example

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/122042-solved-wrong-sum/#findComment-631194
Share on other sites

Wrong, no 'if' at the beginning. Use what I had.

 

This:

 

$color = ($Totalhours > 40) ? 'red' : 'blue';

 

is the same as this:

 

if ($Totalhours > 40)

    $color = 'red';

else

    $color = 'blue';

 

 

See ternary operator:

http://us3.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary

 

 

Link to comment
https://forums.phpfreaks.com/topic/122042-solved-wrong-sum/#findComment-631241
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.