In the evaluation there are 4 alternative options but it does not work randomly.
'alternatives' => [$row["a1"], $row["a2"],$row["a3"],$row["a4"]],
which is equal to:
'alternatives' => [1, 2, 3, 4],
This information is obtained by the following columns
a1 a2 a3 a4
1 2 3 4
2a1b 3c2a y4x1 2v7a
And its visualization taking into account the first row is:
A) 1
B) 2
C) 3
D) 4
But how can I receive that data randomly for example:
'alternatives' => [4, 2, 1, 3],
So then when repeating an evaluation, the correct answers are not in the same specific letter.
A) 4
B) 2
C) 1
D) 3
This is my code:
$sql = "SELECT * FROM quiz WHERE enabled=yes";
$result = $con->query($sql);
while($row = mysqli_fetch_assoc($result)) {
$answers[] = [
'name' => 'respuesta_'.$row["id_question"],
'question' => $row["question"],
'alternatives' => [$row["a1"], $row["a2"],$row["a3"],$row["a4"]],
'answer' => $row["answer"]
];
}
$questions = [];
foreach($answers as $index => $answer) {
$questions[]=$answer;
$questions[$index]['answer']=null;
}
Example of data to obtain
[
'name' => 'respuesta_1',
'question' => 'Cuanto es 2+2?',
'alternatives' => [1, 2, 3, 4],
'answer' => 4,
]
Data table structure
id_question question a1 a2 a3 a4 answer category
.........
What you are looking for is the function
shuffle()
http://php.net/manual/es/function.shuffle.php