How to 'defragment' an array to make an insert in the database?

1

In my controller perform the following arrangement

$DataPerson=
        array(
                    'documentos' => $this->input->post('documentos'),
                    'num_identificacion' => $this->input->post('num_identificacion'),
                    'nombre' => $this->input->post('nombre'),
                    'apellido' => $this->input->post('apellido'),
                    'sexo' => $this->input->post('sexo'));

$envio = $this->Person_model->insertPerson($DataPerson);

The model receives the parameter:

public function insertPerson($DataPerson)
{

}

How can I navigate the $DataPerson so that I can insert that data into the DB?

I would appreciate the interest.

    
asked by JDavid 22.06.2017 в 18:52
source

2 answers

1

You can do a foreach by indicating the key and the value of that field of your arrangement to access each element as you occupy it for your insert:

foreach($DataPerson as $key => $value){
    echo "Llave: ".$key."=> Valor: ".$value; //Ejemplo de como accederías en cada iteración al valor
}
  • $ key represents the name that associates the value you keep in your arrangement, or in mundane terms what is on the left side of => in your array.

  • $ value is just the value you are looking for for your inserts, or what is on the right side of => in your array.

answered by 22.06.2017 в 19:00
1

Do you use any FRAMEWORK?

In your controller, build your array with the same attributes of your table once the array is created, it is only a question of whether you pass it to the model there with the function insert will make the route

A parenthesis is not what your table is called, I assumed a name

function insertPerson($DataPerson) {
            $this->db->insert("empleados", $DataPerson);    
            if ($this->db->affected_rows() > 0) {
                return true;
            } else {
                return false;
            }
        }
    
answered by 22.06.2017 в 19:14