PDA

View Full Version : Display sum of table


ikmyer
09-27-2005, 03:37 AM
Hi,

I just found this forum and it looks like I will be reading ALOT from this site. Tons of info.

I do have a question, or if someone is willing to help.
I am using DW MX 2004 with PHP Version 5.0.4 ( phpMyAdmin 2.6.2-pl1 ) MySQL 4.1.12

I have a database - donate
table - entry
with fields: name ( varchar 40 ), amount ( int ), comment ( txt )
I have a page set up to display the total number of records in the table,
what i want is to display the Sum of field amount

Current code:

<?php require_once('Connections/connect.php'); ?>
<?php
mysql_select_db($database_connect, $connect);
$query_donate = "SELECT * FROM entry";
$donate = mysql_query($query_donate, $connect) or die(mysql_error());
$row_donate = mysql_fetch_assoc($donate);
$totalRows_donate = mysql_num_rows($donate);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<p>Thank you for Entering your Information.</p>
<p></p>
<p>Our current total is: </p>
<p> </p>
<p>with <?php echo $totalRows_donate ?> submissions</p>
<p></p>
</body>
</html>
<?php
mysql_free_result($donate);
?>

How do I display the total sum of all values in Amount ( int )

Thanks

davidj
09-27-2005, 09:32 AM
you could use a recordset to do this


select sum(field) as name from table


field is the field you want to calculate

name is what you want to call the result ...

e.g.

<?php echo $row_query['name']; ?>

table is the table you want to use

ikmyer
09-27-2005, 12:41 PM
thank you, i have tried that and now i get:

Parse error: parse error in ..\htdocs\ikmyertest\donate_thank_you.php on line 8

Here is what i got.


select sum(amount) as amount_total from entry;

<?php echo $row_query['amount_total']; ?>

davidj
09-27-2005, 12:46 PM
try changing amount_total to amounttotal

ikmyer
09-27-2005, 12:54 PM
still same error

davidj
09-27-2005, 12:55 PM
please post entire recordset code

davidj
09-27-2005, 01:20 PM
when i used example

<?php echo $row_query['amount_total']; ?>

change $row_query to $row_recordsetname

ikmyer
09-27-2005, 01:55 PM
here is update. Still doesn't like line 8


<?php require_once('Connections/connection.php'); ?>
<?php
mysql_select_db($database_connection, $connection);
$query_donate = "SELECT * FROM entry";
$donate = mysql_query($query_donate, $connection) or die(mysql_error());
$row_donate = mysql_fetch_assoc($donate);
$totalRows_donate = mysql_num_rows($donate);
select sum(amount) as amounttotal from entry;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<p>Thank you!</p>
<p>Our current total is: <?php echo $row_donate['amounttotal']; ?></p>
<p>with <?php echo $totalRows_donate ?> submissions</p>
</body>
</html>
<?php
mysql_free_result($donate);
?>

davidj
09-27-2005, 02:44 PM
this is how it should be written

<?php require_once('Connections/connection.php'); ?>
<?php
mysql_select_db($database_connection, $connection);
$query_donate = "select sum(amount) as amounttotal from entry;";
$donate = mysql_query($query_donate, $connection) or die(mysql_error());
$row_donate = mysql_fetch_assoc($donate);
$totalRows_donate = mysql_num_rows($donate);

?>

ikmyer
09-27-2005, 04:12 PM
I pasted your code in and the total sum worked but then my total submission didn't work. So i created 2 version of each statement and it works now for both data values that I need. Thank you for taking the time to help me :D
let me know your paypal account if you want and I will buy you lunch!!

Thanks again, I will continue to read the board and contribute when i can
ikmyer

davidj
09-27-2005, 04:18 PM
I pasted your code in and the total sum worked but then my total submission didn't work. So i created 2 version of each statement and it works now for both data values that I need.

yes

forgot to mention that you would need 2 recordsets to perform both tasks


let me know your paypal account if you want and I will buy you lunch!!

get me this instead

BeoSound 9000 ('http://www.bang-olufsen.com/web2/systems/product.asp?section=systems&sub=ms&prodid=12')

ikmyer
10-04-2005, 05:22 PM
ha :D

I think that is a little out of my budget !!!

nice try though