Re: [PHP] Average Number For Math Functions
In article [EMAIL PROTECTED], [EMAIL PROTECTED] says... Ok I think your definently going in the right direction - when I put this into my series I seem to be getting this message for each result: Resource id #40 or some other number that in some way represents something? Any Clue? Here is my SQL: $rsum =mysql_query(select sum(rating) from ratings)or die (mysql_error()); John You are missing quite a bit there - $rsum is merely a pointer to the data. Look at http://www.php.net/mysql_fetch_assoc which has an example of how to connect to mysql, issue a query and display the results of the query. Also, you might find http://www.php.net/extract useful in future. -- David Robley Temporary Kiwi! Quod subigo farinam -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Average Number For Math Functions
Ok I think your definently going in the right direction - when I put this into my series I seem to be getting this message for each result: Resource id #40 or some other number that in some way represents something? Any Clue? Here is my SQL: $rsum =mysql_query(select sum(rating) from ratings)or die (mysql_error()); John Martin Towell [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... if you have a table called ratings and a field called score in it, then doing this will show you the sum of all the scores: select sum(score) from ratings; eg, if the scores were 5, 2, 5, 8, and 2 then this sql should return 22 (unless I added it wrong... :/ ) -Original Message- From: JohnP [mailto:[EMAIL PROTECTED]] Sent: Friday, August 30, 2002 3:10 PM To: [EMAIL PROTECTED] Subject: Re: [PHP] Average Number For Math Functions I am still very new to PHP so things are still a little foreign to me - what exactly is the SUM finction - I tried to locate one on both the PHP and MySQL site but found nothing! Thanks ~ John Volve [EMAIL PROTECTED] wrote in message 02d901c24fd5$aba46020$7800a8c0@idiom">news:02d901c24fd5$aba46020$7800a8c0@idiom... What database are you using? MySQL has a SUM function which automatically selects the total of a column. -VolVE - Original Message - From: JohnP [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, August 29, 2002 21:25 Subject: Re: [PHP] Average Number For Math Functions Ok so how do I sum up an entire column in my db? For example if one row is : 1 , the next is 2, and the next is 1 - I need to have a total of 4 and the be able to divide by the num_rows The problem I ma having is the inside row addition Martin Towell [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... just sum them up and divide by the count - making sure to deal with a count of zero -Original Message- From: JohnP [mailto:[EMAIL PROTECTED]] Sent: Friday, August 30, 2002 10:56 AM To: [EMAIL PROTECTED] Subject: [PHP] Average Number For Math Functions Ok I looked at all the math functions for PHP but saw no way of returning the average of a set of numbers - I plan on using this for a rating system - any help? -- John -- 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Average Number For Math Functions
just sum them up and divide by the count - making sure to deal with a count of zero -Original Message- From: JohnP [mailto:[EMAIL PROTECTED]] Sent: Friday, August 30, 2002 10:56 AM To: [EMAIL PROTECTED] Subject: [PHP] Average Number For Math Functions Ok I looked at all the math functions for PHP but saw no way of returning the average of a set of numbers - I plan on using this for a rating system - any help? -- John -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Average Number For Math Functions
Ok so how do I sum up an entire column in my db? For example if one row is : 1 , the next is 2, and the next is 1 - I need to have a total of 4 and the be able to divide by the num_rows The problem I ma having is the inside row addition Martin Towell [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... just sum them up and divide by the count - making sure to deal with a count of zero -Original Message- From: JohnP [mailto:[EMAIL PROTECTED]] Sent: Friday, August 30, 2002 10:56 AM To: [EMAIL PROTECTED] Subject: [PHP] Average Number For Math Functions Ok I looked at all the math functions for PHP but saw no way of returning the average of a set of numbers - I plan on using this for a rating system - any help? -- John -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Average Number For Math Functions
At 05:55 PM 8/29/02 , JohnP wrote: Ok I looked at all the math functions for PHP but saw no way of returning the average of a set of numbers - I plan on using this for a rating system - any help? -- John Nope - you'll have to 'roll your own' by looping though the set, or (if you have version = 4.0.5) you can use array_reduce() in conjunction with the count() function. If these values are coming from a database, most databases have aggregate functions to do sums, averages, etc. -steve ++ | Steve Edberg [EMAIL PROTECTED] | | Database/Programming/SysAdmin(530)754-9127 | | University of California, Davis http://pgfsun.ucdavis.edu/ | +-- Gort, Klaatu barada nikto! --+
Re: [PHP] Average Number For Math Functions
Can't you just count the elements and then divide the total by the count? It seems pretty obfuscating to consider a function that takes an unknown number of elements, unless it were an array... still, seems pretty easy to count and divide total... doesn't it? -VolVE - Original Message - From: JohnP [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, August 29, 2002 20:55 Subject: [PHP] Average Number For Math Functions Ok I looked at all the math functions for PHP but saw no way of returning the average of a set of numbers - I plan on using this for a rating system - any help? -- John -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Average Number For Math Functions
if i remember 4th grade math class correctly... ? $total = 0; $count = 0; // get each of the numbers foreach($array_of_the_numbers as $key = $value) { $total += $array_of_the_numbers[$key]; ++$count; } // wow, that's a toughie! $average = $total / $count; // then you could make it pretty $pretty_average = number_format($average, 1); ? booyah. :) dave -Original Message- From: JohnP [mailto:[EMAIL PROTECTED]] Sent: Thursday, August 29, 2002 5:56 PM To: [EMAIL PROTECTED] Subject: [PHP] Average Number For Math Functions Ok I looked at all the math functions for PHP but saw no way of returning the average of a set of numbers - I plan on using this for a rating system - any help? -- John -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Average Number For Math Functions
I can't seem to see a pre-built functions but here is one I just wrote in my email client: function average($numberArray) { $sum = 0; for ($i=0;$icount($numberArray);$i++) $sum += $numberArray[$i]; return $sum / count($numberArray); } echo average(array (1,2,3,4,5)); Hope this helps :) Andrew - Original Message - From: JohnP [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Friday, August 30, 2002 1:55 AM Subject: [PHP] Average Number For Math Functions Ok I looked at all the math functions for PHP but saw no way of returning the average of a set of numbers - I plan on using this for a rating system - any help? -- John -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Average Number For Math Functions
Ok so how do I sum up an entire column in my db? For example if one row is : 1 , the next is 2, and the next is 1 - I need to have a total of 4 and the be able to divide by the num_rows The problem I ma having is the inside row addition If you're doing it on values from a database column then you're probably best off doing it as a part of your database query. If you're using MySQL then consult chapter 6 of the manual for all sorts of useful functions that can be used in a query. CYA, Dave -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Average Number For Math Functions
What database are you using? MySQL has a SUM function which automatically selects the total of a column. -VolVE - Original Message - From: JohnP [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, August 29, 2002 21:25 Subject: Re: [PHP] Average Number For Math Functions Ok so how do I sum up an entire column in my db? For example if one row is : 1 , the next is 2, and the next is 1 - I need to have a total of 4 and the be able to divide by the num_rows The problem I ma having is the inside row addition Martin Towell [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... just sum them up and divide by the count - making sure to deal with a count of zero -Original Message- From: JohnP [mailto:[EMAIL PROTECTED]] Sent: Friday, August 30, 2002 10:56 AM To: [EMAIL PROTECTED] Subject: [PHP] Average Number For Math Functions Ok I looked at all the math functions for PHP but saw no way of returning the average of a set of numbers - I plan on using this for a rating system - any help? -- John -- 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] Average Number For Math Functions
I am still very new to PHP so things are still a little foreign to me - what exactly is the SUM finction - I tried to locate one on both the PHP and MySQL site but found nothing! Thanks ~ John Volve [EMAIL PROTECTED] wrote in message 02d901c24fd5$aba46020$7800a8c0@idiom">news:02d901c24fd5$aba46020$7800a8c0@idiom... What database are you using? MySQL has a SUM function which automatically selects the total of a column. -VolVE - Original Message - From: JohnP [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, August 29, 2002 21:25 Subject: Re: [PHP] Average Number For Math Functions Ok so how do I sum up an entire column in my db? For example if one row is : 1 , the next is 2, and the next is 1 - I need to have a total of 4 and the be able to divide by the num_rows The problem I ma having is the inside row addition Martin Towell [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... just sum them up and divide by the count - making sure to deal with a count of zero -Original Message- From: JohnP [mailto:[EMAIL PROTECTED]] Sent: Friday, August 30, 2002 10:56 AM To: [EMAIL PROTECTED] Subject: [PHP] Average Number For Math Functions Ok I looked at all the math functions for PHP but saw no way of returning the average of a set of numbers - I plan on using this for a rating system - any help? -- John -- 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] Average Number For Math Functions
if you have a table called ratings and a field called score in it, then doing this will show you the sum of all the scores: select sum(score) from ratings; eg, if the scores were 5, 2, 5, 8, and 2 then this sql should return 22 (unless I added it wrong... :/ ) -Original Message- From: JohnP [mailto:[EMAIL PROTECTED]] Sent: Friday, August 30, 2002 3:10 PM To: [EMAIL PROTECTED] Subject: Re: [PHP] Average Number For Math Functions I am still very new to PHP so things are still a little foreign to me - what exactly is the SUM finction - I tried to locate one on both the PHP and MySQL site but found nothing! Thanks ~ John Volve [EMAIL PROTECTED] wrote in message 02d901c24fd5$aba46020$7800a8c0@idiom">news:02d901c24fd5$aba46020$7800a8c0@idiom... What database are you using? MySQL has a SUM function which automatically selects the total of a column. -VolVE - Original Message - From: JohnP [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, August 29, 2002 21:25 Subject: Re: [PHP] Average Number For Math Functions Ok so how do I sum up an entire column in my db? For example if one row is : 1 , the next is 2, and the next is 1 - I need to have a total of 4 and the be able to divide by the num_rows The problem I ma having is the inside row addition Martin Towell [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... just sum them up and divide by the count - making sure to deal with a count of zero -Original Message- From: JohnP [mailto:[EMAIL PROTECTED]] Sent: Friday, August 30, 2002 10:56 AM To: [EMAIL PROTECTED] Subject: [PHP] Average Number For Math Functions Ok I looked at all the math functions for PHP but saw no way of returning the average of a set of numbers - I plan on using this for a rating system - any help? -- John -- 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Average Number For Math Functions
I am still very new to PHP so things are still a little foreign to me - what exactly is the SUM finction - I tried to locate one on both the PHP and MySQL site but found nothing! http://www.mysql.com/doc/en/Group_by_functions.html#IDX1364 Well, it's close to "nothing" but if you study how the others are use, you'll get the idea how to use it as well... - E _ 最新のファイナンス情報とライフプランのアドバイス MSN マネー http://money.msn.co.jp/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php