mercredi 13 juillet 2022

array_rand returing only the value of first array in for loop

include '../db.php';
$sql = "SELECT question, rightans, ans2, ans3, ans4, id, subject, author FROM others WHERE subject = '".$sub."'";

$result = $con->query($sql);
$question=array();
if ($result->num_rows > 0) {
$x=0;
while($row = $result->fetch_assoc()) {
if ($row['question']!=""){
$question[$x]=$row['question'];
$x=$x+1;
}


}}
$random_keys=array_rand($question,20);
$rd=count($random_keys);
$con->close();
$z=array();
for ($y = 0; $y <= $rd; $y++) {
    

echo $question[$random_keys[$y]];
}

Technically I should be receiving 20 random questions. But i am only getting the first one.

I had checked that $y value, it is increasing and $question[$random_keys[1]] and $question[$random_keys[2]] are also returing the expected values.

But for some reason $question[$random_keys[$y]] is functioning like $question[$random_keys[0]] only? what went wrong?




Aucun commentaire:

Enregistrer un commentaire