Jump to content

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

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.