Go Back   Gaming Gutter > Non-Gaming > Programming > Source Code


Source Code - Have a source code/project files you want to post? Do so here.

» Site Navigation
» Home
» FAQ
» Log in
User Name:

Password:

Not a member yet?
Register Now!
» Advertisement
» Recent Threads
Go to first new post Heather wants to kill a...
Today 06:49 PM
Last post by Fewmitz
Today 07:31 PM
13 Replies, 47 Views
Go to first new post The ctrl v button game!
04-07-2007 02:50 PM
Last post by Arthritis
Today 07:30 PM
765 Replies, 6,540 Views
Go to first new post On a serious note
Today 07:28 PM
by Bex
Last post by Bex
Today 07:29 PM
1 Replies, 2 Views
Go to first new post Froze-owned
11-03-2008 08:32 PM
by Reemer
Last post by checkone
Today 07:27 PM
15 Replies, 314 Views
Go to first new post forgot my birthday i put...
10-30-2008 09:27 AM
by dookie
Last post by checkone
Today 07:25 PM
22 Replies, 463 Views
Reply
 
LinkBack Thread Tools Display Modes

 [PHP] Quiz script using MySQL.
Old 12-09-2006, 08:28 PM   #1 (permalink)
Underground

Male gommle is offline
 
gommle's Avatar
 
Join Date: Sep 2006
Location: The o great land of Nooooooreeeway
Age: 20
Posts: 658
GPoints: 38
iTrader: 0 / 0%
gommle Is Recognizable
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.
  Reply With Quote

 Re: [PHP] Quiz script using MySQL.
Old 12-10-2006, 04:47 AM   #2 (permalink)
emit
Guest

 
Posts: n/a
GPoints: 0 [Check]
iTrader: / %
Rep Power:
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.
  Reply With Quote

 Re: [PHP] Quiz script using MySQL.
Old 12-10-2006, 05:11 AM   #3 (permalink)
Underground

Male gommle is offline
 
gommle's Avatar
 
Join Date: Sep 2006
Location: The o great land of Nooooooreeeway
Age: 20
Posts: 658
GPoints: 38
iTrader: 0 / 0%
gommle Is Recognizable
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.
  Reply With Quote

 Re: [PHP] Quiz script using MySQL.
Old 12-10-2006, 05:21 AM   #4 (permalink)
emit
Guest

 
Posts: n/a
GPoints: 0 [Check]
iTrader: / %
Rep Power:
Re: [PHP] Quiz script using MySQL.

Yup. I was just saying
  Reply With Quote
Reply

Bookmarks



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Powered by vBadvanced CMPS v3.0 RC2

All times are GMT -7. The time now is 07:32 PM.


vBulletin skin developed by: eXtremepixels
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The contents of this webpage are copyright © 2006-2008 GamingGutter.com. All Rights Reserved.

Page generated in 0.11573291 seconds (100.00% PHP - 0% MySQL) with 19 queries