On this system a user can create a quiz of any number of questions, that info saves to a database. I'm using the below to output the quiz to the users. This outputs fine and you can only choose one of each answer. Each question has 3 inputs with the same post name, q1 has ans1 ans1 ans1, q2 has ans2ans2 ans2 etc... thats where the ".$x." comes in.
$x = 1;
while($row = mysql_fetch_row($quiz)) {
echo "Question ".$x.": ";
echo "<a><b> $row['question'] </b></a>";
echo "<input type='radio' name='ans".$x." /> ".$row['ans1']."<br />";
echo "<input type='radio' name='ans".$x." /> ".$row['ans2']."<br />";
echo "<input type='radio' name='ans".$x." /> ".$row['ans3']."<br />";
$x = $x + 1;
}
The problem is in the next php page. I'm trying to loop through all posts and where the post matches the correct ans then result = result + 1. I need a loop that does something like this:
$x = 1;
while($row = mysql_fetch_row($quiz)) {
if ($_POST[ans[$x]]=='$row['correct']' { $result = $result + 1;
}
$x = $x + 1;
}
is there a way i can use a variable in that $_POST value to say ans1 ans2 for each loop?
$x = 1;
while($row = mysql_fetch_row($quiz)) {
if ($_POST['ans'.$x] == $row['correct']) { $result = $result + 1; }
$x = $x + 1;
}
Hope this will solve your problem
Try something like this
$x = 1;
while($row = mysql_fetch_row($quiz)) {
echo "Question ".$x.": ";
echo "<a><b> $row['question'] </b></a>";
echo "<input type='radio' name='ans[".$row['ansId']."][".$x."] ' /> ".$row['ans1']."<br />";
echo "<input type='radio' name='ans[".$row['ansId']."][".$x."] ' /> ".$row['ans2']."<br />";
echo "<input type='radio' name='ans[".$row['ansId']."][".$x."] ' /> ".$row['ans3']."<br />";
$x = $x + 1;
}
Your front script:
$x = 1;
while($row = mysql_fetch_row($quiz)) {
if ($_POST['ans'][$row['ansId']][$x]=='$row['correct']' {
$result = $result + 1;
}
$x = $x + 1;
}