mardi 28 janvier 2020

Using an array in PHP to echo numbers in a random order

I have written a very simple couple of .php pages where a user chooses a times table to be tested on, and then answers questions.

At the moment they are just asked 12 random questions for that table, so could be asked the same question several times, and not be asked other questions. I am just using random number generation between 1 and 12 to achieve this.

I believe that I can enter the values 1-12 into an array, and call on them in a random order.

Does this sound like the correct way forward? If so, would someone be able to push me in the right direction? I am looking to learn from this as much as I am looking for a solution.

I have used arrays before from mysqli queries, but wouldn't say that I am 100% confident with them.

The $rand variable is used to determine which way round the question will be asked.

$question = 1;
$random = 0;
while ($question < 13)
{
      $random = rand(0,1);
      if ($random == 0)
      {
         $_SESSION['multiplier'.$question] = $_SESSION['table'];
         $_SESSION['multiplicand'.$question] = rand(1,12);
      }
      else
      {
         $_SESSION['multiplier'.$question] = rand(1,12);
         $_SESSION['multiplicand'.$question] = $_SESSION['table'];
      }
      $answer[$question] = $_SESSION['multiplier'.$question] * $_SESSION['multiplicand'.$question]; 
      $question++;
}



Aucun commentaire:

Enregistrer un commentaire