Log in

View Full Version : quiz using php and mysql



tomwaits4noman
05-23-2009, 12:15 AM
I am having problems creating a quiz using my php and mysql.
I am able to hard code the questions into the php but for the project the questions and answers need to be stored in the database assigned values for correct and incorrect answers.

I have managed to build the mysql database and add the questions and answers to mysql tables however I am not sure on the php code to retrieve the questions and answers and also to give the user a score.

As well as taking the quiz, the user also needs to be able to upload, edit and delete their own quizes in a seperate php file.

I have searched the forums here and online and can't find tutorials to advise on how to do this by retrieving the questions and answers from the mysql database instead of hard coding it into php files.

appreciate any help. Thanks


ok editting this have found a possible to the take a quiz but would appreciate help on way to add and edit quizes.

===================

to take a quiz

<?php include("includes/setups.php"); ?>
<?php draw_setups("Web Design Project - registration", "default.css"); ?>
<html>
<head>
<title>Brians Project - registration</title>
<script></script>
<link rel="stylesheet" href="style/layout.css" media="screen" />
</head>
<body>
<div id="container">
<?php include('includes/header.php'); ?>
<?php include('includes/submenu.php'); ?>
<?php include('includes/menu.php'); ?>
<?php draw_menu(3); ?>
<div id="content">
<?php
if(isset($_POST["quiz_id"])) {
$qid = $_POST["quiz_id"];
$id = 1;

$con = mysql_connect("localhost", "root", "") or die("<p>Unable to connect to the DBMS : Error code #".mysql_error()."<br /></p>");
mysql_select_db("quizes", $con);

$strQ = "SELECT title FROM quizes WHERE autokey = ".$qid.";";
$result = mysql_query($strQ) or die ("<p>Error accessing database : Error code #".mysql_error()."<br /></p>");

$row = mysql_fetch_array($result);
echo "<h2>".$row["title"]."</h2><p></p>";
echo "<p><div style=\"width:65%;text-align:right;top:-20px;\"><a href=\"take_a_quiz.php\">Take another quiz</a></div><form action=\"score_test.php\" method=\"POST\">";
echo "<input type=\"hidden\" id=\"quiz_id_num\" name=\"quiz_id_num\" value=\"".$qid."\" />";

$strQ = "SELECT * FROM questions WHERE quiz_id = ".$qid.";";
$result = mysql_query($strQ) or die ("<p>Error accessing database : Error code #".mysql_error()."<br /></p>");

while($row = mysql_fetch_array($result)) {
echo "<ul class=\"quizList\"><u>".$id.". ".$row["q_text"]."</u>";
$strA = "SELECT * FROM answers WHERE question_id = ".$row["autokey"].";";
$resultA = mysql_query($strA) or die ("<p>Error accessing database : Error code #".mysql_error()."<br /></p>");
while($rowA = mysql_fetch_array($resultA)) {
echo "<li><input type=\"radio\" name=\"q".$row["autokey"]."\" id=\"q".$row["autokey"]."\" value=\"".$rowA["autokey"]."\" />".$rowA["a_text"]."</li>";
}
echo "</ul>";
$id++;
}

echo "<hr /><input type=\"submit\" value=\"Submit\" /></form></p>";
mysql_close($con);
}
else {
?>
<h2>Take A Quiz</h2>
<p></p>
<form action="take_a_quiz.php" id="quizFrm" name="quizFrm" method="POST">
<table>
<tr>
<td>
Please select a quiz to take from the list below then press submit:
</td>
</tr>
<tr>
<td>
<!-- this code can be updated to take advantage of AJAX -->
<select id="quiz_id" name="quiz_id">
<?php
$con = mysql_connect("localhost", "root", "") or die("<p>Unable to connect to the DBMS : Error code #".mysql_error()."<br /></p>");
mysql_select_db("quizes", $con);

$strQ = "SELECT * FROM quizes ORDER BY title;";
$result = mysql_query($strQ) or die ("<p>Error accessing database : Error code #".mysql_error()."<br /></p>");

while($row = mysql_fetch_array($result)) {
echo "<option value=\"".$row["autokey"]."\">".$row["title"]."</option>";
}
mysql_close($con);
?>
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
</table>
</form>
<?php
}
?>
</div>
</div>
</body>
</html>

thetestingsite
05-23-2009, 02:08 AM
Can you post the code that you have currently so that we can see the way you have things set up.

tomwaits4noman
05-25-2009, 10:05 PM
The data base set up
===========================================

mysql> show tables;
+------------------+
| Tables_in_quizes |
+------------------+
| answers |
| questions |
| quizes |
| users |
+------------------+
4 rows in set (0.00 sec)

mysql> describe users;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| autokey | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | NO | | | |
| surname | varchar(50) | YES | | NULL | |
| username | varchar(50) | NO | | | |
| password | varchar(50) | NO | | | |
| email_address | varchar(100) | NO | | | |
| dob | datetime | NO | | | |
| date_joined | datetime | NO | | | |
| access_level | int(11) | YES | | 0 | |
| active | smallint(6) | YES | | 0 | |
+---------------+--------------+------+-----+---------+----------------+
10 rows in set (0.13 sec)

mysql> describe quizes;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| autokey | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(255) | NO | | | |
| author_id | int(11) | NO | MUL | 1 | |
| active | smallint(6) | YES | | 0 | |
| date_created | datetime | NO | | | |
+--------------+--------------+------+-----+---------+----------------+
5 rows in set (0.05 sec)

mysql> describe answers;
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| autokey | int(11) | NO | PRI | NULL | auto_increment |
| a_text | text | NO | | | |
| question_id | int(11) | NO | MUL | 1 | |
| correct_ans | smallint(6) | YES | | 0 | |
+-------------+-------------+------+-----+---------+----------------+
4 rows in set (0.08 sec)

mysql> describe questions;
+---------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+----------------+
| autokey | int(11) | NO | PRI | NULL | auto_increment |
| q_text | text | NO | | | |
| quiz_id | int(11) | NO | MUL | 1 | |
+---------+---------+------+-----+---------+----------------+
3 rows in set (0.06 sec)

===========================================



<html>
<head>
<title> take quiz</title>
<script></script>
<link rel="stylesheet" href="style/layout.css" media="screen" />
</head>
<body>
<div id="container">
<?php include('include/header.php'); ?>
<?php include('include/submenu.php'); ?>
<?php include('include/navmenu.php'); ?>
<div id="content">
<form action=results1.php method="POST">
<HR>
<UL><FONT COLOR="#CCCCCC"> When finished, click the "submit" button.</FONT></UL>
<UL>
</ul></ul>
<li>First name:<INPUT TYPE="TEXT" NAME="firstname" size="35"></li><br>
<UL>
1. question1?
<UL>
<INPUT TYPE="RADIO" NAME="question1" VALUE="answer1.1">opt1<BR>
<INPUT TYPE="RADIO" NAME="question1" VALUE="answer1.2">opt2<BR>
<INPUT TYPE="RADIO" NAME="question1" VALUE="answer1.3">opt3<BR>
</UL></UL>
2. question2?
<UL>
<INPUT TYPE="RADIO" NAME="question2" VALUE="answer2.1">opt1<BR>
<INPUT TYPE="RADIO" NAME="question2" VALUE="answer2.2">opt2<BR>
<INPUT TYPE="RADIO" NAME="question2" VALUE="answer2.3">opt3<BR>
</UL></UL>
<INPUT TYPE="SUBMIT" VALUE="Submit">
<INPUT TYPE="RESET" VALUE="Reset">
</ul></ul>
</FORM>
</div>
</div>
</body>
</html>


===================================


<html>
<head>
<title>score</title>
<script></script>
<link rel="stylesheet" href="style/layout.css" media="screen" />
</head>
<body>
<div id="container">
<?php include('include/header.php'); ?>
<?php include('include/submenu.php'); ?>
<?php include('include/navmenu.php'); ?>
<div id="content">
<?php
$score = 0; //initialize score to zero
?>
<h3>Score</h3>
<?php
$firstname = $_POST['firstname'];
$question1 = $_POST['question1'];
$question2 = $_POST['question2'];
$question2 = $_POST['question3'];
$question2 = $_POST['question4'];
$question2 = $_POST['question5'];
echo ($firstname);
?> <p></b></font>
<b>Question 1</b>

<?php
if ($question1 == "answer1.1")
{
echo ("<b>Correct</b>");
$score = $score + 1;
}
else
{
echo ("<b>Incorrect</b>");
}
?></ul>
<p>
<b>Question 2</b>
<?php
if ($question2 == "answer2.3")
{
echo ("<b>Correct</b>");
$score = $score + 1;
}
else
{
echo ("<b>Incorrect</b>");
}
?></ul>
<hr>
<b>Total score:</b>You answered <?php echo ($score); ?> question
<?php
if ($score != 1)
{
echo ("s");
}
?> correctly.
</div>
</div>
</body>
</html>


this is how I have hard coded it

I have also found a way to use javascript to load it into a seperate html file but not to load the questions into a php file from a database

====

javascript


function q_and_a(que, ans, anspos, pos)
{
this.question = que;
this.answer = ans;
this.answerPos = anspos;
this.myPos = pos;

this.draw = draw;
this.check = check;
}

var qList = new Array();
ansList = new Array("Green", "Red", "Yellow", "Blue");
qList[0] = new q_and_a("What color is grass?", ansList, 0, 1);

ansList = new Array("Green", "Red", "Yellow", "Blue");
qList[1] = new q_and_a("What color is the sky?", ansList, 3, 2);

ansList = new Array("23", "37", "53", "98");
qList[2] = new q_and_a("What is 10 + 43?", ansList, 2, 3);

ansList = new Array("10", "5", "2", "20");
qList[3] = new q_and_a("What is 100\/20?", ansList, 1, 4);

ansList = new Array("Cork", "Dublin", "Galway", "Limerick");
qList[4] = new q_and_a("What is the capital of Ireland?", ansList, 1, 5);

function draw(div_id)
{
var the_string = "<span id=\"q" + this.myPos + "\">";
the_string += this.myPos + ". " + this.question + "<br />";
for(var i=0;i<this.answer.length;i++) {
the_string += "<input type=\"radio\" value=\"" + i + "\" name=\"q" + this.myPos + "R\" onclick=\"moveThis('a" + this.myPos + "', this.value);\" />" + this.answer[i] + "<br />";
}
the_string += "<input type=\"hidden\" value=\"-1\" id=\"a" + this.myPos + "\" /></span><br /><br />";

document.getElementById(div_id).innerHTML += the_string;
}

function check(ans)
{
if(this.answerPos == ans)
{
return 1;
}
else
{
return 0;
}
}

function checkAll(questions)
{
var score = 0;
for(i=0;i<questions.length;i++)
{
the_que_div = "q" + questions[i].myPos;
the_ans_div = "a" + questions[i].myPos;
the_answer = document.getElementById(the_ans_div).value;
temp_score = questions[i].check(the_answer);
if(temp_score == 0)
document.getElementById(the_que_div).style.backgroundColor = "red";
else
document.getElementById(the_que_div).style.backgroundColor = "";
score += temp_score;
}
document.getElementById('submenu').innerHTML = "You scored " + score + " out of " + questions.length + ".";
}

function drawQSheet(questions, div_id)
{
document.getElementById(div_id).innerHTML = "<u><h3>Questions</h3></u>";
for(i=0;i<questions.length;i++)
{
questions[i].draw(div_id);
}
document.getElementById(div_id).innerHTML += "<br /><button onclick=\"eval('checkAll(qList);');\">Check your answers</button><br /><br />";
}

function moveThis(tagId, val) {
document.getElementById(tagId).value = val;
}

xereu
05-26-2009, 09:22 PM
I did something similar a year ago, if you still haven't found the solution let me know.
The way I did it used less tables and was a lot simpler.

opoll602
06-10-2009, 05:10 AM
Hey, you are right. MySql and your database supported with each other but in PHP it difficult.

I also have same problem but i solve this problem using less space and less table with database.