We wanted something fun for the visitors at our course in the open day. So I recieved the task to program a quiz. The website is still online at opendag.icthv.nl (Update: Database no longer seems to be working).
I was busy quite some time with this. And unlike my Forum from the same period this did finish. There is no back-end though, everything was added by using the database.
I learned:
- More about Object Oriented Programming (OOP)
- Creating a correct database design
- About the header function
- More about sessions
- Using the FaceBook API
To save everything we needed I used a mysql database.
To not cause cheating between people telling what questions they will recieve, there were different ‘sets’ of questions. These where based on times that were saved in the database. This is how I chose a set:
public function kiesSet(){ global $db; $query = $db->prepare("SELECT * FROM opendag.set WHERE start <= NOW() AND eind > NOW()"); $query->execute(); $set = $query->fetchAll(); if (!isset($set[0][0])){ echo 'U kunt dit nu niet doen, probeer later nog eens. '; return 0; } $this->set = $set[0]['id']; return $set[0]['id']; }
The application even checked if the user already existed, so every facebook account can do it only once. If you loged in again it will return to the last question you answered, if you already answered all of them it will show you you’re done, and how many questions you got right.
public function checkExists(){ global $db; $query = $db->prepare("SELECT COUNT(*) FROM gebruikers WHERE email='{$this->email}'"); if($query) { $query->execute(); if($query->fetchColumn()){ $query = $db->prepare("SELECT id FROM gebruikers WHERE email='{$this->email}'"); if($query) { $query->execute(); $row = $query->fetch(); return $row['id']; } else { $error = $db->errorInfo(); echo $error[2]; exit; } } else { return 0; } } else { $error = $db->errorInfo(); echo $error[2]; return false; } }
2 major parts of the quiz are the code of getting the question (the function ‘doevraag’) and saving the question (the function ‘opslaan’)
class vraag { public function opslaan ($set, $pp, $antwoord){ global $db; $ID = $set * 6 + $pp - 6; $query = $db->prepare("SELECT * FROM antwoorden WHERE vraag_id='$ID' AND gebruiker_id='".$_SESSION['id']."'"); $query->execute() or die("help"); $aantal = $query->rowCount(); if (empty($antwoord)){ $antwoord = "[ leeg ]"; } if ($aantal == 0){ $query = $db->prepare("INSERT INTO antwoorden (gebruiker_id, vraag_id, antwoord, tijd) VALUES ('".$_SESSION['id']."', '$ID', '$antwoord', NOW())"); $query->execute() or die ('Antwoord invoegen mislukt. '); } } public function doeVraag($pagina, $set){ global $db; $query = $db->prepare("SELECT * FROM vragen WHERE set_id=$set AND pagina=$pagina"); $query->execute() or die('Er zijn geen vragen voor dit tijdstip. '); $fetchy = $query->fetchAll(); $nextPP = $pagina + 1; echo nl2br($fetchy[0]['vraag']).'<br />'; ?> <form action="index.php" method="post"> <input type="hidden" value="<?=$nextPP?>" name="pp" /> <input type="text" name="antwoord" /> <input type="submit" value="Volgende" name="submit" /> </form><?php } }