mysql - Multiple Choices PHP

I made a multiple choice with PHP (Codeigniter) and MySQL. I Got trouble when trying to retrieve value from the answer (with dynamic name) for each question. Here is the code for radio button :

<input type="radio" name="question_id (according to id of question)" value="answer_id">

So, if I have 3 random questions, the structure will be

<p>Question number 1 goes here</p>
<input type="radio" name="question_id1[]" value="1">
<input type="radio" name="question_id1[]" value="2">
<input type="radio" name="question_id1[]" value="3">
<input type="radio" name="question_id1[]" value="4">
<input type="radio" name="question_id1[]" value="5">


<p>Question number 6 goes here</p>
<input type="radio" name="question_id6[]" value="1">
<input type="radio" name="question_id6[]" value="2">
<input type="radio" name="question_id6[]" value="3">
<input type="radio" name="question_id6[]" value="4">
<input type="radio" name="question_id6[]" value="5">


<p>Question number 9 goes here</p>
<input type="radio" name="question_id9[]" value="1">
<input type="radio" name="question_id9[]" value="2">
<input type="radio" name="question_id9[]" value="3">
<input type="radio" name="question_id9[]" value="4">
<input type="radio" name="question_id9[]" value="5">

How to retrieve the answer that relates to the question ? For example put it in Array like :

array p = ['id_question' => 21, 'id_answer'=4]

The radio button structure

2 Answers

  1. Patrick- Reply

    2019-11-15

    Try with

    <p>Question number 9 goes here</p>
    <input type="radio" name="question_id[9]" value="1">
    <input type="radio" name="question_id[9]" value="2">
    <input type="radio" name="question_id[9]" value="3">
    <input type="radio" name="question_id[9]" value="4">
    <input type="radio" name="question_id[9]" value="5">
    <p>Question number 6 goes here</p>
    <input type="radio" name="question_id[6]" value="1">
    <input type="radio" name="question_id[6]" value="2">
    <input type="radio" name="question_id[6]" value="3">
    <input type="radio" name="question_id[6]" value="4">
    <input type="radio" name="question_id[6]" value="5">
    

    And in server side

    <?php
    //assuming form method is post
    $questions_array = array();
    foreach($_POST[question_id] as $key=>$answer)
    {
      $questions_array[] = array('id_question' => $key, 'id_answer'= $answer); 
    }
    print_r($questions_array);
    ?>
    

    Assuming that user can select a single radio button for single question....

  2. Peter- Reply

    2019-11-15

    I have placed the code in the form and with a submit button i submitted it. Then the output i got it is Array ( [question_id1] => Array ( [0] => 3 )

    [question_id6] => Array
        (
            [0] => 2
        )
    
    [question_id9] => Array
        (
            [0] => 3
        )
    
    [submit] => submit
    

    ) So we can access with the same name. I did it with normal php code. Because the array will that is formed will be same in both core php as well as codeigniter. So we can normally access it with for loop.

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>