0

How and where must the shuffle function be used in the given below code so that it displays random questions for every user

if(isset($_SESSION['stdname']))
{

    $result=executeQuery("select stdanswer,answered from studentquestion where stdid=".$_SESSION['stdid']." and testid=".$_SESSION['testid']." and qnid=".$_SESSION['qn'].";");
    $r1=mysql_fetch_array($result);

    $result=executeQuery("select * from question where testid=".$_SESSION['testid']." and qnid=".$_SESSION['qn'].";");
    $r=mysql_fetch_array($result); 
Mahidul Islam
  • 580
  • 11
  • 29
  • if you know the functions to use, how about the manual? –  Mar 29 '18 at 04:31
  • **The `mysql` PHP extension is dead** -- Stop using the [`mysql_*()` PHP functions](http://php.net/manual/en/function.mysql-connect.php) in new code. They are old, deprecated since PHP 5.5 and completely removed in PHP 7. Use [`mysqli`](http://php.net/manual/en/book.mysqli.php) or [`PDO_mysql`](http://php.net/manual/en/ref.pdo-mysql.php) instead. Read the answers to [this question](https://stackoverflow.com/q/12859942/4265352) to learn more about why and how. – axiac Mar 29 '18 at 06:26

2 Answers2

0

You can use RAND() function to get random results if you want only one question you can add LIMIT 1

$result=executeQuery("select * from question where testid=".$_SESSION['testid']." and qnid=".$_SESSION['qn']." ORDER BY RAND();");
$r=mysql_fetch_array($result);
Anshuman Jaiswal
  • 5,352
  • 1
  • 29
  • 46
0

If all you want to do is shuffle the same question set around from student to student you can use the shuffle() function like so.

$shuffledArray = shuffle($array); 

If you want to randomly select questions from your question set you can do one of the below options;

You can use the RAND() function in your query as @Anshuman Jaiswal suggested, but there is no guarantee that you will not get a duplicate question.

Here is a function that you can use for each student and guarantee that there will not be a duplicate question on a test. Plus, I added a parameter where you can select how many questions you want to pull from your question set.

   function getRandomQuestions($results, $numberOfQuestions){

  //Check to make sure you have a result.
  if(count($results) == 0){

    return FALSE;

  }

  //Check to make sure you have enough results for your desired amount of questions.
  if(count($results) < $numberOfQuestions || $numberOfQuestions == 0){

      $numberOfQuestions = count($results);
  }


  $randArray = array();
  for($i = 0; $i < $numberOfQuestions; $i++){

    $test = FALSE;

    Do{

      //Generate a basic random integer.
      $rand = rand(0, count($results) - 1);

      //Check to make sure the random number has not been used yet.
      if(!in_array($rand, $randArray)){

        $randArray[] = $rand;

        $questionSet[] = $results[$rand];

        $test = TRUE;

      }

    }while($test === FALSE);


  }

return $questionSet;

}

$results = array(Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10);

$numTestQuestions = 5;
$questions = getRandomQuestions($results, $numTestQuestions);

print_r($questions);

This will output something like:

Array ( [0] => Q2 [1] => Q6 [2] => Q3 [3] => Q7 [4] => Q5 ) 

Updated Example

Using the code you have from above, this is all you need to do. Add the function to the code, then run the queries, then call the function using the results from the queries.

function getRandomQuestions($results, $numberOfQuestions){ //<--The new function.

  //Check to make sure you have a result.
  if(count($results) == 0){

    return FALSE;

  }

  //Check to make sure you have enough results for your desired amount of questions.
  if(count($results) < $numberOfQuestions || $numberOfQuestions == 0){

      $numberOfQuestions = count($results);
  }


  $randArray = array();
  for($i = 0; $i < $numberOfQuestions; $i++){

    $test = FALSE;

    Do{

      //Generate a basic random integer.
      $rand = rand(0, count($results) - 1);

      //Check to make sure the random number has not been used yet.
      if(!in_array($rand, $randArray)){

        $randArray[] = $rand;

        $questionSet[] = $results[$rand];

        $test = TRUE;

      }

    }while($test === FALSE);


  }

return $questionSet;

}





if(isset($_SESSION['stdname']))
    {
    //Run your queries and get the results in r & r1.
    $result=executeQuery("select stdanswer,answered from studentquestion where stdid=".$_SESSION['stdid']." and testid=".$_SESSION['testid']." and qnid=".$_SESSION['qn'].";");
    $r1=mysql_fetch_array($result);

    $result=executeQuery("select * from question where testid=".$_SESSION['testid']." and qnid=".$_SESSION['qn'].";");
    $r=mysql_fetch_array($result); 

//Set the number of questions you want and then run the function.
$numTestQuestions = 5; //This will get 5 questions per student test.    
$questions = getRandomQuestions($r1, $numTestQuestions);//<--Call Function.
var_dump($questions); //<---Display your results.

$numTestQuestions = 0; //If you set to zero if will display all test questions per student test.
$questions = getRandomQuestions($r, $numTestQuestions); //<--Call function.
var_dump($questions); //<--Display your results.

}
Joseph_J
  • 3,654
  • 2
  • 13
  • 22
  • I will provide u whole code can u write full code with the above code suggested by u – Javed Ansari Apr 08 '18 at 04:40
  • I will provide u whole code can u write full code with the above code suggested by u – Javed Ansari Apr 08 '18 at 04:40
  • @Javed Ansari What are you still having problems with, what do you mean by full code? – Joseph_J Apr 08 '18 at 05:14
  • i m little bit confused how to use the above function in my code....just give me your email id i will send you the php file – Javed Ansari Apr 10 '18 at 03:37
  • @Javed Ansari I added more code as an example. This should give you a good idea on how to run the code. If you still have questions, ask another question on stackoverflow for additional help. Stackoverflow is not a code writing service. It's to help you with specific problems. If yo need someone to write code for you I would google code writing services. I hope this all helps. – Joseph_J Apr 10 '18 at 04:23