I’m in the process of an online trivia game and decided to share a method that I used to create the daily 10 questions or how many daily questions you want. It takes all the ids from the database table that contains the questions/answers and puts them into an array then it randomizes (shuffles) the array. Finally it puts it into a proper format, so it can be save into another database table that can be used as the controller.
Here’s the method:
protected function quizFormat() {
$this->idArray = $this->getTotalIds(); // Grab all the record id's from nasaTrivia table:
shuffle($this->idArray); // Shuffle the Array:
$this->total = floor(count($this->idArray) / 10); // Determine the number of rows for 10 questions per day:
$this->dailyDate = new DateTime("Now", new DateTimeZone('America/Detroit')); // Grab today's date:
/* Create a loop on how many rows of 10 questions per day will be */
for ($x = 0; $x < $this->total; $x++) {
$this->dailyTen = array_slice($this->idArray, ($x * 10), 10); // Slice off the first 10 questions for each day's set:
$this->quizFormat[$this->dailyDate->format("Y-m-d H:i:s")] = serialize($this->dailyTen); // Serialize the array:
$this->dailyDate->modify("+1 Day"); // Increment the day by 1:
}
}
Sure it leaves out a few questions if the trivia database table isn’t exactly divisible by 10 (or what have you), but eventually the question will be used within the given year. An when you read the data back into an array it will first have to be unserialized. Like I said I am still in the process of developing the trivia game, but I have this portion up and running and it works pretty good.
Here’s a link to the unfinished game : Space Trivia Game