在Codeiginiter中使用公式变量

时间:2017-06-14 07:44:25

标签: php mysql codeigniter

大家好我试着在Codeigniter上使用变量等式,就像我在下面写的代码一样,我的代码写得正确吗?到目前为止,这是我的代码,我想在我的控制器中使用可变$ amount

在控制器

$amount = $this->m_data->getTotalSales->$data['TOTAL_SALES'];

,这在Model

//GET TOTAL REVENUE
function getTotalSales(){ 
$this->db->select("(SELECT SUM(grand_total) FROM sales_order WHERE member = ".$this->session->userdata('ID').") - (SELECT SUM(amount) FROM payment WHERE member_id = ".$this->session->userdata('ID').")  AS total_sales");

$query = $this->db->get(); 
    if ($query->num_rows() >0){ 
        foreach ($query->result() as $data) {

            $hasilSemua[] = $data;
        }
    return $hasilSemua; 
    }
}

2 个答案:

答案 0 :(得分:0)

$amount = $this->m_data->getTotalSales();

function getTotalSales(){ 

        $result1 = $this->db->select_sum("grand_total")
                           ->from('sales_order')
                           ->where("member = $this->session->userdata('ID')");
                           ->get();


        $result2 = $this->db->select_sum("amount")
                            ->from('payment')
                            ->where("member_id = $this->session->userdata('ID')")
                            ->get();    

        return $result1->row()-$result2->row(); 
    }
}

答案 1 :(得分:0)

我相信这就是你所追求的。请注意,我将UserID作为第二个参数传递给WHERE函数。这是因为用户有可能输入它(无论如何总是这样做)。

$itinerary = Itinerary::find($tour_id);
$itinerary->plan = $temp_itinerary[$i];        
$itinerary->day = $temp_day[$i];
$itinerary->save();

然后,您可以致电//GET TOTAL REVENUE function getTotalSales(){ $db = $this->db; $db->select('SUM(grand_total)'); $db->where('member',$this->session->userdata('ID')); $q1 = $db->get_compiled_select('sales_order'); $db->select('SUM(amount)'); $db->where('member_id',$this->session->userdata('ID')); $q2 = $db->get_compiled_select('payment'); $query = $db->query("SELECT ($q1) - ($q2) as total_sales"); $data = $query->row_array(); return $data['total_sales']; } 获取结果。