Create a Daily Random Question Method (Function)

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

why would you serialize the array?

Dont store the questions more than once.

Table 2 should contain the ID’s of the questions to ask, paired with a date.

Why are you setting object properties for all of these variables that you’re only going to use inside this function?

protected function quizFormat() {
    $idArray = $this->getTotalIds(); // Grab all the record id's from nasaTrivia table:
    shuffle($idArray); // Shuffle the Array:
    $dailyDate = new DateTime("today", new DateTimeZone('America/Detroit')); // Grab today's date:
   do {
        $this->quizFormat[$dailyDate->getTimestamp()] = array_splice($idArray, 0, 10); //Here's one variable we'll need outside, since this doesnt return anything. For... whatever reason.
        $dailyDate->modify("+1 Day"); // Increment the day by 1:
    } while (count($idArray) >= 10) 
    
}

Why not use array_rand() to grab as many random questions that are needed?

I believe the idea is to guarantee you use all of the array exactly once during the time span, to avoid repeating the question on consecutive days or leaving questions out entirely. Repeated uses of array_rand would not respect that, and if you’re going to consume the entire array, shuffling it once should be faster than repeated random selections from it.

Also array_rand pulls the keys, rather than the values, meaning you’d need an extra step to go back through and yank the values you rand’ed.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.