Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    93
    Thanks
    9
    Thanked 0 Times in 0 Posts

    PHP help for re-ordering questions in a test generator

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

    PHP Code:
    <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>

  • #2
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Srry, but that doesn't make sense at all. Try breaking the problem down into chunks and present one chunk at a time.
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • #3
    New Coder
    Join Date
    Aug 2007
    Posts
    93
    Thanks
    9
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New Coder
    Join Date
    Aug 2007
    Posts
    93
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Problem solved at last - sorry for the incoherent ramble!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •