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>
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>