...

View Full Version : inserting a total of a column in a table



mikeberish
02-27-2010, 10:21 PM
I have a database which feeds into a table on my webpage. I am trying to add a total for each column which has a number vavlue. I have been struggling wtih this for hours and just can't figure it out. The code I am struggling with is at line 52-54.

Obviously the name fields won't be totaled, but all the rest should be.

Thanks ahead of time.

Mike






<?php require_once('../Connections/ReunionPreRegDB.php'); ?>
<?php
mysql_select_db($database_ReunionPreRegDB, $ReunionPreRegDB);
$query_PreRegRosterrecordset = "SELECT GradYear, LName, FName, FriSocial, SatRHSTour, SatPicnic, SatDance, SunGolf, SunBrunch, SunOtherEvent, HotelRooms FROM PreRegDB ORDER BY GradYear ASC";
$PreRegRosterrecordset = mysql_query($query_PreRegRosterrecordset, $ReunionPreRegDB) or die(mysql_error());
$row_PreRegRosterrecordset = mysql_fetch_assoc($PreRegRosterrecordset);
$totalRows_PreRegRosterrecordset = mysql_num_rows($PreRegRosterrecordset);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>



<table border="1" cellpadding="1" cellspacing="1">
<tr>
<td><div align="center">Graduation<br />
Year</div></td>
<td><div align="center">Last<br />
Name</div></td>
<td><div align="center">First<br />
Name</div></td>
<td><div align="center">Friday<br />
Evening<br />
Social</div></td>
<td><div align="center">Saturday<br />
RHS<br />
Tour</div></td>
<td><div align="center">Saturday<br />
Picnic</div></td>
<td><div align="center">Saturday<br />
Dinner/<br />
Dance</div></td>
<td><div align="center">Sunday<br />
Golf</div></td>
<td><div align="center">Sunday<br />
Brunch</div></td>
<td><div align="center">Sunday<br />
Other<br />
Event</div></td>
<td><div align="center">Hotel<br />
Rooms</div></td>
</tr>
<tr>
<td colspan="3"><div align="right">Event Totals</div></td>
<td>

<?php
$total += $FriSocial;
?>




</td>



<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_PreRegRosterrecordset['GradYear']; ?></td>
<td><?php echo $row_PreRegRosterrecordset['LName']; ?></td>
<td><?php echo $row_PreRegRosterrecordset['FName']; ?></td>
<td><div align="center"><?php echo $row_PreRegRosterrecordset['FriSocial']; ?></div></td>
<td><div align="center"><?php echo $row_PreRegRosterrecordset['SatRHSTour']; ?></div></td>
<td><div align="center"><?php echo $row_PreRegRosterrecordset['SatPicnic']; ?></div></td>
<td><div align="center"><?php echo $row_PreRegRosterrecordset['SatDance']; ?></div></td>
<td><div align="center"><?php echo $row_PreRegRosterrecordset['SunGolf']; ?></div></td>
<td><div align="center"><?php echo $row_PreRegRosterrecordset['SunBrunch']; ?></div></td>
<td><div align="center"><?php echo $row_PreRegRosterrecordset['SunOtherEvent']; ?></div></td>
<td><div align="center"><?php echo $row_PreRegRosterrecordset['HotelRooms']; ?></div></td>
</tr>
<?php } while ($row_PreRegRosterrecordset = mysql_fetch_assoc($PreRegRosterrecordset)); ?>
</table>
</body>
</html>
<?php
mysql_free_result($PreRegRosterrecordset);
?>

F00Baron
02-27-2010, 11:46 PM
Try another query at the end with the SUM() function:

$totals_sql = "SELECT SUM(HotelRooms) AS total_HotelRooms,SUM(SunGolf) AS total_SunGolf FROM PreRegDB ORDER BY GradYear ASC";
$totals_result = mysql_query($totals_sql, $ReunionPreRegDB) or die(mysql_error());
$totals_row = mysql_fetch_assoc($totals_result);
echo 'total_HotelRooms: ' . $totals_row['total_HotelRooms'] . "<br>\n";
echo 'total_SunGolf: ' . $totals_row['total_SunGolf'] . "<br>\n";

mikeberish
02-28-2010, 03:45 AM
YOU ROCK!!:thumbsup:

With a few modifications I made it fit exactly what I needed. I very much appreciate your help. Wish I knew more about PHP and HTML.

Do you have a recommendation for a way to learn this stuff?

Thanks again,

:thumbsup::thumbsup::thumbsup::thumbsup::thumbsup::)

Mike

Shauny_B
02-28-2010, 11:37 AM
Hello Mike,
The best way to learn PHP in my opinion is by doing what you're doing now. Asking for help when you're stuck on forums, and of course by actualling coding with trial and error.

Practice makes perfect,
Shaun



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum