...

View Full Version : PHP help for re-ordering questions in a test generator



nicky77
08-05-2007, 11: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:&nbsp;<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.":&nbsp;</td>";
echo "<td>".$ans."</td></tr>";
if($corr=="yes")
$correct=$option;
}
echo "<tr><td>Correct Answer:&nbsp;".$correct."</td><td>Weighting:&nbsp;".$weight."</td></tr><br/>";
echo "<tr><td>Section:&nbsp;".$sectName."</td><td>Category:&nbsp;".$cat."</td></tr><tr>
<td>&nbsp;</td></tr>";
}
}
?>
</table>
<br/>
<input name="Submit" value="Update question order" type="submit" title="Submit" />
</form>

meth
08-06-2007, 12:42 AM
Srry, but that doesn't make sense at all. Try breaking the problem down into chunks and present one chunk at a time.

nicky77
08-06-2007, 10:25 AM
Sorry Meth, was trying to give as much detail as possible about the problem, probably over-explained it.

There is only one problem i am having with the above script. I want to update the dbs with the values in the Question number text fields. If the user submits changes, I want to be able to pick up these values and insert them into the dbs in such a way that i pick up each individual value by looping through them. At present, i only pick up the very last value, which results in all the test questions having the same number. The problem, I think, is where I have positioned the if(isset($_POST['Submit'])) within the for loop, however, I cannot see where else to position it where i will access to the variables i need. Hope this makes more sense than my last post.

nicky77
08-06-2007, 11:23 PM
Problem solved at last - sorry for the incoherent ramble!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum