0

I have 3 different tables in my database: providers, families and relationproviderfamily. Each provider can have many families.

In the form to add a new provider, I have a multiple select where you add the families that belong to that provider. I add the items to the select multiple using a select dropdown with an add button. /views/proveedor/add/

<label for="nombresFamilia" class="col-md-4 control-label"></label>
<div class="col-md-6">
    <select multiple id="nombresFamilia" name="nombresFamilia[]" class="form-control">
        <option value="CABLES">CABLES</option>
        <option value="PCR">PCR</option>
        <option value="POSTES">POSTES</option>
    </select>
</div>

I need to take the ID for the current provider and all the selected families and insert those values in the table relationproviderfamily, which only has to columns. So there should be a row for the provider and each family that belongs to it.

`idProveedor` int(11) NOT NULL,
`idFamilia` int(11) NOT NULL

This is the function I use in my model "Proveedormodel" to insert into database

function add_uk_proveedor_familia($id, $params){
    foreach($param as $clave){
        $clave = $this->db->query("SELECT id FROM familia WHERE clave = '$param'");
        $this->db->query("INSERT INTO 'relacionproveedorfamilia' ('idProveedor', 'idFamilia') VALUES ('$id', '$clave')");
    }
}

And this is where I call the function from the controller "Proveedor"

$idProveedor = $this->Proveedormodel->get_proveedor($id);
$params_familia = array(
    'nombresFamilia' => $this->input->post('nombresFamilia')
);
$relacion_proveedor = $this->Proveedormodel->add_uk_proveedor_familia($idProveedor, $params_familia);

I just started to work with codeigniter, and I've gotten so much help from other questions in this site, but I just haven't been able to pull this off. Any help would be very much appreciated.

You can check the repository of the project here

2 Answers 2

1

Try these code.

Form:

<label for="nombresFamilia" class="col-md-4 control-label"></label>
<div class="col-md-6">
    <select multiple="multiple" id="nombresFamilia" name="nombresFamilia[]" class="form-control">
        <option value="CABLES">CABLES</option>
        <option value="PCR">PCR</option>
        <option value="POSTES">POSTES</option>
    </select>
</div>

Controller "Proveedor":

$idProveedor = $this->Proveedormodel->get_proveedor($id);
$params_familia = array(
    'nombresFamilia' => implode(",", $this->input->post('nombresFamilia'))
);
$relacion_proveedor = $this->Proveedormodel->add_uk_proveedor_familia($idProveedor, $params_familia);

And in your model "Proveedormodel", just insert the data.

Sign up to request clarification or add additional context in comments.

Comments

0

Your nombresFamilia itself return array so remove appending array from controller

controller file

$idProveedor = $this->Proveedormodel->get_proveedor($id);
$params_familia = $this->input->post('nombresFamilia');

$relacion_proveedor = $this->Proveedormodel->add_uk_proveedor_familia($idProveedor, $params_familia);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.