Posted on July 8th, 2010
14947 views In this tutorial I am going to teach you how to create a simple quizsystem. I saw that in these days, a lot of websites have this type ofthings. In this way, you can keep a track of what your users desires, soyou can implement it in the following versions of your site. Thisscript can also be implemented in any type of php script.
First of all, you will need to create a database in your phpmyadmin andthen run the following sql command:
CREATE TABLE `quiz` ( `id` mediumint(8) NOT NULL auto_increment, `questions` varchar(255) NOT NULL default '', `answers` varchar(255) NOT NULL default '', PRIMARY KEY(`id`), UNIQUE(`questions`) ) ENGINE=MyISAMDEFAULT CHARSET=utf8; CREATE TABLE `money` ( `id` int(20) NOT NULL auto_increment, `money` varchar(250) NOT NULL, `data` text NOT NULL, PRIMARY KEY(`id`) ) ENGINE=MyISAMDEFAULT CHARSET=utf8;
Next, go into your config.php file and copy thefollowing code:
<?php session_start(); error_reporting(0); // Database Information $AdresaBazaDate = "localhost";// DB ADDRESS ( usually localhost ) $UtilizatorBazaDate = "";// DB USER $ParolaBazaDate = "";// DB PASS $NumeBazaDate = "";// DB NAME $conexiune = @mysql_connect($AdresaBazaDate,$UtilizatorBazaDate,$ParolaBazaDate) or die("Nu ma pot conecta la MySQL!"); @mysql_select_db($NumeBazaDate, $conexiune) or die("Nu gasesc baza de date ".$db."!"); @mysql_close($AdresaBazaDate,$UtilizatorBazaDate,$ParolaBazaDate); ?>
I assume that you already know a little bit of php since you are a socalled âwebmasterâ and I am not going to explain the code. If you needhelp just drop me a line through the contact form. Next we are going to wrap up the index.php file:
<?php include("config.php"); ?> <?php $sql = "SELECT * FROM quiz ORDER BY rand() LIMIT 1"; $result = mysql_query($sql); $row = mysql_fetch_object($result); $question = "$row->questions"; $correctanswer = "$row->answers"; $money = '20'; $today = date('Y-m-d'); if (isset($_POST['submitted'])) { if (empty($_POST['answer']) || empty($_POST['id'])) { echo ' Also, if you want to have a top.php file so you canshow for example how much money was raised, just use the following code:
<?php include ("config.php"); ?> <?php $query1="SELECT money, data, Sum(money) AS SumOfMoney FROM money GROUP BY DATE_FORMAT(data, '%M %d, %Y') = NOW()"; $result1=mysql_query($query1); $row1=mysql_fetch_assoc($result1); echo 'Total: '.$row1['SumOfMoney'].' dollars '; ?> There you go, you have created a sexy simple quiz system. The only thingis that you have to manually put your questions with your answers inyour database. In the future maybe I will develop a better version ofthis script. |