[PHP-DB] pulling numbers from a column and getting a total
Hi, I was just wondering on how I could get a whole column full of integers and added them up and display that total? I currently use a for loop to display the whole column in a table, but I wanted to add all those numbers up. Thanks. -- Dave W
Re: [PHP-DB] pulling numbers from a column and getting a total
http://www.techonthenet.com/sql/sum.php That should help, Dave! Natalie On 7/26/06, Dave W [EMAIL PROTECTED] wrote: Hi, I was just wondering on how I could get a whole column full of integers and added them up and display that total? I currently use a for loop to display the whole column in a table, but I wanted to add all those numbers up. Thanks. -- Dave W -- Natalie Leotta Independent Consultant Arbonne International 607-785-2151 http://www.arbonne.com/
Re: [PHP-DB] pulling numbers from a column and getting a total
I just reread your question and realized that you said you're already displaying each number, so instead you might be better off with a variable that you initialize to 0 and then in each iteration of your for loop add on that value. Natalie On 7/26/06, Natalie Leotta [EMAIL PROTECTED] wrote: http://www.techonthenet.com/sql/sum.php That should help, Dave! Natalie On 7/26/06, Dave W [EMAIL PROTECTED] wrote: Hi, I was just wondering on how I could get a whole column full of integers and added them up and display that total? I currently use a for loop to display the whole column in a table, but I wanted to add all those numbers up. Thanks. -- Dave W -- Natalie Leotta Independent Consultant Arbonne International 607-785-2151 http://www.arbonne.com/ -- Natalie Leotta Independent Consultant Arbonne International 607-785-2151 http://www.arbonne.com/
Re: [PHP-DB] pulling numbers from a column and getting a total
Thanks Natalie and Miles! The loop thing worked. What I had tried before was put the echo inside the loop and it wasn't working, but now it does, w00t! Thanks! On 7/26/06, Miles Thompson [EMAIL PROTECTED] wrote: At 10:27 AM 7/26/2006, Dave W wrote: Hi, I was just wondering on how I could get a whole column full of integers and added them up and display that total? I currently use a for loop to display the whole column in a table, but I wanted to add all those numbers up. Thanks. -- Dave W Dave, You're v. close to an answer. Think about it a bit. Initialize a variable before your loop, setting it to zero. $tot = 0; In the loop, add the amount to that variable, $tot += $num_to_add; OR $tot = $tot + $num_to_add; After the loop, echo $tot. HTH - Miles -- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.1.394 / Virus Database: 268.10.4/399 - Release Date: 7/25/2006 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Dave W
[PHP-DB] Column names into variables
Hi, I have a table with about 50 columns in it. I'd like to query the information for one record, and hold the data in variables that have the same name as the column headings, along these lines: $query = Select * from `postings` where `id` = $userid; $result = mysql_query($query); while($row=mysql_fetch_array($result)) { $id = $row[id]; $location = $row[location]; $employer = $row[employer]; ... } I'd like to do this for all columns in the table, and feel like there's a way to do this automatically, but haven't been able to find the function or syntax to use. Any help is greatly appreciated. Thanks, Zeth
Re: [PHP-DB] Column names into variables
I believe this does exactly what you want. http://us3.php.net/extract Ben Hatfield Programmer [EMAIL PROTECTED] Soapbox Studio, Inc. 469 N. Lake Street Mundelein, IL 60060 P: +1.847.566.0666 x6# www.soapboxstudio.com On Jul 26, 2006, at 12:14 pm, z wrote: Hi, I have a table with about 50 columns in it. I'd like to query the information for one record, and hold the data in variables that have the same name as the column headings, along these lines: $query = Select * from `postings` where `id` = $userid; $result = mysql_query($query); while($row=mysql_fetch_array($result)) { $id = $row[id]; $location = $row[location]; $employer = $row[employer]; ... } I'd like to do this for all columns in the table, and feel like there's a way to do this automatically, but haven't been able to find the function or syntax to use. Any help is greatly appreciated. Thanks, Zeth -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php