Я сделал множественный выбор с PHP (Codeigniter) и MySQL. У меня возникли проблемы при попытке получить значение из ответа (с динамическим именем) для каждого вопроса. Вот код для радио-кнопки:Множество вариантов PHP
<input type="radio" name="question_id (according to id of question)" value="answer_id">
Так что, если у меня есть 3 случайные вопросы, структура будет
<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">
Как получить ответ, который имеет отношение к вопросу? Например поместить его в массив, как:
array p = ['id_question' => 21, 'id_answer'=4]
где question_id? –
Измените свой формат с 'name =" question_id1 [] "' на 'name =" question_id [1] "'. Таким образом, вы можете сделать 'foreach ($ _ POST ['question_id'] как $ id => $ answer)' – Sean
Я бы тогда просто попросил ваш массив ответов на '[id_question => id_answer]', т.е. '$ answers = [21 => 4]', поэтому с помощью 'foreach()' в моем последнем комментарии 'foreach ($ _ POST ['question_id'] как $ id => $ answer) {if ($ answer = = $ answers [$ id]) echo «У вас есть вопрос №». $ id. «Правильно!»; else echo «У вас есть вопрос №». $ id. «Неверно!»; } ' – Sean