Jump to content

Archived

This topic is now archived and is closed to further replies.

Moron

How do I add the sum of this result?

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?

Share this post


Link to post
Share on other sites
Can we see your sql statement Moron?

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
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.

Share this post


Link to post
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]

Share this post


Link to post
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!

:)

Share this post


Link to post
Share on other sites

×

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.