Moron Posted August 16, 2006 Share Posted August 16, 2006 while ($RESULT = mssql_fetch_assoc($RESULTDS)) {echo "<tr align=center>";echo "<td>";//echo $combineddate;echo $RESULT['Lmo']; echo "/";echo $RESULT['Lda'];echo "/";echo $RESULT['LYR']; echo "</td>";echo "<td>";echo $RESULT['Leave Code'];echo "</td>";echo "<td>";echo $RESULT['Hours']; echo "</td>";echo "</tr>";}//printf ("%01.2f", $TotalHours);print("</table>");?>This produces the total hours of leave taken over a period of time. What would be the right way to add these hours? When I try the usual SUM statement, it gives me the figure for one record only. I've also tried to include the SUM statement within the WHILE statement and that pretty much hoses the entire thing.Anybody? Quote Link to comment Share on other sites More sharing options...
trq Posted August 16, 2006 Share Posted August 16, 2006 Can we see your sql statement Moron? Quote Link to comment Share on other sites More sharing options...
Moron Posted August 16, 2006 Author Share Posted August 16, 2006 [quote author=thorpe link=topic=104506.msg416848#msg416848 date=1155741464]Can we see your sql statement Moron?[/quote]Here you go:[quote]$RESULTDS=mssql_query("SELECT DISTINCT LH.[Employee Number], LH.[Lmo], LH.[Lda], LH.[LYR], LH.[Hours], LH.[Leave Code], M2.[HRYRAT], M2.[EMPNO], M2.[MANLAP], M2.[MANLAC], M2.[MANLTC], M2.[MSKLAB], M2.[MSKLTC], M2.[MSKLAB], M2.[MSKLTC], M2.[NAMEMI], M2.[NAMEL], M2.[NAMEF] FROM LEAVHST LH INNER JOIN MASTERL2 M2 ON LH.[Employee Number]=M2.EMPNO WHERE M2.[EMPNO] = '".$_POST['employeenumber']."' ORDER BY LH.[LYR] desc, LH.[Lmo] desc, LH.[Lda] desc"); $RESULT=mssql_fetch_assoc($RESULTDS);[/quote] Quote Link to comment Share on other sites More sharing options...
trq Posted August 16, 2006 Share Posted August 16, 2006 Explain exactly what it is you want to get the sum of. Your not really helping much here. A simple example would be...[code]SELECT SUM(hours) AS totalhours FROM tbl[/code] Quote Link to comment Share on other sites More sharing options...
Moron Posted August 16, 2006 Author Share Posted August 16, 2006 [quote author=thorpe link=topic=104506.msg416857#msg416857 date=1155742401]Explain exactly what it is you want to get the sum of. Your not really helping much here. A simple example would be...[code]SELECT SUM(hours) AS totalhours FROM tbl[/code][/quote]I see. This part of the code...[quote]echo "<td>";echo $RESULT['Hours']; echo "</td>";[/quote].... pulls the hours of leave taken for each record. What I want to do is add the hours from each record displayed.I've tried doing it like in your example above but I can't seem to properly integrate that statement into my query.If there is a way to do this outside of the query? If so, it would be my preferred method. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 16, 2006 Share Posted August 16, 2006 [code]<?php$total_hours = 0;//assign 0 to it otherwise php5 will give a warningwhile ($RESULT = mssql_fetch_assoc($RESULTDS)) {echo "<tr align=center>";echo "<td>";//echo $combineddate;echo $RESULT['Lmo']; echo "/";echo $RESULT['Lda'];echo "/";echo $RESULT['LYR']; echo "</td>";echo "<td>";echo $RESULT['Leave Code'];echo "</td>";echo "<td>";$total_hours = $total_hours + $RESULT['Hours'];echo $RESULT['Hours']; echo "</td>";echo "</tr>";}//printf ("%01.2f", $TotalHours);print("</table>");echo "Total hours: $total_hours";?>[/code] Quote Link to comment Share on other sites More sharing options...
Moron Posted August 16, 2006 Author Share Posted August 16, 2006 [quote author=GingerRobot link=topic=104506.msg416875#msg416875 date=1155743789][code]<?php$total_hours = 0;//assign 0 to it otherwise php5 will give a warningwhile ($RESULT = mssql_fetch_assoc($RESULTDS)) {echo "<tr align=center>";echo "<td>";//echo $combineddate;echo $RESULT['Lmo']; echo "/";echo $RESULT['Lda'];echo "/";echo $RESULT['LYR']; echo "</td>";echo "<td>";echo $RESULT['Leave Code'];echo "</td>";echo "<td>";$total_hours = $total_hours + $RESULT['Hours'];echo $RESULT['Hours']; echo "</td>";echo "</tr>";}//printf ("%01.2f", $TotalHours);print("</table>");echo "Total hours: $total_hours";?>[/code][/quote]Thank you, thank you, thank you!Ginger, you ROCK! :) Quote Link to comment 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.