techker Posted August 30, 2008 Share Posted August 30, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/122042-solved-wrong-sum/ Share on other sites More sharing options...
techker Posted August 30, 2008 Author Share Posted August 30, 2008 i tried this $query2 = "SELECT COUNT( `to_t1` )-( `from_t1`) AS Totalhours FROM `week1`; "; but it gives me -7h Quote Link to comment https://forums.phpfreaks.com/topic/122042-solved-wrong-sum/#findComment-630002 Share on other sites More sharing options...
fenway Posted August 31, 2008 Share Posted August 31, 2008 I'm confused by the math... what are you trying to count? And for SUM, why isn't the difference inside the parens? Quote Link to comment https://forums.phpfreaks.com/topic/122042-solved-wrong-sum/#findComment-630489 Share on other sites More sharing options...
techker Posted August 31, 2008 Author Share Posted August 31, 2008 see i want to calculat how many hours i have done in a day.and a week. they bust my balls over over time..lol i dont understand the sum part? Quote Link to comment https://forums.phpfreaks.com/topic/122042-solved-wrong-sum/#findComment-630495 Share on other sites More sharing options...
toplay Posted August 31, 2008 Share Posted August 31, 2008 Maybe you want something like this: $query2 = "SELECT COUNT(*) AS TotalCount, `to_t1` - `from_t1` AS TotalNumber, SUM( `to_t1` - `from_t1`) AS Totalhours FROM `week1`"; Quote Link to comment https://forums.phpfreaks.com/topic/122042-solved-wrong-sum/#findComment-630545 Share on other sites More sharing options...
techker Posted August 31, 2008 Author Share Posted August 31, 2008 that should be it but it gives me this? Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause Quote Link to comment https://forums.phpfreaks.com/topic/122042-solved-wrong-sum/#findComment-630570 Share on other sites More sharing options...
toplay Posted August 31, 2008 Share Posted August 31, 2008 So, add a group by at the end then. group by some_column_name # Use something relevant to query Quote Link to comment https://forums.phpfreaks.com/topic/122042-solved-wrong-sum/#findComment-630576 Share on other sites More sharing options...
techker Posted September 1, 2008 Author Share Posted September 1, 2008 that did it.did it ask for a group since we added to fileds in one? now i just need to add all the filds up.thx Quote Link to comment https://forums.phpfreaks.com/topic/122042-solved-wrong-sum/#findComment-631049 Share on other sites More sharing options...
techker Posted September 1, 2008 Author Share Posted September 1, 2008 hey is there a way that if the total hours pass 40 it shows in red? i added up all the sums and it works a1! now in the query can i add something like an if Totalhours is over '40' show in red Quote Link to comment https://forums.phpfreaks.com/topic/122042-solved-wrong-sum/#findComment-631057 Share on other sites More sharing options...
toplay Posted September 1, 2008 Share Posted September 1, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/122042-solved-wrong-sum/#findComment-631194 Share on other sites More sharing options...
techker Posted September 1, 2008 Author Share Posted September 1, 2008 so if $color = ($Totalhours > 40) ? 'red' : 'blue'; echo 'Total hours: <font color="', $color, '">', $Totalhours, '</font>'; Quote Link to comment https://forums.phpfreaks.com/topic/122042-solved-wrong-sum/#findComment-631200 Share on other sites More sharing options...
toplay Posted September 1, 2008 Share Posted September 1, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/122042-solved-wrong-sum/#findComment-631241 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.