In the previous post, a very simple Facebook application was shown.
The next part of the MySQL Quiz application was to randomly pick unique questions from the database of quiz questions.
The first step was to get the number of questions in a rapidly expanding database. I will keep the PHP fairly simple :
$result = mysql_query("SELECT count(*) FROM fb.quiz");
$row = mysql_fetch_array($result);
$randomMax = $row[0];
Next was creating an array to hold the randomly picked, unique id numbers. The quiz uses $nbrQuestions (currently five) questions.
$randomNbrs = array();
$randomCounter = 0;
while ($randomCounter < $NbrQuestions) {
$newRandom = rand(1,$randomMax);
if (!in_array($newRandom,$randomNbrs)) {
$randomCounter++;
array_push($randomNbrs,$newRandom);
}
}
Displaying the the questions from the unique id number should be routine stuff and is omitted. The rest of the app was fairly straight forward PHP.
There is a lot of good stuff in the Facebook documentation and I hope to be able to dive in further.
No comments:
Post a Comment