0

I'm making a quiz system and tried a different way of taking the variables and the values from MySQL.

$question = mysql_query("SELECT * FROM `questions`");
$stat = mysql_fetch_assoc($question);
$num = mysql_num_rows($question);
$questionid = 0;
for($i=0;$i<=$num;$i++)
{
$question = mysql_query("SELECT * FROM `questions` WHERE `id`='$i'");
$stat = mysql_fetch_assoc($question);
//if($stat['answer'] == null
echo $stat['question'] . '<br />';
echo '<input type="radio" name="' . $i .'" value="' . $questionid . '" />' . $stat['answer1'] . '<br />';
echo '<input type="radio" name="$i" value"$questionid" />' . $stat['answer2'] . '<br />';
echo '<input type="radio" name="$i" value"$questionid" />' . $stat['answer3'] . '<br />';
echo '<input type="radio" name="$i" value"$questionid" />' . $stat['answer4'] . '<br />';
$questionid++;
}

Now, I want to let the person choose the correct answer but when I try to select answer in question 1, and then in question 2, it won't let me probably because the radios have the same name - I don't know how to make it possible for the student to select one answer in each question and also how to get his choice (to store it in a variable and check if the answer is correct).

Michael Naidis
  • 116
  • 2
  • 4
  • 15

2 Answers2

0

You had problem with PHP variables and quote('/ "). Concatenate operators are not used proper.

name="' . $i .'" value="' . $questionid . '"

       /\ /\       /\         /\

See code:

for($i=0;$i<=$num;$i++)
{
$question = mysql_query("SELECT * FROM `questions` WHERE `id`='$i'");
$stat = mysql_fetch_assoc($question);
//if($stat['answer'] == null
echo $stat['question'] . '<br />';
echo '<input type="radio" name="' . $questionid .'" value="' . $stat['answer1'] . '" />' . $stat['answer1'] . '<br />';
echo '<input type="radio" name="' . $questionid .'" value="' . $stat['answer2'] . '" />' . $stat['answer2'] . '<br />';
echo '<input type="radio" name="' . $questionid .'" value="' . $stat['answer3'] . '" />' . $stat['answer3'] . '<br />';
echo '<input type="radio" name="' . $questionid .'" value="' . $stat['answer4'] . '" />' . $stat['answer4'] . '<br />';
$questionid++;
}
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
  • If the value of every button is `$questionid`, then you will not know what answer button was pressed... See my answer in http://stackoverflow.com/questions/14462380/how-can-i-see-if-the-users-choice-in-the-quiz-is-correct for a more complete solution – Floris Jan 22 '13 at 22:13
  • @Floris: I have only thought of quote. Yes you are right. Each radio button should have same question id name but different value i.e answer. – Somnath Muluk Jan 23 '13 at 05:06
0

I don't think that your way of doing is that good.I think best way to iterate your code is while() loop.Like

$all = mysql_query("SELECT * FROM 'questions'");
while($all_array=mysql_fetch_array($all))
{
$question = mysql_query("SELECT * FROM questions WHERE id='".$all_array['id']."'");
while($stat=mysql_fetch_array(question)){
echo $stat['question'] . '<br />';
echo '<input type="radio" name="' .$stat['id'].'" value="' . $stat['answer1'] . '" />' . $stat['answer1'] . '<br />';
echo '<input type="radio" name="' .$stat['id'].'" value="' . $stat['answer2'] . '" />' . $stat['answer2'] . '<br />';
echo '<input type="radio" name="' .$stat['id'].'" value="' . $stat['answer3'] . '" />' . $stat['answer3'] . '<br />';
echo '<input type="radio" name="' .$stat['id'].'" value="' . $stat['answer4'] . '" />' . $stat['answer4'] . '<br />';
}
}
vinu
  • 658
  • 10
  • 20