this code outputs different question from db until while loop expires with MCQ options of type radio:
$sql = "SELECT * FROM questions WHERE `type` IN
('".implode("','",$fin_element)."')";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br>";
echo "Q:" . $row["question_name"]. "<br>";
echo "<input type='radio' name='question1' value='answer1.1'/ >
<code>".$row["answer1"]."</code>". "<br>";
echo "<input type='radio' name='question1' value='answer1.2'/>
<code>".$row["answer2"]."</code>". "<br>";
echo "<input type='radio' name='question1' value='answer1.3'/>
<code>".$row["answer3"]."</code>". "<br>";
echo "<input type='radio' name='question1' value='answer1.4'/>
<code>".$row["answer4"]."</code>". "<br>";
}
} else {
echo "0 results";
}
but here every options of every question have the same name attribute of question1 only , but i want that for the next question name attribute should change to question2 and so on. Please help
If you have an identifier in your table question
, you could use it.
But you could just use a variable to increment question number :
$index = 1 ;
while($row = $result->fetch_assoc()) {
echo "<br>";
echo "Q:" . $row["question_name"]. "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.1'/ >
<code>".$row["answer1"]."</code>". "<br>";
//...
$index++;
}