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)
===========================================
Code:
<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>
===================================
Code:
<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
Code:
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;
}
Bookmarks