array in insert php codeigniter

0

I have the following problem I am working with php and codeigniter, and I need to do the following insert in my model this is my code:

public function guardar_permisos(){
    
    $perfil = $this->session->userdata("perfil");
    $data = json_decode(stripslashes($this->input->post("data")));
    
    foreach ($data as $idpagina) {
      $this->db->set('Id_perfil', $perfil);
      $this->db->set('Id_pagina', $idpagina);
      $sql = $this->db->insert('perfil_pagina');
    }
           if($sql === true) {
           return true; 
        }else {
       return false;
      }

  }

this is my function to insert the data, in the profile variable I capture the id of the profile that the user of the session has, and the data variable by post I have an array and I need to insert a row for each value of the array. for example if I have in the variables the following data:

  • $ profile = 1
  • $ data = [2,3,5]

make an insert for each one

  • insert into profile_page (profile_id, page_id) values (1,2)
  • insert into profile_page (profile_id, page_id) values (1,3)
  • insert into profile_page (profile_id, page_id) values (1,5)

I hope you understand and give me a guide on how to do this in codeigniter

<form id="formid">
<label>V<input type="checkbox" value="1" name="check" class="up"><span class="lever"></span></label>
<label>G<input type="checkbox" value="2" name="check" class="up"><span class="lever"></span></label>
<label>M<input type="checkbox" value="3" name="check" class="up"><span class="lever"></span></label>
<label>E<input type="checkbox" value="4" name="check" class="up"><span class="lever"></span></label>
<button type="button" id="send" class="btn bg-blue">Guardar</button>
</form>

$(document).ready(function() {
          $('#send').click(function(){
           var select = [];
           $(":checkbox[name=check]").each(function() {
             if (this.checked) {
               select.push($(this).val());
              }
            });
           if (select.length) {
             var jsonString = JSON.stringify(select);
             $.ajax({
               cache: false,
               type: 'post',  
               data: {data : jsonString},  
               url: '<?php echo site_url();?>user/guardar',
               success: function(data) {
                }
              });
            }else
           alert('seleccione');     
           return false;
          });
        });
    
asked by max 26.01.2017 в 17:31
source

1 answer

0

That you could do in the controller, in the model only leaves the function to save. For example, the function in your model could look like this:

public function guardar_permisos($arreglo){
    if($this->db->insert('perfil_pagina',$arreglo)){
      return true;
    }else{
      return $this->db->error();
    }
}

And in your controller you could do something like this:

public function guardar(){
    $perfil = $this->session->userdata("perfil");
    $data = json_decode(stripslashes($this->input->post("data")));

    //suponiendo que data es un arreglo del tipo $data = [2,3,5]
    foreach($data as $key => $value){
      $arreglo = ["Id_perfil"=>$perfil, "Id_pagina" => $value];

      //mi_modelo cambialo por el nombre de tu modelo
      if($this->mi_modelo->guardar_permisos($arreglo)){
        echo "Agregado";
      }else{
        echo "Error";
      }
    }
}
    
answered by 26.01.2017 / 17:58
source