user11174163
user11174163

Reputation:

Loop post array to make dynamic

I want to make this code simpler and cleaner, I am making quiz app, so I have 20 answers. Now that problem is what if I want to make it 50 answers. I do not want to type 50 new answers.

The code works well, but the problem is all are coded in static. How can I simplify this to become more effective in adding questions? I used radio button as its input. This is also the main problem because it does not have dynamic. Can I loop this? tried it before but it did not work as expected

public function resultDisplay(){

        $this->data['checks'] = array(
            'quest1' => $this->input->post('quizId1'),
            'quest2' => $this->input->post('quizId2'),
            'quest3' => $this->input->post('quizId3'),
            'quest4' => $this->input->post('quizId4'),
            'quest5' => $this->input->post('quizId5'),
            'quest6' => $this->input->post('quizId6'),
            'quest7' => $this->input->post('quizId7'),
            'quest8' => $this->input->post('quizId8'),
            'quest9' => $this->input->post('quizId9'),
            'quest10' => $this->input->post('quizId10'),
            'quest11' => $this->input->post('quizId11'),
            'quest12' => $this->input->post('quizId12'),
            'quest13' => $this->input->post('quizId13'),
            'quest14' => $this->input->post('quizId14'),
            'quest15' => $this->input->post('quizId15'),
            'quest16' => $this->input->post('quizId16'),
            'quest17' => $this->input->post('quizId17'),
            'quest18' => $this->input->post('quizId18'),
            'quest19' => $this->input->post('quizId19'),
            'quest20' => $this->input->post('quizId20'),
        );

        $this->load->model('quiz_model');

        $this->data['results'] = $this->quiz_model->getQuestions();

        $this->load->view('templates/header');
        $this->load->view('activity/result_display', $this->data);
        $this->load->view('templates/footer');
    }

I view page source the image and here are the arrays of the radio button, you can see it on the highlighted one the name

view page source

Here is my radio button, Do I need to make this an array? and how?

    <?php $i = 'A';
        foreach($ans_array AS $array_value): 
    ?>
    <?= $i; ?>.&nbsp; 

    <input type="radio" name="quizId<?= $question->id ?>" value="<?= $array_value ?>" required /> <?= $array_value ?> <br>

    <?php 
        $i++; 
        endforeach; 
    ?>

Upvotes: 0

Views: 55

Answers (1)

Serghei Leonenco
Serghei Leonenco

Reputation: 3507

Using array will be more efficient. You can do it this way:

<input type="radio" name="quizId[<?= $question->id ?>]" value="<?= $array_value ?>" required /> <?= $array_value ?> <br />

So after post you can go throughout loop and get your post results like:

foreach($this->input->post('quizId') as $val){
    //work with each value here
} 

Like i see you can update the controller this way:

$this->data['checks'] = $this->input->post('quizId');

You sent entire array to view. Now in the view file you need to specify the key value to be like quizId . $key.

Upvotes: 1

Related Questions