0

I am using CodeIgniter framework and getting values from html form.

Currently i am preparing array with fixed number of inputs, ques_X are inputs, But when ques_x numbers are increasing, i need to manually add each key pair value, like ques_11, ques_12...

$answers = array(
                'ques_1'    =>  $this->input->post('ques_1', TRUE),
                'ques_2'    =>  $this->input->post('ques_2', TRUE),
                'ques_3'    =>  $this->input->post('ques_3', TRUE),
                'ques_4'    =>  $this->input->post('ques_4', TRUE),
                'ques_5'    =>  $this->input->post('ques_5', TRUE),
                'ques_6'    =>  $this->input->post('ques_6', TRUE),
                'ques_7'    =>  $this->input->post('ques_7', TRUE),
                'ques_8'    =>  $this->input->post('ques_8', TRUE),
                'ques_9'    =>  $this->input->post('ques_9', TRUE),
                'ques_10'   =>  $this->input->post('ques_10', TRUE))

Is it possible to create something with for or foreach loops?

5
  • You want to add input tag again and again into form? Commented Jul 22, 2017 at 8:57
  • @GyandeepSharma, yes but each input are different, but sequential. example currently there are 10 inputs, but if there are 50 inputs, i need to write it manually, so need some short code with some loops Commented Jul 22, 2017 at 8:58
  • see you can do it easily by javascript Commented Jul 22, 2017 at 9:00
  • example currently there are 10 inputs, but if there are 50 inputs so you need to take loop on Daniel Alexandrov Commented Jul 22, 2017 at 9:08
  • I didn't understand the question... so deleted my answer... Sorry for interruption. Commented Jul 22, 2017 at 9:18

3 Answers 3

2

You can create a for loop like this:

$answers = array();
for($i = 1; $i <= 10; ++$i){
    $answers['ques_'.$i] = $this->input->post('ques_'.$i, TRUE);
}
Sign up to request clarification or add additional context in comments.

Comments

2

Use an array instead of ques_n naming for the inputs.

<input type="text" name="questions[]" value="value1"/>
<input type="text" name="questions[]" value="value2"/>
<input type="text" name="questions[]" value="value3"/>

and then you can just get the data like this:

$answers = $this->input->post('questions', TRUE);

Edit based on the comment of rjcod:

You can also generate the inputs like this, and still use the same php code:

<input type="text" name="questions[0]" value="value1"/>
<input type="text" name="questions[1]" value="value2"/>
<input type="text" name="questions[2]" value="value3"/>
<!-- This 3 radio buttons are grouped, you can also wrap them in
fieldset if you want -->
<input type="radio" name="questions[3]" value="1"/>
<input type="radio" name="questions[3]" value="2"/>
<input type="radio" name="questions[3]" value="3"/>

1 Comment

this will be useful, but i am pulling questions from DB and looping questions to show in HTML and some questions are with 4 options radio buttons,
0

Your can try this:

 $data = array();
 $post_length = sizeof($_POST);

   for($i = 1; $i <= $post_length; ++$i){
        $data ['ques_'.$i] = $this->input->post('ques_'.$i, TRUE);
    }

Or you can use this which is fully dynamic.

$data = array();
foreach($_POST as $key=>$value)
    {
      $data [$key] = $this->input->post($key, TRUE);
      //or 
      //$data [$key] = $value;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.