Return Value from Select

Asked

Viewed 154 times

0

Hello, I would like to know how to recover a result from a select in Controller to perform a sum of variables. I have two selects that I need the resulting value of them to add inside the Controller.

    // A = select total ja proposto
    $this->db->select('SUM(tb_proposicao.valor) as totalproposto');
    $this->db->where('tb_proposicao.usuarioid', 1;
    $this->db->where('tb_proposicao.tipoid', 1;
    $dados['totalproposto'] = $this->db->get('tb_proposicao')->result();


    // B = select limite
    $this->db->select('*');
    $this->db->where('tb_limites.usuarioid', 1;
    $this->db->where('tb_limites.tipoid', 1;
    $dados['limites'] = = $this->db->get('tb_limites')->result(); 

    $A + $B = $C
  • I don’t understand, what is the error? Select you refer to Query SQL?

1 answer

0

$A = $this->db ->select('SUM(tb_proposicao.valor) as totalproposto') ->where('tb_proposicao.usuarioid', 1) ->where('tb_proposicao.tipoid', 1) ->get('tb_proposicao')->row(); Row(), will rornarme only one column object

For calculations the "B" will return the whole table, kick that will be a Count.

$B = $this->db ->select('count(*) as limite') ->where('tb_limites.usuarioid', 1) ->where('tb_limites.tipoid', 1) ->get('tb_limites')->row();

Having the two objects just sum the values
It is interesting to inform the returned data types, whether float, double, int.. for calculations.

$C = floatval($A->totalproposto) + intval($B->limite);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.