سؤال

i have created a quiz format like QCM examen first i used radio button only and the problem of this radio button must have a name with the id of the question and the value must be the id of the answer example : the php code (using while)

<input type="radio" name="question_['.$question_id.']" value="'.$ans_id.'">

output

<input type="radio" name="question_1" value="5">
<input type="radio" name="question_1" value="6">
<input type="radio" name="question_2" value="10">
<input type="radio" name="question_2" value="11">

so what i want is posting this form i already used the var_dump for this but i've get an array format so i don't how to foreach it so please can someone help me ?

هل كانت مفيدة؟

المحلول

It should generate an array in your $_POST or $_GET superglobal just like any other PHP array, you can iterate over it like this:

foreach($_POST['question_'] as $question_id => $answer) {
    echo 'Answer for Question #' . $question_id . ' is: ' . $answer . PHP_EOL;
}

Edit: This example is following your first line of code - the second block of code is different and would not be produced by looping the first. You should definitely use the first style not the second, as arrays are much easier to loop over in this scenario than an arbitrary number of elements named like question_1, question_2 etc.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top