xKlydez
xKlydez

Reputation: 39

Getting values of radio buttons generated in for loop

I'm currently running on codeigniter framework and running into some trouble. I am retrieving a list of questions and a list of their possible answers (something like a questionnaire) from the database and loading it out to participants to attempt.

Due to the the for loop I'm using, I'm unable to change the name or id value of individual questions. I sort of overcome this by appending the questionID to the name/id.

The trouble comes here when i post this over to my controller. How am I able to differentiate it when I need to insert it the results back into the database?

 <?php foreach($dbresult->result() as $row) {?>

            <p><?php echo $row->Question ;?></p>
            <?php if ($row->QuestionType == "MCQ"){ ?>

            <ul>
                <li><b>A: </b><?php echo $row->SelectionA; ?></li>
                <li><b>B: </b><?php echo $row->SelectionB; ?></li>
                <li><b>C: </b><?php echo $row->SelectionC; ?></li>
                <li><b>D: </b><?php echo $row->SelectionD; ?></li>
            </ul>

           Select one of the following options:<br>
            <input type="radio" name="<?php echo $row->QuestionID;?>.Ans" required value="A">A<br>
            <input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="B">B<br>
            <input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="C">C<br>
            <input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="D">D<br>
            <?php } elseif($row->QuestionType== "truefalse") {?>

           Select one of the following options:<br>
                <input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="true">True<br>
                <input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="false">False<br>
            <?php } else {?>
                <input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="ok">OK<br>
                <input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="agree">Agree<br>
            <?php }?>
<?php } ?>

The above is the way I have printed the questionnaire out, any possible solutions for this?

Upvotes: 1

Views: 979

Answers (1)

Kevin
Kevin

Reputation: 41885

Thats hard to manage having a name attribute like that.

Use a grouping name attribute:

name="answers[<?php echo $row->QuestionID;?>]"

Simple example:

<?php foreach($dbresult->result() as $row) {?>
    <p><?php echo $row->Question ;?></p>

    <?php if ($row->QuestionType == "MCQ"){ ?>

        <ul>
            <li><b>A: </b><?php echo $row->SelectionA; ?></li>
            <li><b>B: </b><?php echo $row->SelectionB; ?></li>
            <li><b>C: </b><?php echo $row->SelectionC; ?></li>
            <li><b>D: </b><?php echo $row->SelectionD; ?></li>
        </ul>

       Select one of the following options:<br>
        <input type="radio" name="answers[<?php echo $row->QuestionID;?>]" required value="A">A<br>
        <input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="B">B<br>
        <input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="C">C<br>
        <input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="D">D<br>

    <?php } elseif($row->QuestionType== "truefalse") {?>

       Select one of the following options:<br>
            <input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="true">True<br>
            <input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="false">False<br>

    <?php } else {?>

        <input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="ok">OK<br>
        <input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="agree">Agree<br>

    <?php }?>
<?php } ?>

So that in PHP, you could just call it by its name:

public function controller_name()
{
    $answers = $this->input->post('answers');
    foreach($answers as $question_id => $answer) {

    }
}

Upvotes: 1

Related Questions