nicky77
08-05-2007, 10:20 PM
Hi there, I'm developing a simple test generator and I want to allow users to be able to preview a test and to manually change the question numbers if they wish. I currently have a preview page which displays the relevant question/answer information for the particular test. The question numbers are incremented (not auto) as each one is created, but i want to allow the flexibility to the user to reorder the questions. Therefore each question's number is displayed in an editable text field, with a "Update Question Numbers" submit button, which should update the database and refresh the page to display the updates. However, i've struggled with the code to implement this, with the problem being that the number of the last question is the one which is written for all the questions in that test. So my code is clearly flawed, and I've not been able to resolve it. If anyone could help, that would be greatly appreciated.
Hopefully this makes some sort of sense, here's a snippet of the database structure as it might help. There are 2 question tables, one (Question) for a permanent bank of questions and one (TestQuestion) for the use of a question in a particular test.
Test {testNum, name, dateCreated, createdBy}
Question {questID, questText, quesType, category}
Section {test#, sectionID, sectionName}
TestQuestion {t#, qID, weight, questNumber, sectionID} – note composite key, dependent on Test and Question for unique identification
Answer {qID, option, ansText, correct(yes/no)}
<form action="<? $_SERVER['PHP_SELF'] ?>" method ="post" >
<?
$query = mysql_query("select* from TestQuestion t,Question q, Section s WHERE t.testID = '$_SESSION[test]'
and q.questID = t.questID and s.sectionID = t.sectionID and s.testId = '$_SESSION[test]' order by questNumber asc");
$numrows = mysql_num_rows($query);
if($numrows ==0)
{
echo "No questions exist";
}
else
{
echo "<table border='1'>";
for ($i=0;$i<$numrows;$i++)
{
$row = mysql_fetch_array($query);
$questNum = $row['questNumber'];
$qID = $row['questID'];
$sectName = $row['sectionName'];
$qtext = $row['quesText'];
$cat = $row['category'];
$weight = $row['weight'];
echo "<tr>";
echo "<th>Q: <input type='text' name='number' value='".$questNum."' size='2'></th>";
echo "<th>".$qtext."</th></tr>";
if(isset($_POST['Submit']))
{
//update with the new quest number
$update = mysql_query("update TestQuestion set questNumber ='$_POST[number]' where questID ='$qID'");
}
$query2 = mysql_query("select* from Answer where qID = '$row[questID]'");
$numrows2 = mysql_num_rows($query2);
for ($j=0;$j<$numrows2;$j++)
{
$row2 = mysql_fetch_array($query2);
$option = $row2['option'];
$ans = $row2['ansText'];
$corr = $row2['correct'];
echo "<tr><td>".$option.": </td>";
echo "<td>".$ans."</td></tr>";
if($corr=="yes")
$correct=$option;
}
echo "<tr><td>Correct Answer: ".$correct."</td><td>Weighting: ".$weight."</td></tr><br/>";
echo "<tr><td>Section: ".$sectName."</td><td>Category: ".$cat."</td></tr><tr>
<td> </td></tr>";
}
}
?>
</table>
<br/>
<input name="Submit" value="Update question order" type="submit" title="Submit" />
</form>
Hopefully this makes some sort of sense, here's a snippet of the database structure as it might help. There are 2 question tables, one (Question) for a permanent bank of questions and one (TestQuestion) for the use of a question in a particular test.
Test {testNum, name, dateCreated, createdBy}
Question {questID, questText, quesType, category}
Section {test#, sectionID, sectionName}
TestQuestion {t#, qID, weight, questNumber, sectionID} – note composite key, dependent on Test and Question for unique identification
Answer {qID, option, ansText, correct(yes/no)}
<form action="<? $_SERVER['PHP_SELF'] ?>" method ="post" >
<?
$query = mysql_query("select* from TestQuestion t,Question q, Section s WHERE t.testID = '$_SESSION[test]'
and q.questID = t.questID and s.sectionID = t.sectionID and s.testId = '$_SESSION[test]' order by questNumber asc");
$numrows = mysql_num_rows($query);
if($numrows ==0)
{
echo "No questions exist";
}
else
{
echo "<table border='1'>";
for ($i=0;$i<$numrows;$i++)
{
$row = mysql_fetch_array($query);
$questNum = $row['questNumber'];
$qID = $row['questID'];
$sectName = $row['sectionName'];
$qtext = $row['quesText'];
$cat = $row['category'];
$weight = $row['weight'];
echo "<tr>";
echo "<th>Q: <input type='text' name='number' value='".$questNum."' size='2'></th>";
echo "<th>".$qtext."</th></tr>";
if(isset($_POST['Submit']))
{
//update with the new quest number
$update = mysql_query("update TestQuestion set questNumber ='$_POST[number]' where questID ='$qID'");
}
$query2 = mysql_query("select* from Answer where qID = '$row[questID]'");
$numrows2 = mysql_num_rows($query2);
for ($j=0;$j<$numrows2;$j++)
{
$row2 = mysql_fetch_array($query2);
$option = $row2['option'];
$ans = $row2['ansText'];
$corr = $row2['correct'];
echo "<tr><td>".$option.": </td>";
echo "<td>".$ans."</td></tr>";
if($corr=="yes")
$correct=$option;
}
echo "<tr><td>Correct Answer: ".$correct."</td><td>Weighting: ".$weight."</td></tr><br/>";
echo "<tr><td>Section: ".$sectName."</td><td>Category: ".$cat."</td></tr><tr>
<td> </td></tr>";
}
}
?>
</table>
<br/>
<input name="Submit" value="Update question order" type="submit" title="Submit" />
</form>