phploopscodeigniterforeachmultiple-insert

Codeigniter - multiple input with loop


I wanted to make the multiple inputs form with loop and the different value for each data I fill, it seems this code is working but it was only getting 1 data to 3 times with the same value.

what I need is that I can make different value for each data in the form.

Or maybe do I need a model for this one or something? been struggle for few days here please help.

this is what i expect for the result in the form loop

enter image description here

but I want to get this Output:- enter image description here

controller.php

public function aksi_soal3(){
        $ps5 = $this->input->post('soal');
        $ps6 = $this->input->post('opsi_a');
        $ps7 = $this->input->post('opsi_b');
        $ps11 = $this->input->post('kunci_jawaban');

        $data = array(
            array(
                'soal' => $ps5,
                'opsi_a' => $ps6,
                'opsi_b' => $ps7,
                'kunci_jawaban' => $ps11
            ),
            array(
                'soal' => $ps5,
                'opsi_a' => $ps6,
                'opsi_b' => $ps7,
                'kunci_jawaban' => $ps11
            ),
            array(
                'soal' => $ps5,
                'opsi_a' => $ps6,
                'opsi_b' => $ps7,
                'kunci_jawaban' => $ps11
            )
        );

        $this->db->insert_batch('soal',$data);
        redirect('guru/index');
    }

view.php

<!DOCTYPE html>
<html lang="en" >

<head>
  <title></title>
</head>

<body>
  
<?php
$i=1;
while ($i<=3){
foreach($tampilan as $soal){
    ?>
    <form action="<?php echo base_url()?>guru/aksi_soal3" method="post">
    
        <?php
    echo "  
            <input type='hidden' name='kode_soal' value='$soal->kode_soal''>
            <textarea placeholder='soal' name='soal'></textarea>

            <input type='text' name='opsi_a' placeholder='jawaban a'>
            <input type='text' name='opsi_b' placeholder='jawaban b'>
            <input type='text' name='kunci_jawaban' placeholder='Kunci jawaban' >
            </div>
            </div>
     ";
    ?>

        <?php
    $i=$i+1;
}}
?>   

<button type="submit" class="btn">Selesai</button>
</form>
</html>

Solution

  • You need to do something like below,

    Controller Part :-

        public function aksi_soal3(){
                $ps5 = $this->input->post('soal');
                $ps6 = $this->input->post('opsi_a');
                $ps7 = $this->input->post('opsi_b');
                $ps11 = $this->input->post('kunci_jawaban');
             $data = array();
            foreach($ps5 as $key=>$value) {
                $data[]  = array(
                        'soal' => $value,
                        'opsi_a' => $ps6[$key],
                        'opsi_b' => $ps7[$key],
                        'kunci_jawaban' => $ps11[$key]
                    );
            }
                $this->db->insert_batch('soal',$data);
                redirect('guru/index');
            }
    

    View Part :-

        <form action="<?php echo base_url()?>guru/aksi_soal3" method="post">
        
        <input type='hidden' name='kode_soal[]' value='$soal->kode_soal''>
        <textarea placeholder='soal' name='soal[]'></textarea>
        <input type='text' name='opsi_a[]' placeholder='jawaban a'>
        <input type='text' name='opsi_b[]' placeholder='jawaban b'>
        <input type='text' name='kunci_jawaban[]' placeholder='Kunci jawaban' >
        
        <button type="submit" class="btn">Selesai</button>
        </form>