PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 26th, 2004, 09:25 AM
Spongy's Avatar
Spongy Spongy is offline
Alternately High
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Hilversum, Netherlands
Posts: 223 Spongy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 56 m 41 sec
Reputation Power: 5
Send a message via MSN to Spongy
Question Calculate average in PHP

Hi all,

I know that this is a real n00b question, and I'm not really a n00b anymore, but I've never needed something like this before...
I have a list of numbers in a db, and I want to write a function which calculates the average of it. You can guess that it's not the same amount of numbers all the time. It's an average rating of a pic users can vote at. Does anyone know how to calculate this? It would be great.

Cheers,
__________________
Work to live, don't live to work

Reply With Quote
  #2  
Old February 26th, 2004, 11:24 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Check out the following:

Code:

mysql> describe math;
+-------+---------------------------+------+-----+---------+----------------+
| Field | Type                      | Null | Key | Default | Extra          |
+-------+---------------------------+------+-----+---------+----------------+
| id    | int(10) unsigned zerofill |      | PRI | NULL    | auto_increment |
| num   | int(10)                   | YES  |     | NULL    |                |
+-------+---------------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
 
mysql> select * from math;
+------------+------+
| id         | num  |
+------------+------+
| 0000000001 |    5 |
| 0000000002 |    5 |
| 0000000003 |    5 |
| 0000000004 |    5 |
| 0000000005 |    5 |
| 0000000006 |    7 |
| 0000000007 |    7 |
| 0000000008 |    7 |
| 0000000009 |    7 |
| 0000000010 |    7 |
+------------+------+
10 rows in set (0.00 sec)
 
mysql> select count(*) as cnt, SUM(num) as sum, (SUM(num)/count(*)) as avg FROM math;
+-----+------+------+
| cnt | sum  | avg  |
+-----+------+------+
|  10 |   60 | 6.00 |
+-----+------+------+
1 row in set (0.00 sec)

__________________
Please don't PM me asking for solutions outside the scope of a thread.
Keeping all responses in a thread stands to help others who come along later,
which is after all what this forum's all about.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > Calculate average in PHP


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT