I want calculate the average for a rating system, but I have normal table in innodb and one column in json with name: "reviews" with this structure:
{"comments":
[
{"name": "Jonh", "text": "nice phone", "star": 4},
{"name": "igon", "text": "not good", "star": 2},
{"name": "marco", "text": "i not like this", "star": 3},
{"name": "david", "text": "good product", "stelle": 5}
]
}
Now I need calculate the average star. it is in sql or in php? In sql I dont know how, in php I have the problem with the query for extract only the all star, for example:
$reviews_nn = $rowprod["reviews"];
$reviews = json_decode($reviews_nn,true);
$max = 0;
$n = 0;
foreach ($reviews[comments][star] as $rate => $count) {
echo 'Seen ', $count, ' ratings of ', $rate, "\n";
$max += $rate * $count;
$n += $count;
}
echo 'Average rating: ', $max / $n, "\n";
But the result is NAN...Is not integer? It is integer in star...
star=4
not star="4"
I hope you can help me....Tanks!!!