I have a table with a set of fields. Each of these fields contains a number between one and 10.
|item_id|field1|field2|field3|field4|
|1 | 2 |5 |2 |9 |
|3 | 9 |3 |5 |10 |
|4 | 9 |9 |9 |10 |
What I would like to do is get the average of these fields. However, I need the number to be between 10 and 100.
First what you wanna do is multiply the rating by 10;
foreach ($results as $result) {
$rating = 0;
$rating = $rating + $result['field1'] + $result['field2'] + $result['field3'] + $result['field4'];
// Multiply rating by 10
$rating = $rating*10;
// Devide rating by 4
$rating = $rating/4;
// Output average
echo $result['item_id']." : ".$rating."<br />";
}
Or you could do it even simpler while doing your query:
SELECT
(field1 + field2 + field3 + field4)*10/4 AS average
FROM table
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.