multiple input selection with foreach nested on codeigniter 3

20 Views Asked by At

I use nested foreach to display a form with answer choices. This is the view I created:

<?php echo form_open('formulir/submit_survei_ikm'); ?>
<div class="container">
    <?php foreach ($pertanyaan_ikm as $keyp) : ?>
    <div class="card mb-3">
        <div class="card-header">
            <?php echo $keyp->isi_pertanyaan ?>
            <input type="hidden" name="id" value="<?php echo $keyp->id_pertanyaan?>" />
        </div>
        <div class="card-body">
            <?php foreach ($jawaban_ikm as $keyj) : ?>
            <?php if ($keyj->id_pertanyaan_ikm == $keyp->id_pertanyaan) : ?>
            <div class="form-check">
                <input class="form-check-input" type="radio" name="<?php echo $keyj->id_pertanyaan_ikm ?>
                  "value="<?php echo $keyj->value_jawaban ?>" required>
                <label class="form-check-label">
                    <?php echo $keyj->isi_jawaban ?>
                </label>            
            </div>
            <?php endif ?>
            <?php endforeach ?>
         </div>
    </div>
    <?php endforeach ?>
    <button type="submit" class="btn btn-primary">Simpan dan lanjutkan </button>
</div>
<?php echo form_close(); ?>

this is my controller :

public function submit_survei_ikm()
    {
        
        $data = [
            'id_responden' => $this->session->flashdata('id'),
            'id' => $this->input->post('id_pertanyaan_ikm'),
            'value_jawaban' => $this->input->post('value_jawaban')
        ];
        print_r($data);
        die;
        
        // $this->Formulir_model->insert_survei_ikm($data);
        $insert_id = $this->Formulir_model->insert_survei_ikm($data);
        $this->session->set_flashdata('id', $insert_id);

        $this->load->view('layout/header');
        $this->load->view('form_survei_ipk');
        $this->load->view('layout/footer');
        //echo 'data berhasil disimpan';
    }

This is my model :

public function insert_survei_ikm($data)
    {
        $this->db->insert('responden_ikm', $data);
        return $this->session->flashdata('id');
    }

I tried making a controller and model but it's not as expected. How do I make the form work to save the data?

0

There are 0 best solutions below