Webmaster Forum


Go Back   Webmaster Forum > Web Development > Web Design Lobby > Coding Forum
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Coding Forum Problems with your code? Let's hear about it.

Ezilon Directory   I Sell Pagerank   V7N Directory

Reply
 
LinkBack Thread Tools Display Modes
Old 12-17-2006, 06:02 PM   #1 (permalink)
SN3
Contributing Member
 
SN3's Avatar
 
Join Date: 10-13-03
Location: Work, USA
Posts: 5,382
iTrader: 0 / 0%
Latest Blog:
None

SN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really nice
Send a message via AIM to SN3 Send a message via Yahoo to SN3
Totaling Numbers PHP

How can I total colums in PHP from a database? Just a little script I can add into a current PHP script.
SN3 is offline  
Add Post to del.icio.us
Reply With Quote
Sponsored Links
SEO Hosting by HostGator  Advertise Here  Buy Blog Links
Old 12-17-2006, 08:01 PM   #2 (permalink)
Inactive
 
littleFella's Avatar
 
Join Date: 06-20-04
Location: Ontario
Posts: 3,359
iTrader: 0 / 0%
Latest Blog:
None

littleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to behold
It might be a bit silly to do that in PHP, although there may be circumstances under which you'd opt for PHP rather than for SQL.
Why don't you try SQL first:

SELECT SUM(FieldName) FROM TableName
littleFella is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-17-2006, 11:08 PM   #3 (permalink)
Inactive
 
jeet's Avatar
 
Join Date: 03-04-06
Posts: 181
iTrader: 0 / 0%
jeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the rough
Adding columns? Do you mean rows?
I thought columns were made in the table structure...
Are you making columns on the fly with ALTER TABLE command?
Bye
jeet is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-20-2006, 04:27 PM   #4 (permalink)
Inactive
 
littleFella's Avatar
 
Join Date: 06-20-04
Location: Ontario
Posts: 3,359
iTrader: 0 / 0%
Latest Blog:
None

littleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to behold
Quote:
Originally Posted by jeet View Post
Adding columns? Do you mean rows?
Who's adding columns?
Quote:
Originally Posted by jeet View Post
I thought columns were made in the table structure...
You thought correctly, but you have no idea about basic SQL syntax.
littleFella is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-20-2006, 04:36 PM   #5 (permalink)
Inactive
 
jeet's Avatar
 
Join Date: 03-04-06
Posts: 181
iTrader: 0 / 0%
jeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the rough
Quote:
Originally Posted by littleFella View Post
Who's adding columns?

You thought correctly, but you have no idea about basic SQL syntax.
I'm confused... Didn't the thread starter say, "How can I total colums"?
And the thread title says "totaling numbers"?

Thanks for your nice words anyway...
Bye

Last edited by jeet : 12-20-2006 at 04:48 PM.
jeet is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-20-2006, 05:45 PM   #6 (permalink)
Contributing Member
 
Join Date: 07-08-06
Location: Here
Posts: 236
iTrader: 0 / 0%
Latest Blog:
None

daboss is a jewel in the roughdaboss is a jewel in the roughdaboss is a jewel in the roughdaboss is a jewel in the roughdaboss is a jewel in the roughdaboss is a jewel in the rough
Quote:
Originally Posted by jeet View Post
I'm confused... Didn't the thread starter say, "How can I total colums"?
And the thread title says "totaling numbers"?

Thanks for your nice words anyway...
Bye
he sure had nice words for you didn't he?

anyway, yup doing it using an sql statement is much easier. but if you insist of doing it using php, you'll have to:
1. query the table
2. store the results in a recordset
3. iterate every record in the recordset and sum up the values

try the codelet below (i have not tested it - coded it right out of my head):

Code:
$sql = "select column from table where [your criteria here]"; $rst = mysql_query($sql); while($row = mysql_fetch_array($rst)) : $total = $total + $row['column']; endwhile; print "Total = " . $total;
daboss is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-20-2006, 06:56 PM   #7 (permalink)
Inactive
 
jeet's Avatar
 
Join Date: 03-04-06
Posts: 181
iTrader: 0 / 0%
jeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the rough
Quote:
Originally Posted by daboss View Post
he sure had nice words for you didn't he?
anyway, yup doing it using an sql statement is much easier. but if you insist of doing it using php, you'll have to:
1. query the table
2. store the results in a recordset
3. iterate every record in the recordset and sum up the values
try the codelet below (i have not tested it - coded it right out of my head):
Code:
$sql = "select column from table where [your criteria here]"; $rst = mysql_query($sql); while($row = mysql_fetch_array($rst)) : $total = $total + $row['column']; endwhile; print "Total = " . $total;

Oh, I get it now...
He wanted to total the "values" stored in a "column".
Hope I am right this time...
Bye
jeet is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-21-2006, 06:55 PM   #8 (permalink)
SN3
Contributing Member
 
SN3's Avatar
 
Join Date: 10-13-03
Location: Work, USA
Posts: 5,382
iTrader: 0 / 0%
Latest Blog:
None

SN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really nice
Send a message via AIM to SN3 Send a message via Yahoo to SN3
Hmm...It does not seem to be working. The column name is "total" and the table is listings. Why isn't it working:
Quote:
<?
$sql = "listings";
$rst = mysql_query($sql);

while($row = mysql_fetch_array($rst)) : <----line 87
$total = $total + $row['total'];
endwhile;

print "Total = " . $total;
?>

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in [file location] on line 87
Total =
SN3 is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-21-2006, 07:44 PM   #9 (permalink)
Inactive
 
littleFella's Avatar
 
Join Date: 06-20-04
Location: Ontario
Posts: 3,359
iTrader: 0 / 0%
Latest Blog:
None

littleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to behold
out of curiosity; what's stopping you from doing this in just one SQL statement?
littleFella is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-21-2006, 08:06 PM   #10 (permalink)
SN3
Contributing Member
 
SN3's Avatar
 
Join Date: 10-13-03
Location: Work, USA
Posts: 5,382
iTrader: 0 / 0%
Latest Blog:
None

SN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really nice
Send a message via AIM to SN3 Send a message via Yahoo to SN3
What's the PHP code for that?
SN3 is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-21-2006, 08:32 PM   #11 (permalink)
Inactive
 
littleFella's Avatar
 
Join Date: 06-20-04
Location: Ontario
Posts: 3,359
iTrader: 0 / 0%
Latest Blog:
None

littleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to behold
PHP Code:
<?php

$query_sum 
"SELECT SUM(FiledName) FROM TableName ";
while(
$row mysql_fetch_array($query_sum)) 
print 
"Total = " $row;

?>
littleFella is offline  
Add Post to del.icio.us
Reply With Quote
Old 01-06-2007, 10:16 AM   #12 (permalink)
SN3
Contributing Member
 
SN3's Avatar
 
Join Date: 10-13-03
Location: Work, USA
Posts: 5,382
iTrader: 0 / 0%
Latest Blog:
None

SN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really nice
Send a message via AIM to SN3 Send a message via Yahoo to SN3
I'm getting:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in xxx line 86.

Line 86:
<?php

$query_sum = "SELECT SUM(total) FROM listings ";
while($row = mysql_fetch_array($query_sum)) <<<Line 86
print "Total = " . $row;

?>
SN3 is offline  
Add Post to del.icio.us
Reply With Quote
Old 01-06-2007, 10:56 AM   #13 (permalink)
SN3
Contributing Member
 
SN3's Avatar
 
Join Date: 10-13-03
Location: Work, USA
Posts: 5,382
iTrader: 0 / 0%
Latest Blog:
None

SN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really nice
Send a message via AIM to SN3 Send a message via Yahoo to SN3
Updated code; still same error:

PHP Code:
<?php
$sql_querysum 
'SELECT SUM(total) FROM listings';
while(
$row mysql_fetch_array($sql_querysum)) 
print 
$row

?>
SN3 is offline  
Add Post to del.icio.us
Reply With Quote
Old 01-07-2007, 04:49 PM   #14 (permalink)
SN3
Contributing Member
 
SN3's Avatar
 
Join Date: 10-13-03
Location: Work, USA
Posts: 5,382
iTrader: 0 / 0%
Latest Blog:
None

SN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really nice
Send a message via AIM to SN3 Send a message via Yahoo to SN3
Anyone?
SN3 is offline  
Add Post to del.icio.us
Reply With Quote
Old 01-08-2007, 10:36 AM   #15 (permalink)
v7n Mentor
 
Taltos's Avatar
 
Join Date: 11-22-06
Location: Phoenix, AZ
Posts: 1,804
iTrader: 0 / 0%
Latest Blog:
None

Taltos is a highly respected web proTaltos is a highly respected web proTaltos is a highly respected web proTaltos is a highly respected web proTaltos is a highly respected web proTaltos is a highly respected web proTaltos is a highly respected web proTaltos is a highly respected web proTaltos is a highly respected web proTaltos is a highly respected web proTaltos is a highly respected web pro
Send a message via Yahoo to Taltos
SN3, you do need to change your code just a bit. Try this:
PHP Code:
<?php
$sql_querysum 
'SELECT SUM(total) FROM listings';
$sum_results mysql_query($sql_querysum);
while(
$row mysql_fetch_array($sum_results)) 
print 
$row

?>
__________________
Experimenting
Taltos is offline  
Add Post to del.icio.us
Reply With Quote
Old 01-08-2007, 12:21 PM   #16 (permalink)
Inactive
 
jeet's Avatar
 
Join Date: 03-04-06
Posts: 181
iTrader: 0 / 0%
jeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the rough
If the above code does not work, try this one:

<?php
$sql= "SELECT total FROM listings";
$q = mysql_query($sql);
$l=0;
while($row = mysql_fetch_array($q)):
$l= $row['total'];
$l=$l+$l;
endwhile;
echo $l;
?>
Bye
jeet is offline  
Add Post to del.icio.us
Reply With Quote
Old 01-08-2007, 12:44 PM   #17 (permalink)
Contributing Member
 
Izzmo's Avatar
 
Join Date: 11-01-03
Location: Kansas City
Posts: 1,067
iTrader: 0 / 0%
Latest Blog:
Moved In!

Izzmo is a splendid one to beholdIzzmo is a splendid one to beholdIzzmo is a splendid one to beholdIzzmo is a splendid one to beholdIzzmo is a splendid one to beholdIzzmo is a splendid one to beholdIzzmo is a splendid one to beholdIzzmo is a splendid one to beholdIzzmo is a splendid one to beholdIzzmo is a splendid one to beholdIzzmo is a splendid one to behold
Send a message via ICQ to Izzmo Send a message via AIM to Izzmo Send a message via MSN to Izzmo Send a message via Yahoo to Izzmo
lol.. Sorry to be a party buster, but hasn't the question been answered like 3 or 4 times already?? lol
__________________
Izzmo
Coding Guru Extraordinaire
ZeroWeb Hosting & Design - Customizable hosting for every type of user!
Izzmo is offline  
Add Post to del.icio.us
Reply With Quote
Old 01-08-2007, 01:02 PM   #18 (permalink)
Inactive
 
jeet's Avatar
 
Join Date: 03-04-06
Posts: 181
iTrader: 0 / 0%
jeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the roughjeet is a jewel in the rough
None of the codes worked...
Bye
Quote:
Originally Posted by Izzmo View Post
lol.. Sorry to be a party buster, but hasn't the question been answered like 3 or 4 times already?? lol
jeet is offline  
Add Post to del.icio.us
Reply With Quote
Old 01-08-2007, 02:09 PM   #19 (permalink)
SN3
Contributing Member
 
SN3's Avatar
 
Join Date: 10-13-03
Location: Work, USA
Posts: 5,382
iTrader: 0 / 0%
Latest Blog:
None

SN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really niceSN3 is just really nice
Send a message via AIM to SN3 Send a message via Yahoo to SN3
Quote:
Originally Posted by jeet View Post
If the above code does not work, try this one:

<?php
$sql= "SELECT total FROM listings";
$q = mysql_query($sql);
$l=0;
while($row = mysql_fetch_array($q)):
$l= $row['total'];
$l=$l+$l;
endwhile;
echo $l;
?>
Bye
That worked. Thanks!
__________________
StealthNet3000 Administrator
StealthNet3000