Get INT as INT and not as string - Codeigniter - Ajax

1

I have the following tabela , controller and model , I want to get what is int as int , not string .

Note that when the item is retrieved, the int field is enclosed in quotation marks. How do I recover the data correctly?

Table

DROP TABLE IF EXISTS 'tbl_banco';
CREATE TABLE IF NOT EXISTS 'tbl_banco' (
  'id' INT(11) NOT NULL AUTO_INCREMENT,
  'codigo' VARCHAR(5) NOT NULL,
  'nome' VARCHAR(255) NOT NULL,
  'assessoria_id' INT(11) NOT NULL,
  'dt_cadastro' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  'dt_atualizacao' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  'situacao' TINYINT(1) NULL DEFAULT NULL,
   PRIMARY KEY ('id'))
ENGINE = InnoDB
DEFAULT CHARSET = utf8;

Controller

//Função Obter Banco por ID
public function obter($id)
{
    if ($id == 0)
    {
        $id = $id;
        $dados = $this->banco->json_array($id);
        echo json_encode($dados);
    }
    else
    {
        $dados = $this->banco->obter($id);
        echo json_encode($dados);
    }
}

Model

/*** Obter por ID ***/
public function obter($id)
{
    $this->db->from($this->tbl_banco);
    $this->db->where('id',$id);
    $query = $this->db->get();
    return $query->row_array();
}

Ajax

function obter(id, acao) {
var ret;
$.ajax({
    type: 'GET',
    async: false,
    contentType: 'application/json',
    url : "banco/obter/" + id + "/" + acao,
    success: (function (obj) {          
        _obj = JSON.parse(obj);
        _obj_banco = _obj;
        objeto_form();
        ret = true;
    }),
    error: (function (erro) {
        trata_erro_ajax(erro);
        ret = false;
    })
});
return ret;
}
  

    
asked by anonymous 19.12.2017 / 19:08

1 answer

1

You can solve with JSON_NUMERIC_CHECK , an option of json_encode that was added in PHP 5.3.3

Problem and this will turn int all the numbers it gets, eg your codigo will return as 1 instead of 001

Just add the option at the time of encode echo json_encode($dados, JSON_NUMERIC_CHECK);

More infos can be found in json_encode () docs

    
19.12.2017 / 19:21