I am creating a rating system whereas the customer can rate the seller.
I am using the rateyo plugin, so there's a float value on it like 2.9, 3.8, 4.5 etc
I would like to get all the values of the seller and get the average from it to display his/her overall total ratings. I am using Codeigniter as a framework in PHP
Here's my code - View
<div class="rateyo" id= "rating"
data-rateyo-rating="0"
data-rateyo-num-stars="5"
data-rateyo-score="3">
</div>
<br>
<span class='result'>0</span>
<input type="hidden" name="rating">
Script
<script>
$(function () {
$(".rateyo").rateYo().on("rateyo.change", function (e, data) {
var rating = data.rating;
$(this).parent().find('.score').text('score :' $(this).attr('data-rateyo-score'));
$(this).parent().find('.result').text('Rating :' rating);
$(this).parent().find('input[name=rating]').val(rating); //add rating value to input field
});
});
</script>
Model
public function get_rate($id){
$this->db->where('book_id', $id);
$query = $this->db->get('rates');
return $query->row_array();
}
CodePudding user response:
You can use $this->db->select_avg('column_name')
in your select statement to get the average of the rates.
Also make sure that you use float datatype for in your column.
To study more about select_avg()