Give an example db layout and/or array layout that you plan on using.

Would I be correct in assuming that the array would look something like
this?

$values['val_1'] = 100.00;
$values['val_2'] = 150.00;
$values['val_3'] = 200.00;
$values['val_4'] = 250.00;
$values['val_5'] = 100.00;

if this is correct you could do something like this.

$t=0;
foreach($values AS $amnt)
  $t += $amnt;

echo $t;  // this should return 800

if you wanted the formatted return to be something like xxx.xx you'll want
to use something like number_format()

if for some reason you are getting it from mysql then it could look like the
following.

$sql = "SELECT title, val FROM tbl";
$results = mysql_query($sql);

$t=0;
while($row = mysql_fetch_assoc($results)) {
  $t += $row['val'];
}
echo $t;

Maybe you have a different style sql statement.

$sql = "SELECT SUM(val) FROM tbl";
$results = mysql_query($sql);
list($t) = mysql_fetch_row($results)) {
echo $t;

Notice the difference in mysql_fetch_* functions

if you want the second sql statement used but want to refer to it by a name.
do this

$sql = "SELECT SUM(val) AS total FROM tbl";
$results = mysql_query($sql);
$arr = mysql_fetch_assoc($results);
echo $arr['total'];


If i confused you with all the different ways to do this, don't worry, I
confused myself also.

If need be, I will explain more.

Jim Lucas

----- Original Message -----
From: "Ben C." <[EMAIL PROTECTED]>
To: "Jay Blanchard" <[EMAIL PROTECTED]>;
<[EMAIL PROTECTED]>
Sent: Monday, August 04, 2003 2:04 PM
Subject: RE: Re: [PHP] Addin a column of numbers not using MySQL


> Let me try and explain a little better and easier.  What I am trying to do
is take a list of values and add them to get.  I am getting the values from
a database query.
>
> Assuming that I had the following:
> val_1     100.00
> val_2     150.00
> val_3     200.00
> val_4     250.00
> val_5     100.00
>
> What I want to do now is add up the values 1-5 and get 800.00.
>
> Does this help clarify?
>
>
>
>
> >
> > From: "Jay Blanchard" <[EMAIL PROTECTED]>
> > Date: 2003/08/04 Mon PM 04:57:17 EDT
> > To: "Ben C." <[EMAIL PROTECTED]>,  <[EMAIL PROTECTED]>
> > Subject: RE: Re: [PHP] Addin a column of numbers not using MySQL
> >
> > [snip]
> > Unsure.  How would I do that?
> > [/snip]
> >
> > Ben I think that we have a communications failure. Let's start
> > over...you wish to total a database column using php, correct?
> > Everything that you have been shown so far will do that. Is it possible
> > that there are no query results? If not, then we are doing the query
> > wrong. Try it with single quotes;
> >
> > $sql_2 = "SELECT paidamount FROM $tb_name WHERE id = '" . $id . "' ";
> >
> > Please do a verbose check on the query to see if it is working properly
> >
> > if(!($result_2 = mysql_query($sql_2, $connection))){
> >    print("MySQL reports: " . mysql_error() . "\n");
> >    exit();
> > }
> >
> > Let's use objects instead of arrays (humor me for a moment)
> >
> > while($row = mysql_fetch_object($result_2)){
> > $paid = $row->paidamount;
> > echo "$paid \n";
> > $total = $total + paid;
> > }
> >
> > echo "$total \n";
> >
> > Please copy and paste the results into your reply
> >
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to