Re: [PHP] Sum of results
On Wed, January 30, 2008 12:58 am, Dax Solomon Umaming wrote: Hi; I've tried Googling this out but failed to search for an answer to this, so I'm posting to this list. I'd like to know if there's a way to get the sum this results: // Results Individual Daily Consumption //AccountNo : Consumption 4146121002: 1.42 4146111002: 0.29 4146113002: 1.38 4146110002: 0.33 4146112002: 0.00 4146118002: 9.96 == MORE == // Code that generated the results while ($row6 = mysql_fetch_assoc($queryconsumerresults)) { // Show Consumer AccountNo and their Consumption echo $row6['AccountNo'] . : . sprintf(%1.2f, $row6['Reading'] + $row6['KwHrAdjustment']) - ($row6['Reading1'] + $row6['KwHrAdjustment1'])) / $noofdays) * $row6['Multiplier'])) . br /; } I've tried getting the sum() from the MySQL table, but Multiplier is either at 1 or 2 or 2.5 and the only way I can get an accurate total consumption is getting the sum from the results. You should be able to use: sum( (Reading + KwHrAdjustment - Reading1 + KwHrAdjustment1) / $noofdays * Multiplier) from MySQL in another query... But if you are already iterating through the results anyway, it's probably just as easy to do: $sum = 0; while ($row6 = mysql_fetch_assoc(...)){ $consumption = $row6['Reading'] + ...; $sum += $consumption; } echo Total: $sumhr /\n; Is there a way I can place this code on an array? I've tried using $indcons = array( sprintf(%1.2f, $row6['Reading'] + $row6 ['KwHrAdjustment']) - ($row6['Reading1'] + $row6['KwHrAdjustment1'])) / $noofdays) * $row6['Multiplier']))) but I've obviously failed. -- Dax Solomon Umaming http://knightlust.com/ GPG: 0x715C3547 -- Some people have a gift link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Sum of results
Hi; I've tried Googling this out but failed to search for an answer to this, so I'm posting to this list. I'd like to know if there's a way to get the sum this results: // Results Individual Daily Consumption //AccountNo : Consumption 4146121002: 1.42 4146111002: 0.29 4146113002: 1.38 4146110002: 0.33 4146112002: 0.00 4146118002: 9.96 == MORE == // Code that generated the results while ($row6 = mysql_fetch_assoc($queryconsumerresults)) { // Show Consumer AccountNo and their Consumption echo $row6['AccountNo'] . : . sprintf(%1.2f, $row6['Reading'] + $row6['KwHrAdjustment']) - ($row6['Reading1'] + $row6['KwHrAdjustment1'])) / $noofdays) * $row6['Multiplier'])) . br /; } I've tried getting the sum() from the MySQL table, but Multiplier is either at 1 or 2 or 2.5 and the only way I can get an accurate total consumption is getting the sum from the results. Is there a way I can place this code on an array? I've tried using $indcons = array( sprintf(%1.2f, $row6['Reading'] + $row6 ['KwHrAdjustment']) - ($row6['Reading1'] + $row6['KwHrAdjustment1'])) / $noofdays) * $row6['Multiplier']))) but I've obviously failed. -- Dax Solomon Umaming http://knightlust.com/ GPG: 0x715C3547 signature.asc Description: This is a digitally signed message part.
Re: [PHP] Sum of results
On Jan 30, 2008 1:58 AM, Dax Solomon Umaming [EMAIL PROTECTED] wrote: I've tried Googling this out but failed to search for an answer to this, so I'm posting to this list. I'd like to know if there's a way to get the sum this results: you can simply sum them as you print the report $sum = 0.0; while ($row6 = mysql_fetch_assoc($queryconsumerresults)) { $curVal = ((($row6['Reading'] + $row6['KwHrAdjustment']) - ($row6['Reading1'] + $row6['KwHrAdjustment1'])) / $noofdays) * $row6['Multiplier'])); // Show Consumer AccountNo and their Consumption echo $row6['AccountNo'] . : . sprintf(%1.2f, ( $curVal . br /; // add curVal to sum $sum += $curVal; } Is there a way I can place this code on an array? I've tried using $indcons = array( sprintf(%1.2f, $row6['Reading'] + $row6 ['KwHrAdjustment']) - ($row6['Reading1'] + $row6['KwHrAdjustment1'])) / $noofdays) * $row6['Multiplier']))) but I've obviously failed. array() creates an array, what youve done on this line is created an array with one element, which will be the last value of $row6 from the while loop that precedes it. since the last value returned from mysql_fetch_assoc() is false (thats what terminates the while loop), im guessing $indcons contains false as its only value. if you want to place these values in an array as you iterate over them, add this inside the while loop: $indcons[] = $curVal; -nathan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sum a column of values from a MySQL query
Sorry, Jay, but that's a horrible method. You could just run one query with a GROUP BY clause to get the sum for each invoiceID, instead of running multiple queries like you've mentioned... $query = SELECT invoiceid, SUM(partpaidamount) AS partpaid FROM $tb_name GROUP BY invoiceid; $rs = mysql_query($query) or die(mysql_error()); while($r = mysql_fetch_assoc($rs)) { echo Invoice: {$r['invoiceid']}, SUM: {$r['partpaid']}; } ---John Holmes... PS: Sorry for the top post, but I'm at work and outlook express sucks. :) - Original Message - From: Jay Blanchard [EMAIL PROTECTED] To: Ben C. [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Tuesday, August 05, 2003 8:29 AM Subject: RE: [PHP] Sum a column of values from a MySQL query [snip] 1) The code returns the sum of a partially paid invoice. The individual invoice is 'partpaid'. WORKS...NO PROBLEM 2) The while() then return a list of partially paid invoices which is $invoicepartpaid. WORKS..NO PROBLEM 3) I then want to add the list of partially paid invoices ($invoicepartpaid) together. I AM STUCK HERE ADDING THE INVOICES AMOUNTS TOGETHER. [/snip] Ben...do you want to add together invoices where each ID is different? Is that right? If so I have the answer! You need to loop through the ID's which you are not doingI have taken your code and added to it(and made changes for clarity in the e-amil) //get invoice id's $sql_1 = SELECT DISTINCT(invoiceid) as inid FROM $tb_name ; $result_1 = mysql_query($sql_1, $connection) or die(mysql_error()); while($idinvoice = mysql_fetch_array($result_1)){ // get partial paid for THIS invoice $sql_2 = SELECT SUM(partpaidamount) as partpaid ; $sql_2 .= FROM $tb_name ; $sql_2 .= WHERE invoiceid = ' . $idinvoice['inid'] . ' ; $result_2 = @mysql_query($sql_2,$connection) or die(mysql_error()); //you will only be returning ONE row, so no need for while loop $row = mysql_fetch_array($result_2); $invoicepartpaid = $row['partpaid']; $total = $total + $invoicepartpaid; } // loop to NEXT invoice print(number_format($total, 2, '', ','). \n); Does this make sense? You must loop through each ID and query for that ID's partial amount. -- 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
RE: [PHP] Sum a column of values from a MySQL query
[snip] 1) The code returns the sum of a partially paid invoice. The individual invoice is 'partpaid'. WORKS...NO PROBLEM 2) The while() then return a list of partially paid invoices which is $invoicepartpaid. WORKS..NO PROBLEM 3) I then want to add the list of partially paid invoices ($invoicepartpaid) together. I AM STUCK HERE ADDING THE INVOICES AMOUNTS TOGETHER. [/snip] Ben...do you want to add together invoices where each ID is different? Is that right? If so I have the answer! You need to loop through the ID's which you are not doingI have taken your code and added to it(and made changes for clarity in the e-amil) //get invoice id's $sql_1 = SELECT DISTINCT(invoiceid) as inid FROM $tb_name ; $result_1 = mysql_query($sql_1, $connection) or die(mysql_error()); while($idinvoice = mysql_fetch_array($result_1)){ // get partial paid for THIS invoice $sql_2 = SELECT SUM(partpaidamount) as partpaid ; $sql_2 .= FROM $tb_name ; $sql_2 .= WHERE invoiceid = ' . $idinvoice['inid'] . ' ; $result_2 = @mysql_query($sql_2,$connection) or die(mysql_error()); //you will only be returning ONE row, so no need for while loop $row = mysql_fetch_array($result_2); $invoicepartpaid = $row['partpaid']; $total = $total + $invoicepartpaid; } // loop to NEXT invoice print(number_format($total, 2, '', ','). \n); Does this make sense? You must loop through each ID and query for that ID's partial amount. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Sum a column of values from a MySQL query
I am trying to sum a query of values from a MySQL table. The code I am using is: ---BEGIN CODE #1-- $sql_2 = SELECT SUM(partpaidamount) as partpaid FROM $tb_name WHERE invoiceid = \$invoiceid\ ; $result_2 = @mysql_query($sql_2,$connection) or die(mysql_error()); while ($row = mysql_fetch_array($result_2)) { $invoicepartpaid = $row['partpaid']; } ---END CODE #2 1) The code returns the sum of a partially paid invoice. The individual invoice is 'partpaid'. WORKS...NO PROBLEM 2) The while() then return a list of partially paid invoices which is $invoicepartpaid. WORKS..NO PROBLEM 3) I then want to add the list of partially paid invoices ($invoicepartpaid) together. I AM STUCK HERE ADDING THE INVOICES AMOUNTS TOGETHER. If anyone can help I would greatly appreciate it. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Sum a column of values from a MySQL query
I think you're a big confused here. Your query will only return one row, because you're using sum(). $invoicepartpaid will be your total for the invoiceid specified. -Original Message- From: Ben C. [mailto:[EMAIL PROTECTED] Sent: Monday, August 04, 2003 11:42 PM To: [EMAIL PROTECTED] Subject: [PHP] Sum a column of values from a MySQL query I am trying to sum a query of values from a MySQL table. The code I am using is: ---BEGIN CODE #1-- $sql_2 = SELECT SUM(partpaidamount) as partpaid FROM $tb_name WHERE invoiceid = \$invoiceid\ ; $result_2 = @mysql_query($sql_2,$connection) or die(mysql_error()); while ($row = mysql_fetch_array($result_2)) { $invoicepartpaid = $row['partpaid']; } ---END CODE #2 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Sum a column of values from a MySQL query
[snip] Sorry, Jay, but that's a horrible method. You could just run one query with a GROUP BY clause to get the sum for each invoiceID, instead of running multiple queries like you've mentioned... $query = SELECT invoiceid, SUM(partpaidamount) AS partpaid FROM $tb_name GROUP BY invoiceid; $rs = mysql_query($query) or die(mysql_error()); while($r = mysql_fetch_assoc($rs)) { echo Invoice: {$r['invoiceid']}, SUM: {$r['partpaid']}; } ---John Holmes... PS: Sorry for the top post, but I'm at work and outlook express sucks. :) [/snip] No problem bro'. I knew it was the long way around I was trying to understand if that was what Ben wanted. And I am so old school that almost always avoid ternary! :) And I feel for youExpress...blech! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sum a column of values from a MySQL query
From bitter experience I'll suggest that it's a REALLY GOOD idea to store invoice totals, subtotals, etc. in either the invoice header record. If not rounding errors, or some small change in logic, can come back and bite you - accountants get *really* upset when that 3000.00 invoice becomes 3000.01 or 2999.99. Know what else? THEY'LL NEVER LET YOU FORGET. Don't call it denormalization - call it history. Cheers - Miles At 03:53 PM 8/5/2003 +0800, Jason Wong wrote: On Tuesday 05 August 2003 15:43, Ben C. wrote: Yes, I know. However, the while() loop should generate all the invoice in a list. As has been pointed out the query only results in a single row. So how would the while-loop generate all the invoice[s]? It's only ever given a single row to play with. And has also been pointed out, could you state clearly: - what exactly you're trying to do - what your database schema is - what you tried that didn't work, and *how* it didn't work -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general -- /* I appoint you ambassador to Fantasy Island!!! */ -- 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
Re: [PHP] Sum a column of values from a MySQL query
Quoting Ben C. [EMAIL PROTECTED]: I am trying to sum a query of values from a MySQL table. The code I am using is: ---BEGIN CODE #1-- $sql_2 = SELECT SUM(partpaidamount) as partpaid FROM $tb_name WHERE invoiceid = \$invoiceid\ ; $result_2 = @mysql_query($sql_2,$connection) or die(mysql_error()); while ($row = mysql_fetch_array($result_2)) { $invoicepartpaid = $row['partpaid']; } ---END CODE #2 1) The code returns the sum of a partially paid invoice. The individual invoice is 'partpaid'. WORKS...NO PROBLEM 2) The while() then return a list of partially paid invoices which is $invoicepartpaid. WORKS..NO PROBLEM 3) I then want to add the list of partially paid invoices ($invoicepartpaid) together. I AM STUCK HERE ADDING THE INVOICES AMOUNTS TOGETHER. Well, instead of doing $invoicepartpaid = $row['partpaid']; you could do $invoicepartpaid += $row['partpaid']; which will just add $row['partpaid'] to $invoicepartpaid, not replace it. However, why not just SUM all the rows in the table in the query if you just want a total? $sql_2 = SELECT SUM(partpaidamount) as partpaid FROM $tb_name; -- Adam Alkins http://www.rasadam.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Sum a column of values from a MySQL query
I am trying to sum a query of values from a MySQL table. The code I am using is: ---BEGIN CODE #1-- $sql_2 = SELECT SUM(partpaidamount) as partpaid FROM $tb_name WHERE invoiceid = \$invoiceid\ ; $result_2 = @mysql_query($sql_2,$connection) or die(mysql_error()); while ($row = mysql_fetch_array($result_2)) { $invoicepartpaid = $row['partpaid']; } ---END CODE #2 1) The code returns the sum of a partially paid invoice. The individual invoice is 'partpaid'. WORKS...NO PROBLEM 2) The while() then return a list of partially paid invoices which is $invoicepartpaid. WORKS..NO PROBLEM 3) I then want to add the list of partially paid invoices ($invoicepartpaid) together. I AM STUCK HERE ADDING THE INVOICES AMOUNTS TOGETHER. If anyone can help I would greatly appreciate it. -- The content of this email message and any attachments are confidential and may be legally privileged, intended solely for the addressee. If you are not the intended recipient, be advised that any use, dissemination, distribution, or copying of this e-mail is strictly prohibited. If you receive this message in error, please notify the sender immediately by reply email and destroy the message and its attachments. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Sum a column of values from a MySQL query
Yes, I know. However, the while() loop should generate all the invoice in a list. -Original Message- From: Giz [mailto:[EMAIL PROTECTED] Sent: Tuesday, August 05, 2003 12:03 AM To: 'Ben C.'; [EMAIL PROTECTED] Subject: RE: [PHP] Sum a column of values from a MySQL query I think you're a big confused here. Your query will only return one row, because you're using sum(). $invoicepartpaid will be your total for the invoiceid specified. -Original Message- From: Ben C. [mailto:[EMAIL PROTECTED] Sent: Monday, August 04, 2003 11:42 PM To: [EMAIL PROTECTED] Subject: [PHP] Sum a column of values from a MySQL query I am trying to sum a query of values from a MySQL table. The code I am using is: ---BEGIN CODE #1-- $sql_2 = SELECT SUM(partpaidamount) as partpaid FROM $tb_name WHERE invoiceid = \$invoiceid\ ; $result_2 = @mysql_query($sql_2,$connection) or die(mysql_error()); while ($row = mysql_fetch_array($result_2)) { $invoicepartpaid = $row['partpaid']; } ---END CODE #2 -- 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
Re: [PHP] Sum a column of values from a MySQL query
On Tuesday 05 August 2003 15:43, Ben C. wrote: Yes, I know. However, the while() loop should generate all the invoice in a list. As has been pointed out the query only results in a single row. So how would the while-loop generate all the invoice[s]? It's only ever given a single row to play with. And has also been pointed out, could you state clearly: - what exactly you're trying to do - what your database schema is - what you tried that didn't work, and *how* it didn't work -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general -- /* I appoint you ambassador to Fantasy Island!!! */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Sum a column of values from a MySQL query
[snip] However, why not just SUM all the rows in the table in the query if you just want a total? $sql_2 = SELECT SUM(partpaidamount) as partpaid FROM $tb_name; [/snip] That is what he is doing. The SELECT SUM will only return ONE row! I am not sure that the problem is being explained clearly though. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Sum a column of values from a MySQL query
-Original Message- From: Ben C. [mailto:[EMAIL PROTECTED] Sent: 05 August 2003 07:42 I am trying to sum a query of values from a MySQL table. The code I am using is: ---BEGIN CODE #1-- $sql_2 = SELECT SUM(partpaidamount) as partpaid FROM $tb_name WHERE invoiceid = \$invoiceid\ ; $result_2 = @mysql_query($sql_2,$connection) or die(mysql_error()); while ($row = mysql_fetch_array($result_2)) { $invoicepartpaid = $row['partpaid']; } ---END CODE #2 1) The code returns the sum of a partially paid invoice. The individual invoice is 'partpaid'. WORKS...NO PROBLEM That's a very confusing description -- what do you mean by sum of a partially paid invoce? What you're getting is the sum of partpaidamount for all invoices with the given invoiceid -- although how you can have more than one invoice with the same invoiceid beats me! 2) The while() then return a list of partially paid invoices which is $invoicepartpaid. WORKS..NO PROBLEM No, wrong -- it can't and doesn't return a list, since the query above will return a *single* value -- the sum of partpaidamount for the selected invoices. That's one row with one value in it. 3) I then want to add the list of partially paid invoices ($invoicepartpaid) together. I AM STUCK HERE ADDING THE INVOICES AMOUNTS TOGETHER. But you've already added the partially paid invoice amount together with your SELECT SUM(partpaidamount) -- why are you trying to do it again? There seems to be a fundamental problem here somewhere -- why don't you go right back to the beginning and describe: (1) The table you are selecting from (fieldnames, what they contain, etc.). (2) Exactly what you are trying to extract from it, using what criteria. (3) Which exact values from that you want to add together. (4) and what you expect the final result to be. Then we might all be starting from a level playing field in terms of understanding exactly what you're trying to do (and if not, someone will be sure to ask further questions!). Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] sum
I am using the mysql sum() function to return the sum of a bunch of dolor amounts stored float values. But what I get back it something like this 98.18855591 it is to precise. All I want is the dolor amounts added up and rounded up to the closest penny. Is there a better value type to use or a function that will return the information properly formatted. Mike Fifield Charles Schwab Co, Inc. WARNING: All e-mail sent to or from this address will be received by the Charles Schwab corporate e-mail system and is subject to archival and review by someone other than the recipient.
Re: [PHP] sum
On Mon, May 06, 2002 at 10:01:00AM -0700, Fifield, Mike wrote: amounts stored float values. But what I get back it something like this 98.18855591 it is to precise. All I want is the dolor amounts added up and rounded up to the closest penny. Is there a better value type to use or http://www.mysql.com/doc/M/a/Mathematical_functions.html or http://www.php.net/manual/en/function.round.php --Dan -- PHP classes that make web design easier SQL Solution | Layout Solution | Form Solution sqlsolution.info | layoutsolution.info | formsolution.info T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y 4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Sum function
I am looking for code that explains how to sum a value in a database based on a current field retrieved from the database. Example: query = "select course_name, course_build, milestone, testcase, time_tested, issue_status, comment, bug_number, tested_by, date_tested from testissues where test_issue_id = $viewid"; $result = mysql_query( $query ); 1 - retrieve needed fields from database based on the id the user links from ($viewid) 2 - assign the course_name value from the query statement to a variable to be used on html page Assign: $course_name = mysql_result( $result, 0, "course_name" ); Use variable:print " tdfont color = #00b$course_name/b/font/td\n"; Now I would like to sum ALL the time_tested values in my database WHERE the course_name = $course_name (the variable retrieved for the record) query = "select sum(time_tested) where course_name = $course_name"; $result = mysql_query( $query ); $total_time = mysql_result( $result, 0, "sum(time_tested)"); First issue -- php does not seem to recognize the value of $course_name Second issue -- will the $total_time value be calculated correctly based on the above syntax? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Sum function
On Mon, 19 Feb 2001 06:57, Claudia wrote: I am looking for code that explains how to sum a value in a database based on a current field retrieved from the database. Example: query = "select course_name, course_build, milestone, testcase, time_tested, issue_status, comment, bug_number, tested_by, date_tested from testissues where test_issue_id = $viewid"; $result = mysql_query( $query ); 1 - retrieve needed fields from database based on the id the user links from ($viewid) 2 - assign the course_name value from the query statement to a variable to be used on html page Assign: $course_name = mysql_result( $result, 0, "course_name" ); Use variable:print " tdfont color = #00b$course_name/b/font/td\n"; Now I would like to sum ALL the time_tested values in my database WHERE the course_name = $course_name (the variable retrieved for the record) query = "select sum(time_tested) where course_name = $course_name"; $result = mysql_query( $query ); $total_time = mysql_result( $result, 0, "sum(time_tested)"); First issue -- php does not seem to recognize the value of $course_name Do you mean here: query = "select sum(time_tested) where course_name = $course_name"; because in that query $course_name should be enclosed in single quotes if it is a char type field. Second issue -- will the $total_time value be calculated correctly based on the above syntax? What type of field is time_tested? -- David Robley| WEBMASTER Mail List Admin RESEARCH CENTRE FOR INJURY STUDIES | http://www.nisu.flinders.edu.au/ AusEinet| http://auseinet.flinders.edu.au/ Flinders University, ADELAIDE, SOUTH AUSTRALIA -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Sum from Arrays ?
It's probably a stupid question but... I've got the following ; SELECT t_category_name COUNT(*) AS count FROM t_city,t_zipcodecity,t_location,t_category , t_locationcategory WHERE (t_category_id_category = t_locationcategory_id_category) AND (t_location_id_location = t_locationcategory_id_location) AND (t_zipcodecity_zipcode = t_location_zipcode) AND (t_city_id_city = t_zipcodecity_id_city) AND (t_city_id_city = 1) AND ((t_location_sex = 4) OR (t_location_sex = 0)) GROUP BY t_category_name"; $result1 = mysql_db_query(DBWEB, $query); print mysql_error(); echo " TABLE width=\"100%\" "; if ($result1) { while ($r = mysql_fetch_array($result1)) { $numbers = $r["count"]; $names =$r["$t_category_name"]; And what I want is a total for my count - I think it something to do with the mySQL sum funktion but I've got a bit stuck ! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Sum from Arrays ?
Hum, maybe the count() function. Works with mysql_fetch_array() too. Here's a sample: $a[0] = 1; $a[1] = 3; $a[2] = 5; $result = count ($a); //$result == 3 Here's a link: http://php.net/manual/en/function.count.php DISCLAIMER: I am by no means well-versed in this, or any other, topic - Shane - Original Message - From: "Keith Whyman" [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Wednesday, February 07, 2001 12:15 PM Subject: [PHP] Sum from Arrays ? It's probably a stupid question but... I've got the following ; SELECT t_category_name COUNT(*) AS count FROM t_city,t_zipcodecity,t_location,t_category , t_locationcategory WHERE (t_category_id_category = t_locationcategory_id_category) AND (t_location_id_location = t_locationcategory_id_location) AND (t_zipcodecity_zipcode = t_location_zipcode) AND (t_city_id_city = t_zipcodecity_id_city) AND (t_city_id_city = 1) AND ((t_location_sex = 4) OR (t_location_sex = 0)) GROUP BY t_category_name"; $result1 = mysql_db_query(DBWEB, $query); print mysql_error(); echo " TABLE width=\"100%\" "; if ($result1) { while ($r = mysql_fetch_array($result1)) { $numbers = $r["count"]; $names =$r["$t_category_name"]; And what I want is a total for my count - I think it something to do with the mySQL sum funktion but I've got a bit stuck ! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Sum from Arrays ?
No I've explained this badly I've got the array $a what I want is a sum of what's in the array ie from your sample the answer would be 1+3+5 = 9 Thanks Keith Hum, maybe the count() function. Works with mysql_fetch_array() too. Here's a sample: $a[0] = 1; $a[1] = 3; $a[2] = 5; $result = count ($a); //$result == 3 Here's a link: http://php.net/manual/en/function.count.php DISCLAIMER: I am by no means well-versed in this, or any other, topic - Shane - Original Message - From: "Keith Whyman" [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Wednesday, February 07, 2001 12:15 PM Subject: [PHP] Sum from Arrays ? It's probably a stupid question but... I've got the following ; SELECT t_category_name COUNT(*) AS count FROM t_city,t_zipcodecity,t_location,t_category , t_locationcategory WHERE (t_category_id_category = t_locationcategory_id_category) AND (t_location_id_location = t_locationcategory_id_location) AND (t_zipcodecity_zipcode = t_location_zipcode) AND (t_city_id_city = t_zipcodecity_id_city) AND (t_city_id_city = 1) AND ((t_location_sex = 4) OR (t_location_sex = 0)) GROUP BY t_category_name"; $result1 = mysql_db_query(DBWEB, $query); print mysql_error(); echo " TABLE width=\"100%\" "; if ($result1) { while ($r = mysql_fetch_array($result1)) { $numbers = $r["count"]; $names =$r["$t_category_name"]; And what I want is a total for my count - I think it something to do with the mySQL sum funktion but I've got a bit stuck ! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Sum from Arrays ?
As of PHP 4.0.4 the function array_sum() exists (it's undocumented, but in changelog), it's used as such : $array = array(1,3,5); $sum = array_sum($array); Or loop through array as such : foreach ($array as $value) { $sum += $value; } Either way, $sum now equals 9. For info on how all the above works, see : http://www.php.net/manual/en/control-structures.foreach.php http://php.net/ChangeLog-4.php http://www.php.net/manual/en/language.operators.assignment.php Regards, Philip Olson http://www.cornado.com/ On Wed, 7 Feb 2001, Keith Whyman wrote: No I've explained this badly I've got the array $a what I want is a sum of what's in the array ie from your sample the answer would be 1+3+5 = 9 Thanks Keith Hum, maybe the count() function. Works with mysql_fetch_array() too. Here's a sample: $a[0] = 1; $a[1] = 3; $a[2] = 5; $result = count ($a); //$result == 3 Here's a link: http://php.net/manual/en/function.count.php DISCLAIMER: I am by no means well-versed in this, or any other, topic - Shane - Original Message - From: "Keith Whyman" [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Wednesday, February 07, 2001 12:15 PM Subject: [PHP] Sum from Arrays ? It's probably a stupid question but... I've got the following ; SELECT t_category_name COUNT(*) AS count FROM t_city,t_zipcodecity,t_location,t_category , t_locationcategory WHERE (t_category_id_category = t_locationcategory_id_category) AND (t_location_id_location = t_locationcategory_id_location) AND (t_zipcodecity_zipcode = t_location_zipcode) AND (t_city_id_city = t_zipcodecity_id_city) AND (t_city_id_city = 1) AND ((t_location_sex = 4) OR (t_location_sex = 0)) GROUP BY t_category_name"; $result1 = mysql_db_query(DBWEB, $query); print mysql_error(); echo " TABLE width=\"100%\" "; if ($result1) { while ($r = mysql_fetch_array($result1)) { $numbers = $r["count"]; $names =$r["$t_category_name"]; And what I want is a total for my count - I think it something to do with the mySQL sum funktion but I've got a bit stuck ! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Sum from Arrays ?
Try this: for($i=0;$i count($the_array); $i++) $sum = $sum + $the_array[$i]; or if the index of your array is not in order, try this: while($element = array_pop($the_array)) $sum = $sum + $element; Hope that helps. Rdb At 06:38 PM 2/7/01 +0100, you wrote: No I've explained this badly I've got the array $a what I want is a sum of what's in the array ie from your sample the answer would be 1+3+5 = 9 Thanks Keith Hum, maybe the count() function. Works with mysql_fetch_array() too. Here's a sample: $a[0] = 1; $a[1] = 3; $a[2] = 5; $result = count ($a); //$result == 3 Here's a link: http://php.net/manual/en/function.count.php DISCLAIMER: I am by no means well-versed in this, or any other, topic - Shane - Original Message - From: "Keith Whyman" [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Wednesday, February 07, 2001 12:15 PM Subject: [PHP] Sum from Arrays ? It's probably a stupid question but... I've got the following ; SELECT t_category_name COUNT(*) AS count FROM t_city,t_zipcodecity,t_location,t_category , t_locationcategory WHERE (t_category_id_category = t_locationcategory_id_category) AND (t_location_id_location = t_locationcategory_id_location) AND (t_zipcodecity_zipcode = t_location_zipcode) AND (t_city_id_city = t_zipcodecity_id_city) AND (t_city_id_city = 1) AND ((t_location_sex = 4) OR (t_location_sex = 0)) GROUP BY t_category_name"; $result1 = mysql_db_query(DBWEB, $query); print mysql_error(); echo " TABLE width=\"100%\" "; if ($result1) { while ($r = mysql_fetch_array($result1)) { $numbers = $r["count"]; $names =$r["$t_category_name"]; And what I want is a total for my count - I think it something to do with the mySQL sum funktion but I've got a bit stuck ! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ "God is the unrestricted act of understanding, the eternal rapture glimpsed in every Archimedean cry of Eureka." - Bernard Lonergan - ~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^ Reuben Donald Budiardja [EMAIL PROTECTED] Homepage: http://www.goshen.edu/~reubendb -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]