我想获取两个表数据然后获取百分比金额

时间:2018-09-01 18:10:51

标签: php sql codeigniter

这是我的代码,

$query= $this->db->select('tbl_donated.*,tbl_donation_post.*,tbl_users.*,SUM(tbl_donated.dd_amount) AS dd_amount,SUM(tbl_donated.dd_amount_se) AS dd_amount_se')
                ->from('tbl_donated,tbl_users') 
                ->join('tbl_donation_post','tbl_donation_post.d_id = tbl_donated.d_id','left')
                ->where('tbl_donation_post.d_status',1)
                ->where('tbl_donated.dd_status',1)

                ->where('tbl_donation_post.d_created_by = tbl_users.user_id')
                ->group_by('tbl_donation_post.d_id')
                ->get();

        $data = $query->result_array();

$arr = array();
        $i = 0;
        foreach($data as $field){
             $percent = round((($field['ddd_amount']+$field['ddd_amount_se'])/$field['d_amount'])*100);
             '<br>';
            if($percent >= 100){
                $arr[$i] = $field;
                $i++;
            }
        }
        return $arr;

我想从两个表中获取百分比,但是代码显示数据库错误。我找不到错误。 我在上面显示了所有模型代码。

1 个答案:

答案 0 :(得分:1)

我认为

 $percent = round((($field['ddd_amount']+$field['ddd_amount_se'])/$field['d_amount'])*100);

应该是

 $percent = round((($field['dd_amount']+$field['dd_amount_se'])/$field['dd_amount'])*100);

ddd_amount,ddd_amount_se,d_amount

应该是

dd_amount,dd_amount_se,dd_amount