0

Continuation of my previous question, which was resolved... I have this code in the model:

$this->db->select_max('ELDI_Orden');
        $res = $this->db->get('elementos_diccionario')->row()->ELDI_Orden;

        $eldi_key = url_title($this->input->post('eldi_key'), 'underscore', TRUE);
        $eldi_orden = $res+1;

        $datos = array(
            'ELDI_Key' => $eldi_key,
            'ELDI_Orden' => $eldi_orden,
            'ELDI_Display' => $this->input->post('eldi_display'),
            'ELDI_SuDiccionario' => $this->input->post('eldi_sudiccionario'),
        );

        $this->db->insert('elementos_diccionario', $datos);

But I need it to do that only when ELDI_SuDiccinario have different values: Database picture

Looking that image, I need the next ELDI_Orden (of ELDI_SuDiccionario 1) to be 5 and not 6.

ELDI_SuDIccionario    ELDI_Orden
        1                  1
        1                  2
        1                  3
        1                  4
    --NEW-- 1          --NEW-- 5
        2                  1
        2                  2
       ...                ...
        2                  5

I hope you can understand me, my english is very bad.

Thanks for your time.

Nira
  • 197
  • 1
  • 1
  • 12

3 Answers3

1

You need to add where condition to your select_max

$this->db->select_max('ELDI_Orden')
         ->where('ELDI_SuDiccionario', $this->input->post('eldi_sudiccionario'));
vasilenicusor
  • 2,023
  • 1
  • 21
  • 37
1

Hope this will help you :

Just add where condition to your your query where ELDI_SuDiccionario = 1

$this->db->select_max('ELDI_Orden');
$this->db->where('ELDI_SuDiccionario',1);

/*If u want using with post 
   $eld_id = $this->input->post('eldi_sudiccionario');
   $this->db->where('ELDI_SuDiccionario',$eld_id);
*/

$res = $this->db->get('elementos_diccionario')->row()->ELDI_Orden;

$eldi_key = url_title($this->input->post('eldi_key'), 'underscore', TRUE);
$eldi_orden = $res+1;

$datos = array(
            'ELDI_Key' => $eldi_key,
            'ELDI_Orden' => $eldi_orden,
            'ELDI_Display' => $this->input->post('eldi_display'),
            'ELDI_SuDiccionario' => $this->input->post('eldi_sudiccionario'),
        );

$this->db->insert('elementos_diccionario', $datos);

For more : https://www.codeigniter.com/user_guide/database/query_builder.html

Pradeep
  • 9,667
  • 13
  • 27
  • 34
1

just like this

$this->db->select_max('ELDI_Orden');
    $res = $this->db->where('ELDI_SuDIccionario',$this->input->post('eldi_sudiccionario'))->get('elementos_diccionario')->row()->ELDI_Orden;

    $eldi_key = url_title($this->input->post('eldi_key'), 'underscore', TRUE);
    $eldi_orden = $res+1;

    $datos = array(
        'ELDI_Key' => $eldi_key,
        'ELDI_Orden' => $eldi_orden,
        'ELDI_Display' => $this->input->post('eldi_display'),
        'ELDI_SuDiccionario' => $this->input->post('eldi_sudiccionario'),
    );

    $this->db->insert('elementos_diccionario', $datos);
manish
  • 423
  • 3
  • 9