Jump to content

PHP loop/update problem


plastik77

Recommended Posts

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 test the user has just created. 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 an "Update Question Numbers" submit button, which should update the database with the new question numbers and refresh the page to display the updates. 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.

 

Hopefully this makes some sort of sense (probably not), 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. I realise some more clarification might be needed on this one.

 

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>

Link to comment
https://forums.phpfreaks.com/topic/63520-php-loopupdate-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.