Search code examples
formsloopscodeigniterinput

How to post multiple inputs in database


I'm working with Codeigniter, on a sales targets' form for salesmen. They have to input values for each product, locality, year, etc. Product and locality are already get with existing database: no need to set rules (see controller).

When I checked the post (with enable_profiler of Codeigniter), I get this: enable_profiler result

The problem is these datas don't insert into the database table. I read and tested a lot, but always blocked.

Here is my model:

    public function add($params)
        {
            $this->db->insert($this->table, $params);
            return $this->db->insert_id();
        }

My controller:

    $this->load->library('form_validation');
    
            $this->form_validation->set_rules('year', 'Year', 'required|integer');
            $this->form_validation->set_rules('prevision', 'Prevision', 'required');
            $this->form_validation->set_rules('value', 'Value', 'required|integer');
            
            if ($this->form_validation->run()) {
                $params = array(
                    'year' => $this->input->post('year'),
                    'prevision' => $this->input->post('prevision'),
                    'locality_id' => $this->input->post('locality'),
                    'product' => $this->input->post('product'),
                    'value' => $this->input->post('value'),
                );
    
                $this->Objectif_model->add($params);
                redirect('admin/objectif');
            } else {
                $this->layout('admin/objectif/add');
            }

And my view with inputs:

<?php foreach ($products as $product) : ?>
<tr class="form-group">
    <td class="bg-warning">
    <?= $product->grp_product; ?>
    </td>
    <td class="bg-warning product">
    <?= $product->code; ?>
    </td>
        <?php foreach ($localities as $locality ) : ?>
        <td>
            <input type="text" class="form-control valeur" placeholder="Value k€" name="value[]" data-validation="number" data-validation-ignore="./" data-validation-optional="true" />
            <input type="text" name="year[]" />
            <input type="text" name="prevision[]" />
            <input type="text" name="product[]" value="<?= $product->code ?>" />
            <input type="text" name="localite_id[]" value="<?= $locality->id ?>" />
        </td>
        <?php endforeach; ?>
</tr>
<?php endforeach; ?>

Hope you will help me.


Solution

  • I find my answer. In my controller, I had to pass each post in a variable and made a loop.

        //action post in a var for each data
        $id = $this->input->post('id');
        $prev = $this->input->post('prevision');
        $year= $this->input->post('year');
        $locality_id = $this->input->post('locality_id');
        $product = $this->input->post('product');
        $value= $this->input->post('value');
    
        if ($this->form_validation->run()) {
            $count_id = count($id);
    
            if (isset($id)) {
                for ($i = 0; $i < $count_id; $i++) {
                    $obj[$i] = array(
                        'id' => $id[$i],
                        'prevision' => $prev,
                        'year' => $year,
                        'locality_id' => $locality_id[$i],
                        'product' => $product[$i],
                        'value' => $value[$i]*1000
                    );
                } // endfor
    
                if (isset($obj)) {
                    $this->Objectif_model->add_obj($obj);
                } // endif
             }// endif isset
        }// endif form validation