2016-02-16 2 views
0

Я сделал множественный выбор с 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] 

The radio button structure

+0

где question_id? –

+0

Измените свой формат с 'name =" question_id1 [] "' на 'name =" question_id [1] "'. Таким образом, вы можете сделать 'foreach ($ _ POST ['question_id'] как $ id => $ answer)' – Sean

+0

Я бы тогда просто попросил ваш массив ответов на '[id_question => id_answer]', т.е. '$ answers = [21 => 4]', поэтому с помощью 'foreach()' в моем последнем комментарии 'foreach ($ _ POST ['question_id'] как $ id => $ answer) {if ($ answer = = $ answers [$ id]) echo «У вас есть вопрос №». $ id. «Правильно!»; else echo «У вас есть вопрос №». $ id. «Неверно!»; } ' – Sean

ответ

0

Попробуйте с

<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"> 

И в серверной стороне

<?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); 
?> 

Предполагая, что пользователь может выбрать одну радиокнопку одиночный вопрос ....

+0

спасибо, поэтому много .. Я все еще запутался .. Какой $ key => $ ответ здесь? –

+0

$ ключ является id вопроса и $ answer является идентификатором ответа для этого вопроса .... –

+0

отметьте его как ответ, если он удовлетворит вас ... Ищите «0» с левой стороны и «отметьте отметку» под ним нажмите на этом .... –

0

Я разместил код в форме и с помощью кнопки отправки я отправил ее. Тогда на выходе я получил это массив ( [question_id1] => Массив ( [0] => 3 )

[question_id6] => Array 
    (
     [0] => 2 
    ) 

[question_id9] => Array 
    (
     [0] => 3 
    ) 

[submit] => submit 

) Таким образом, мы можем получить доступ с тем же именем. Я сделал это с помощью обычного php-кода. Поскольку массив будет сформирован, он будет таким же, как и для основного php, так и для codeigniter. Поэтому мы можем нормально обращаться к нему с помощью цикла.

+0

большое спасибо .. Мне нужно узнать этот код в любом случае .. –

Смежные вопросы