Tuesday, February 3, 2009

Facebook app creation: A look at the MySQL Quiz. part II

To those of you having a hard time finding the MySQLQuiz, please give the folks at Facebook a little more time to get it into their search. Sorry, I should have submitted a few days before the previous post to make sure that the MySQL Quiz was easy to find.

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: