Jump to content

Need code for an average of numbers


indie

Recommended Posts

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!

 

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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);

 

 

 

 

Link to comment
Share on other sites

  • 3 weeks later...

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!

Link to comment
Share on other sites

$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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.