2

I'm am unable to get last inserted id with codeigniter and grocery crud and then pass them to autofill a field. How can I do that? I'm trying to solve with a function returning id and passing by the hidden field.

Post edited. Everything in the right order I'm working.

Database (Involved tables: citas, entregas)

Database

Scheme (Cita = date inserted):

Image from the process

Model function

 public function get_id(){                 

  //$this->db->query('SELECT MAX(cita) FROM citas;');
    $this->db->insert_id();
    $query = $this->db->get();

    echo $query;

    return $query->result();        

}

Controller(full controller function)

 public function entregas_lista($idCarga) {

        $crud = new grocery_CRUD();



        if ($this->Entregas_Model->get_rows($idCarga)) {
            $crud->unset_add();
        }


        $datos = array(
            'title' => "Solicitudes", 
            'username' => "Administrador"
        );

        $this->load->view('commons/header', $datos);



        $crud->set_language("spanish");
        $crud->set_theme('flexigrid');

        $crud->set_table('entregas');


        $crud->display_as('idCitas', 'Cita');
        $crud->display_as('idAcciones', 'Acción');
        $crud->display_as('idEstadoSolicitud', 'Estado Solicitud')
                ->display_as('horaCita', 'Hora Cita')
                ->display_as('numeroEntrega', 'Nº Entrega')
                ->display_as('Origen', 'Orígen')
                ->display_as('cargaPrevista', 'Carga Prevista')
                ->display_as('entregaPrevista', 'Entrega Prevista');



        $crud->where('entregas.idCitas =', $idCarga);

        $crud->display_as('idCarga', 'Nº Entrega');


        $crud->set_relation('idEstadoSolicitud', 'estadosolicitudes', 'nombreEstado');




        $query = $this->Entregas_Model->get_id();

        $crud->field_type('idCitas', 'hidden', $query);

        $output = $crud->render();

        $this->_example_output($output);


        $this->load->view('commons/footer');
    }
Jose
  • 87
  • 1
  • 8

2 Answers2

4

In order to use insert_id() in Codeigniter, you must Insert a new row in your DB. Otherwise you have to select the MAX(id) from your table

Example of insert_id():

$this->db->insert('test_table', $test_data);
return $this->db->insert_id();

Example of MAX(id):

$qry = $this->db->select('MAX(id)')
                ->from('test_table')
                ->get();
        if ($qry->num_rows() > 0)
            return $qry->row_array();
        return FALSE;

and you grab it in your Controller.

GeorgeGeorgitsis
  • 1,262
  • 13
  • 29
  • Thank you, but i am using the example 2 (MAX(id)) and it does'n allow me to save and come back to the list, and the field is empty. What am i doing wrong? (empty field image above) – Jose May 16 '17 at 09:17
  • You want to save something in Grocery crud and return back again to save something else? Honestly it is not so clear for me to understand what you are trying to do. – GeorgeGeorgitsis May 16 '17 at 09:23
  • That's it, I have to tables so, in the first table I am creating a date, this date is a callback_column that goes to the content of that date, but when i add something in the second table, it's asking me for the date again (Cita = date). I want to call the id of the date i have created and put it into that field. Is it possible? – Jose May 16 '17 at 09:34
  • Can you edit your post with your code? We have to see what you have done so far in order to help you. – GeorgeGeorgitsis May 16 '17 at 09:50
  • Of course, question edited in the correct order of the process – Jose May 16 '17 at 10:16
1

first $this->db->insert($table,$data);

after $insert_id= $this->db->insert_id();

echo $insert_id;

hamed hossani
  • 986
  • 2
  • 14
  • 33