Skip to content
Advertisement

How to get the SUM of 3 individual columns in codeigniter

I am trying to get the SUM of 3 individual columns. When I try to output I get nothing. What do I do wrong?

Model

    $this->db->select('SUM(priceParking) AS price_parking, SUM(priceCharing) AS price_charging, SUM(priceWash) AS price_wash');
    $this->db->from('bestillinger');
    $query=$this->db->get();
    return $query->result_array();

Controller

    $data['total_income'] = $this->accounting_model->total_parking_today();

View

    <span class="info-box-number"> <?php echo $total_income['price_parking']?>  
    <span class="info-box-number"> <?php echo $total_income['price_charging']?>  
    <span class="info-box-number"> <?php echo $total_income['price_wash']?>  

Advertisement

Answer

$this->db->select() accepts an optional second parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names. This is useful if you need a compound select statement where automatic escaping of fields may break them. (Reference)

$this->db->select('SUM(priceParking) AS price_parking, SUM(priceCharing) AS price_charging, SUM(priceWash) AS price_wash', false);
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement