如何使用shuffle随机问题

时间:2018-03-29 04:19:01

标签: php mysql

在下面给出的代码中,如何以及在何处使用随机播放功能,以便为每个用户显示随机问题

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); 

2 个答案:

答案 0 :(得分:0)

如果您只想添加一个问题,可以使用RAND()功能获取随机结果LIMIT 1

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

答案 1 :(得分:0)

如果您想要做的就是将同一个问题从学生转移到学生,您可以使用shuffle()函数。

$shuffledArray = shuffle($array); 

如果您想从问题集中随机选择问题,可以执行以下选项之一;

您可以在@Anshuman Jaiswal建议的查询中使用RAND()函数,但不能保证您不会得到重复的问题。

这是一个可以为每个学生使用的功能,并保证测试中不会出现重复的问题。另外,我添加了一个参数,您可以在其中选择要从问题集中提取的问题数。

   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);

这将输出如下内容:

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

更新了示例

使用上面的代码,您只需要这样做。 将函数添加到代码中,然后运行查询,然后使用查询的结果调用函数。

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.

}