Pregunta

I have some values in my db (type string: ex id: 0001) but when I fetch data, CI convert it to int and I get 1 as result. How to avoid it? I want to display the db's value 0001 in the front

¿Fue útil?

Solución

you can do

Model

function getValues(){

    $query = $this->db->get('table_name');
    $result = array();

    foreach ($query->result() as $row) {
        $id = str_pad($row->id, 4, '0', STR_PAD_LEFT);
        $result[$id] = $row->type;
    }

    return $result;

}

Controller

$result = $this->model_var->getValues();
$data['result'] = $result;
$this->load->view('view_name',$data);

View

foreach($result as $key => $val) {
    echo 'key: ' . $key . ', value: ' . $val;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top