Webmaster Forum

Go Back   Webmaster Forum > Web Development > Coding Forum

Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more.


Reply
 
Thread Tools Display Modes
Share |
  #1  
Old 12-17-2006, 06:02 PM
SN3's Avatar
SN3 SN3 is offline
Senior Member
 
Join Date: 10-13-03
Location: Work, USA
Posts: 2,795
iTrader: 0 / 0%
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.
 
Reply With Quote

Advertisement

Advertisement

  #2  
Old 12-17-2006, 08:01 PM
littleFella's Avatar
littleFella littleFella is offline
Senior Member
 
Join Date: 06-20-04
Location: Ontario
Posts: 1,756
iTrader: 0 / 0%
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
 
Reply With Quote
  #3  
Old 12-17-2006, 11:08 PM
jeet's Avatar
jeet jeet is offline
Contributing Member
 
Join Date: 03-04-06
Posts: 203
iTrader: 0 / 0%
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
 
Reply With Quote
  #4  
Old 12-20-2006, 04:27 PM
littleFella's Avatar
littleFella littleFella is offline
Senior Member
 
Join Date: 06-20-04
Location: Ontario
Posts: 1,756
iTrader: 0 / 0%
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.
 
Reply With Quote
  #5  
Old 12-20-2006, 04:36 PM
jeet's Avatar
jeet jeet is offline
Contributing Member
 
Join Date: 03-04-06
Posts: 203
iTrader: 0 / 0%
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.
 
Reply With Quote
  #6  
Old 12-20-2006, 05:45 PM
daboss daboss is offline
Contributing Member
 
Join Date: 07-08-06
Location: Here
Posts: 188
iTrader: 0 / 0%
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;
 
Reply With Quote
  #7  
Old 12-20-2006, 06:56 PM
jeet's Avatar
jeet jeet is offline
Contributing Member
 
Join Date: 03-04-06
Posts: 203
iTrader: 0 / 0%
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
 
Reply With Quote
  #8  
Old 12-21-2006, 06:55 PM
SN3's Avatar
SN3 SN3 is offline
Senior Member
 
Join Date: 10-13-03
Location: Work, USA
Posts: 2,795
iTrader: 0 / 0%
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 =
 
Reply With Quote
  #9  
Old 12-21-2006, 07:44 PM
littleFella's Avatar
littleFella littleFella is offline
Senior Member
 
Join Date: 06-20-04
Location: Ontario
Posts: 1,756
iTrader: 0 / 0%
out of curiosity; what's stopping you from doing this in just one SQL statement?
 
Reply With Quote
  #10  
Old 12-21-2006, 08:06 PM
SN3's Avatar
SN3 SN3 is offline
Senior Member
 
Join Date: 10-13-03
Location: Work, USA
Posts: 2,795
iTrader: 0 / 0%
What's the PHP code for that?
 
Reply With Quote
  #11  
Old 12-21-2006, 08:32 PM
littleFella's Avatar
littleFella littleFella is offline
Senior Member
 
Join Date: 06-20-04
Location: Ontario
Posts: 1,756
iTrader: 0 / 0%
PHP Code:
<?php

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

?>
 
Reply With Quote
  #12  
Old 01-06-2007, 10:16 AM
SN3's Avatar
SN3 SN3 is offline
Senior Member
 
Join Date: 10-13-03
Location: Work, USA
Posts: 2,795
iTrader: 0 / 0%
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;

?>
 
Reply With Quote
  #13  
Old 01-06-2007, 10:56 AM
SN3's Avatar
SN3 SN3 is offline
Senior Member
 
Join Date: 10-13-03
Location: Work, USA
Posts: 2,795
iTrader: 0 / 0%
Updated code; still same error:

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

?>
 
Reply With Quote
  #14  
Old 01-07-2007, 04:49 PM
SN3's Avatar
SN3 SN3 is offline
Senior Member
 
Join Date: 10-13-03
Location: Work, USA
Posts: 2,795
iTrader: 0 / 0%
Anyone?
 
Reply With Quote
  #15  
Old 01-08-2007, 10:36 AM
Taltos's Avatar
Taltos Taltos is offline
v7n Mentor
 
Join Date: 11-22-06
Location: Phoenix, AZ
Posts: 1,797
iTrader: 0 / 0%
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

?>
 
Reply With Quote
  #16  
Old 01-08-2007, 12:21 PM
jeet's Avatar
jeet jeet is offline
Contributing Member
 
Join Date: 03-04-06
Posts: 203
iTrader: 0 / 0%
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
 
Reply With Quote
  #17  
Old 01-08-2007, 12:44 PM
Izzmo's Avatar
Izzmo Izzmo is offline
v7n Mentor
 
Join Date: 11-01-03
Location: Kansas
Posts: 1,356
iTrader: 1 / 100%
lol.. Sorry to be a party buster, but hasn't the question been answered like 3 or 4 times already?? lol
 
Reply With Quote
  #18  
Old 01-08-2007, 01:02 PM
jeet's Avatar
jeet jeet is offline
Contributing Member
 
Join Date: 03-04-06
Posts: 203
iTrader: 0 / 0%
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
 
Reply With Quote
  #19  
Old 01-08-2007, 02:09 PM
SN3's Avatar
SN3 SN3 is offline
Senior Member
 
Join Date: 10-13-03
Location: Work, USA
Posts: 2,795
iTrader: 0 / 0%
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!
 
Reply With Quote
  #20  
Old 01-08-2007, 05:54 PM
SN3's Avatar
SN3 SN3 is offline
Senior Member
 
Join Date: 10-13-03
Location: Work, USA
Posts: 2,795
iTrader: 0 / 0%
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
I take that back - it isn't adding everything. I have no idea where it is getting these numbers from. Hmm...
 
Reply With Quote
Go Back   Webmaster Forum > Web Development > Coding Forum

Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Numbers in domain name SkGold SEO Forum 10 03-29-2007 09:46 PM
Weird Google Numbers bytech SEO Forum 4 04-06-2006 02:13 PM


V7N Network
Get exposure! V7N I Love Photography V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 09:34 PM.
Powered by vBulletin
Copyright 2000-2014 Jelsoft Enterprises Limited.
Copyright © 2003 - 2018 VIX-WomensForum LLC