» Site Navigation | | | » Advertisement | | | » Recent Threads | | | | | | | Froze-owned 11-03-2008 08:32 PM Today 07:27 PM 15 Replies, 314 Views | | | |  |  | [PHP] Quiz script using MySQL. |  |
12-09-2006, 08:28 PM
|
#1 (permalink)
| Underground
Join Date: Sep 2006 Location: The o great land of Nooooooreeeway Age: 20 Posts: 658
GPoints: 38 Rep Power: 7 | [PHP] Quiz script using MySQL. PHP Code: <?
/*
Quiz script
Gommle
gommle@gmail.com
*/
/* MySQL table:
----------------------
CREATE TABLE `questions` (
`id` int(11) NOT NULL auto_increment,
`category` varchar(255) NOT NULL,
`question` varchar(255) NOT NULL,
`option1` varchar(255) NOT NULL,
`option2` varchar(255) NOT NULL,
`option3` varchar(255) NOT NULL,
`option4` varchar(255) NOT NULL,
`option5` varchar(255) NOT NULL,
`answer` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=26 ;
--
-- Dumping data for table `questions`
--
-- Example questions
INSERT INTO `questions` VALUES (11, 'Ubrukelig', 'Hvor lenge er det lengste en kylling har flydd?', '6 sekund', '13 sekund', '30 sekund', 'Et minutt', 'To minutter', '13 sekund');
INSERT INTO `questions` VALUES (2, 'Ubrukelig', 'Hvor mye sitter et gjennomsnittlig menneske på do i løpet av hele livet sitt?', '1 måned', '1 år', '1 uke', '8 måneder', '8 uker', '8 måneder');
INSERT INTO `questions` VALUES (3, 'Ubrukelig', 'Måneder som begynner på denne dagen har alltid en "Fredag 13".', 'Torsdag', 'Fredag', 'Lørdag', 'Søndag', 'Mandag', 'Søndag');
INSERT INTO `questions` VALUES (4, 'Ubrukelig', 'Hvis du fjerner fargestoffene fra Coca-Cola blir den denne fargen:', 'Svart', 'Rød', 'Grønn', 'Blå', 'Rosa', 'Grønn');
INSERT INTO `questions` VALUES (5, 'Ubrukelig', 'Hvor mange øyelokk har en kamel på hvert øye?', '1', '2', '3', '4', '5', '3');
*/
?>
<?php
// Enable sessions to save quiz progress
session_start();
// Enable output buffering so we can send headers all ze time.
//ob_start();
// Connect to MySQLi
$link = mysqli_connect('localhost', 'root', '');
mysqli_select_db($link, 'quiz');
// Show a string in a nice format.
function debug($str, $name) {
echo '<span style="width:100%; padding:5px; border:1px solid orange; background-color:#c03; color:#fff;">';
echo '<b>'.$name.':</b> '.$str;
echo '</span>';
}
// Returns questions
function get_questions($cat='') {
// Make current link available
global $link;
// Add category to SQL, otherwise select everything
if($cat) {
$cat = " WHERE category = '$cat'";
}
// Query
$sql = 'SELECT * FROM questions'.$cat.' ORDER BY RAND()';
// Get results
$result = mysqli_query($link, $sql) or die(mysqli_error($link));
// Put results into an array
while($row = mysqli_fetch_array($result)) {
$return[] = $row;
}
// Return questions
return $return;
} # Function get_questions
if($_GET['do']=='restart') {
// Restart quiz by deleting the session
$_SESSION = array();
session_destroy();
}
// If answered Q
if($_POST['answer']) {
if($_POST['answer'] == $_SESSION['answer']) {
$_SESSION['correct']++;
$_SESSION['stats'][$_SESSION['curr_q']]['correct'] = TRUE;
} else {
$_SESSION[$_SESSION['stats']['curr_q']]['correct'] = FALSE;
}
$_SESSION['stats'][$_SESSION['curr_q']]['question'] = $_SESSION['question'];
$_SESSION['stats'][$_SESSION['curr_q']]['canswer'] = $_SESSION['answer'];
$_SESSION['stats'][$_SESSION['curr_q']]['answer'] = $_POST['answer'];
$_SESSION['curr_q']++;
}
// If start quiz button has been pressed
if($_POST['startquiz']) {
// Set current question to first Q
$_SESSION['curr_q'] = 0;
$_SESSION['correct'] = 0;
// 3, 2, 1, BOOM
$_SESSION['quiz'] = 'gfdgfdgfd';
// Reload page
//header('Location:quiz.php');
$_SESSION['questions'] = get_questions();
}
if($_SESSION['curr_q']+1>count($_SESSION['questions']) && $_SESSION['curr_q']) {
$finished = "Done! You had {$_SESSION['correct']} correct answers, of ".count($_SESSION['questions'])." possible.
<br /> <h2>Dine svar:</h2><hr />";
// Output answers to the questions
foreach($_SESSION['stats'] as $key => $value) {
$finished .= '<b>'.$value['question'].'</b><br />';
if($value['correct']) {
$finished .= 'Correct: <i>'.$value['canswer'].'</i>';
} else {
$finished .= 'Wrong: '.$value['answer'].'<br />';
$finished .= 'Correct answer: <i>'.$value['canswer'].'</i>';
}
$finished .= '<hr />';
}
$_SESSION = array();
session_destroy;
}
// If quiz is started
if($_SESSION['quiz']) {
// Save questions
if(!$_SESSION['questions']) {
// Shuffle questions
//shuffle($_SESSION['questions']);
// Set current question
$_SESSION['curr_q'] = 1;
// Reload page
//header('Location:quiz.php');
}
// If current question is set
if($_SESSION['curr_q'] || $_SESSION['curr_q'] === 0) {
// Get current question
$_SESSION['question'] = $_SESSION['questions'][$_SESSION['curr_q']]['question'];
$_SESSION['answer'] = $_SESSION['questions'][$_SESSION['curr_q']]['answer'];
$_SESSION['option1'] = $_SESSION['questions'][$_SESSION['curr_q']]['option1'];
$_SESSION['option2'] = $_SESSION['questions'][$_SESSION['curr_q']]['option2'];
$_SESSION['option3'] = $_SESSION['questions'][$_SESSION['curr_q']]['option3'];
$_SESSION['option4'] = $_SESSION['questions'][$_SESSION['curr_q']]['option4'];
$_SESSION['option5'] = $_SESSION['questions'][$_SESSION['curr_q']]['option5'];
$_SESSION['quiz'] = 'started';
}
}
$questionno = $_SESSION['questions'];
?>
<!--Start output here-->
<html>
<head>
<title>
Quiz
</title>
<style type="text/css">
fieldset {
background-color:#FFEBE6;
color:#991F00;
margin-top:20px;
}
fieldset#main {
width:600px;
position:relative;
left:50%;
margin-left:-300px;
text-align:left;
background-color:#FFEBE6;
color:#991F00;
}
body {
background-color:#990000;
}
legend {
color:#FF8080;
}
input#submit {
width:100%;
background-color:#FFEBE6;
font-size:16px;
color:#991F00;
border-top:1px solid #FFBFBF;
border-left:1px solid #FFBFBF;
border-right:1px solid #FF8080;
border-bottom:1px solid #FF8080;
margin-top:10px;
font-weight:bold;
}
</style>
</head>
<body>
<fieldset id="main">
<h1>Quiz</h1>
<?
// If quiz hasn't started
if(!$_SESSION['quiz']) {
?>
<p><?=$finished;?></p>
<p>Click the button to start the quiz</p>
<fieldset>
<form action="?" method="post">
<input type="hidden" name="startquiz" value="startquiz" />
<input name="submit" type="submit" value="Start quiz" />
</form>
</fieldset>
<?
// If quiz has started
} else {
?>
<fieldset>
<legend>Question #<? echo $_SESSION['curr_q']+1; echo ' of '; echo count($questionno);?></legend>
<b><?=$_SESSION['question'];?></b>
<fieldset>
<form action="?" method="post">
<input type="radio" name="answer" value="<?=$_SESSION['option1'];?>" /><?=$_SESSION['option1'];?><br />
<input type="radio" name="answer" value="<?=$_SESSION['option2'];?>" /><?=$_SESSION['option2'];?><br />
<input type="radio" name="answer" value="<?=$_SESSION['option3'];?>" /><?=$_SESSION['option3'];?><br />
<input type="radio" name="answer" value="<?=$_SESSION['option4'];?>" /><?=$_SESSION['option4'];?><br />
<input type="radio" name="answer" value="<?=$_SESSION['option5'];?>" /><?=$_SESSION['option5'];?><br />
</fieldset>
<input type="submit" id="submit" value="Answer question" />
</form>
</fieldset>
<?
// End quiz started
}
?>
<!--Page footer and end tags-->
</fieldset>
</body>
</html> A stat function can easily be added by putting it inside the if/else which checks if the answer is correct. | |
| |  |  | Re: [PHP] Quiz script using MySQL. |  |
12-10-2006, 04:47 AM
|
#2 (permalink)
| | | Re: [PHP] Quiz script using MySQL. That's smooth. Did you start that from scratch? A friend and I developed a Poll generator for MySpace etc. so people just set their questions and answers then code a HTML code snippet which linked up to our script. | |
| |  | Re: [PHP] Quiz script using MySQL. |  |
12-10-2006, 05:11 AM
|
#3 (permalink)
| Underground
Join Date: Sep 2006 Location: The o great land of Nooooooreeeway Age: 20 Posts: 658
GPoints: 38 Rep Power: 7 | Re: [PHP] Quiz script using MySQL. I could make something which generates tables dynamically, and maybe some stats. This is meant as a quiz though, not a poll. | |
| |  | Re: [PHP] Quiz script using MySQL. |  |
12-10-2006, 05:21 AM
|
#4 (permalink)
| | | Re: [PHP] Quiz script using MySQL. Yup. I was just saying  | |
| |  | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | |