繁体   English   中英

使用Codeigniter计算表格中的平均评分

[英]Calculate the average rating in a table using Codeigniter

我有一个名为company_reviews的表,我在其中存储评论和用户提交的评分编号(1-5)。 我需要计算许多评论之间的平均值,并只输出一个小数点(例如2.5、3.5、2.1、5.1等)。在其他方面,我需要计算与id = x相关的每个company_reviews_rating的总和,其中company_reviews_company_idid = x

表结构

+--------------------+----------------------------+------------------------+
| company_reviews_id | company_reviews_company_id | company_reviews_rating |
+--------------------+----------------------------+------------------------+
|                  1 |                          1 |                      1 |
|                  2 |                          1 |                      5 |
|                  3 |                          1 |                      4 |
|                  4 |                          1 |                      4 |
|                  5 |                          1 |                      3 |
|                  6 |                          1 |                      2 |
+--------------------+----------------------------+------------------------+

到目前为止,这是我想出的,但是我被困在这里

public function count_total_rating($id = NULL) {
    $this->db->select('*');
    $this->db->where('company_reviews_company_id', $id);
    $this->db->from('company_reviews');
    $query = $this->db->get();
}

在Codeigniter模型中如何做到这一点?

public function count_total_rating($id = NULL) {
    $this->db->select('AVG(company_reviews_rating) as average');
    $this->db->where('company_reviews_company_id', $id);
    $this->db->from('company_reviews');
    $query = $this->db->get();
}

希望能帮助到你

 public function getAveRating(company_reviews_company_id){ 
        $where=array(
             "company_reviews_company_id"=>company_reviews_company_id
             );
        $this->db->where($where);
        $this->db->select_avg('company_reviews_rating');
        $query = $this->db->get('company_reviews')->first_row('array');
        return round($query['company_reviews_rating'], 2);
}

我希望这会有所帮助:D

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM