Welcome Guest, Not a member yet? Register   Sign In
Store data using For Loop
#1

[eluser]omar_yoi[/eluser]
Hi Guys,

So here is my problem:

below is my html.
-first column, to set how many questions
-2nd and 3rd, will be the question and the answer.
-user will be able to set max 3 questions and answers.

Code:
<?php echo form_open('testing/test');?>
            <table>
                <tr>
                    <td>Jumlah Pertanyaan </td>
                    <td>&lt;input type="text" name="jml" /&gt;&lt;/td>
                </tr>
                <tr>
                    <td>Pertanyaan 1</td>
                    <td>&lt;input type="text" name="pertanyaan[]" /&gt;&lt;/td>
                </tr>
                <tr>
                    <td>Jawaban 1</td>
                    <td>&lt;input type="text" name="jawaban[]" /&gt;&lt;/td>
                </tr>
                <tr>
                    <td>Pertanyaan 2</td>
                    <td>&lt;input type="text" name="pertanyaan[]" /&gt;&lt;/td>
                </tr>
                <tr>
                    <td>Jawaban 2</td>
                    <td>&lt;input type="text" name="jawaban[]" /&gt;&lt;/td>
                </tr>
                <tr>
                    <td>Pertanyaan 3</td>
                    <td>&lt;input type="text" name="pertanyaan[]" /&gt;&lt;/td>
                </tr>
                <tr>
                    <td>Jawaban 3</td>
                    <td>&lt;input type="text" name="jawaban[]" /&gt;&lt;/td>
                </tr>
                <tr>
                    <td colspan="2" align="right">&lt;input type="submit" value="Simpan" name="submit" /&gt;&lt;/td>
                </tr>
            </table>
            
        &lt;?php echo form_close();?&gt;


Next, I would like to store this information on the database.
I have 5 columns on the table test: num, pertanyaan, jawaban, status, id.
the columns status, id will have the same values the only different will be num, pertanyaan and jawaban.

Code:
example
num     pertanyaan         jawaban     status     id
1       what is your name? Maulana     ok         1
2       what is your id?   maulana23   ok         1

now below is my model.

Code:
private $tbl = 'test';

$jml = $this->input->post('jml');
        $questions = $this->input->post('pertanyaan');
        $answers = $this->input->post('jawaban');
        
        
        
        for($i=0;$i<$jml;$i++){
            $question = $questions[$i];
            $answer = $answers[$i];
            $num = $i + 1;
            
            $qna = array(
                        'num' => $num,
                        'pertanyaan' => $question,
                        'jawaban' => $answer,
                        'status' => $this->input->post('status'),
                        'id' => $this->session->userdata('id')
                    );
                    
            $this->db->insert($this->$tbl,$qna);
        }

FYI:
num = number.
pertanyaan = question.
jawaban = answer.

Can someone give me a clue where I did wrong?


thanks in advance.
#2

[eluser]omar_yoi[/eluser]
Ok, I got it working, just simply a typo error,. XD

On this line.
Code:
$this->db->insert($this->$tbl,$qna);

Changed to
Code:
$this->db->insert($this->tbl,$qna);

Isn't that such a cute error? LMAO !

Thanks for reading though. Wink




Theme © iAndrew 2016 - Forum software by © MyBB