0
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?

  • 1
    Note that your loop condition should be `$y < $rd`. You should also show the definition of `$question` (unless it's initialized by PDO or somesuch, and then just make some sample data we could work with). – Rogue Jul 13 '22 at 17:13
  • MYSQL is perfectly capable of randomizing and limiting the results returned. – mickmackusa Jul 13 '22 at 23:09

1 Answers1

1

If you just want 20 questions from a list of questions, then you can try this method.

<?php
$question = array(
    "Question 1",
    "Question 2",
    "Question 3",
    "Question 4",
    "Question 5",
    "Question 6",
    "Question 7",
    "Question 8",
    "Question 9",
    "Question 10",
    "Question 11",
    "Question 12",
    "Question 13",
    "Question 14",
    "Question 15",
    "Question 16",
    "Question 17",
    "Question 18",
    "Question 19",
    "Question 20",
    "Question 21",
    "Question 22",
    "Question 23",
    "Question 24",
    "Question 25"
);
shuffle($question);
$rand_questions = array_slice($question,0,20);
foreach($rand_questions as $question){
    echo $question."<br>";
}
  • Although it changed the method. But it did the task. Thankyou. Just checked it. It solved the problem – Suprin Aziz Talpur Jul 13 '22 at 17:29
  • Please help to combat redundant content by flagging duplicate questions instead of answering them. In 2022, all new, basic questions have been asked and answered 5 or more times. When you see a basic question asked, you can be fairly certain that it should be closed as a duplicate. After voting to close a duplicate, if you can add unique, valuable insights to the older page, please post an answer there. This is how we make Stack Overflow a better resource for researchers. – mickmackusa Jul 13 '22 at 22:47
  • @mickmackusa I didn't found the right answer in any similar post. The question is unique in the sense that no one asked or told that rand doesn't work in the loop. As you can test the code in the question. The answer remain `$question[$random_keys[0]]` no matter what the value of `$y` is. – Suprin Aziz Talpur Jul 14 '22 at 13:51
  • The accepted answer on the dupe target literally gives the same advice as this answer. When another page gives the same/appropriate resolving advice, the new page should be closed with the old page. The questions do not need to be identical, they only need to share resolving advice. This page is now called a "sign post" -- representing a different way of asking for the same resolution. – mickmackusa Jul 14 '22 at 17:40