Jump to content

How do I add the sum of this result?


Moron

Recommended Posts

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?

Link to comment
Share on other sites

[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]

Link to comment
Share on other sites

[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.

Link to comment
Share on other sites

[code]
<?php
$total_hours = 0;//assign 0 to it otherwise php5 will give a warning
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>";
$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]
Link to comment
Share on other sites

[quote author=GingerRobot link=topic=104506.msg416875#msg416875 date=1155743789]
[code]
<?php
$total_hours = 0;//assign 0 to it otherwise php5 will give a warning
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>";
$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!

:)
Link to comment
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.