indie Posted July 8, 2010 Share Posted July 8, 2010 Nice forum here. I know nothing about PHP and need some help. Here is the deal. Members on a forum will be rating products. There will be 4 (this number could change) fields where they will select a number from a drop down menu to rate different characteristics of the product. I need to average those numbers for the overall rating. The code that outputs the individual numbers selected looks like this: {$data['record']['field_14']} 14 being one of the fields, and 15, etc. So what I need is code that takes multiple fields and outputs the average number. I appreciate any help! And please keep in mind I am not familiar with PHP coding, so if you could be as detailed as possible I would appreciate it. Thanks! Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 8, 2010 Share Posted July 8, 2010 I guess the four fields are named field_14 to field_17. To get the average you'd use some basic maths, add all 4 fields together and divide by 4. $avgerage = ($data['record']['field_14'] + $data['record']['field_15'] + $data['record']['field_16'] + $data['record']['field_17']) / 4; Quote Link to comment Share on other sites More sharing options...
indie Posted July 8, 2010 Author Share Posted July 8, 2010 Wow, that was a fast answer! Let me try this and post back. Thanks. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted July 8, 2010 Share Posted July 8, 2010 Or to do it dynamically, assuming all the fields in record are used: $avg = array_sum($data['record']) / count($data['record']); Quote Link to comment Share on other sites More sharing options...
indie Posted July 8, 2010 Author Share Posted July 8, 2010 Thanks. Actually those fields are used for other input as well so can't. But I would like to do an average of numbers instead of dividing by 4, just in case 3 of the 4 review fields are used by a reviewer. The first code above would divide by 4 even if 3 were entered. Is there a way to do an average of the numbers that exist instead of dividing by 4? Thanks for everyone's input so far! Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted July 8, 2010 Share Posted July 8, 2010 Thanks. Actually those fields are used for other input as well so can't. But I would like to do an average of numbers instead of dividing by 4, just in case 3 of the 4 review fields are used by a reviewer. The first code above would divide by 4 even if 3 were entered. Is there a way to do an average of the numbers that exist instead of dividing by 4? Thanks for everyone's input so far! Yes, just what I showed you, except you need to isolate the fields somehow so you know how many. Instead of putting those fields in $data['record'], put them in $data['review'] and loop through that. Not sure what type of inputs they are, but you need to get rid of blank values if there are any: $data['review'] = array_filter($data['review']); $avg = array_sum($data['review']) / count($data['review']); Or to use the fields as they are: $review = array_filter(array($data['record']['field_14'], $data['record']['field_15'], $data['record']['field_16'], $data['record']['field_17'])); $avg = array_sum($review) / count($review); Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 8, 2010 Share Posted July 8, 2010 I'm thinking this would be a good starting point SELECT `product`, AVG(`rating`) AS user_rating FROM `products_table` GROUP BY `product` Quote Link to comment Share on other sites More sharing options...
indie Posted July 28, 2010 Author Share Posted July 28, 2010 Or to use the fields as they are: $review = array_filter(array($data['record']['field_14'], $data['record']['field_15'], $data['record']['field_16'], $data['record']['field_17'])); $avg = array_sum($review) / count($review); This looks like it will be the best for what I need, but I am getting parse errors on my forum, and I don't know what I'm doing. Could someone PM me and I could tell them my site for a little help? Familiar with IPB a plus. Thanks! Quote Link to comment Share on other sites More sharing options...
indie Posted July 29, 2010 Author Share Posted July 29, 2010 Thanks everyone for your help. AbraCadaver, Your code works great, thanks! Quote Link to comment Share on other sites More sharing options...
indie Posted July 29, 2010 Author Share Posted July 29, 2010 $review = array_filter(array($data['record']['field_14'], $data['record']['field_15'], $data['record']['field_16'], $data['record']['field_17'])); $avg = array_sum($review) / count($review); How could I make this code shorten or round the average number to the nearest hundredth? For example: 3.7777777776 would become 3.77, or 3.78 rounded. I'd like the ability for hundredths. Sometimes it shows tenths (3.5) sometimes hundredths (3.75), but don't want any longer than that. Some results are whole numbers, and I don't want it to affect that. 3 should stay 3, not 3.00. Thanks! Quote Link to comment Share on other sites More sharing options...
Alex Posted July 29, 2010 Share Posted July 29, 2010 echo round($avg, 2); round Quote Link to comment Share on other sites More sharing options...
indie Posted July 29, 2010 Author Share Posted July 29, 2010 Thanks. Where does that go in the code? Quote Link to comment Share on other sites More sharing options...
Alex Posted July 29, 2010 Share Posted July 29, 2010 Just change this line: $avg = array_sum($review) / count($review); to: $avg = round(array_sum($review) / count($review), 2); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.