$HTTP_POST_VARS or $_POST is an associative array that holds the keys and
values for all inputs/selects in  form from the submitted page.  Use
array_keys() to determine the names of all your list boxes to examine each
key and its value.  Or you can use the list($key,$val) =
each($HTTP_POST_VARS) construct to extract each key and value.

So then you just walk through the array and process each entry.

-----Original Message-----
From: Chris MacKenzie [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, April 03, 2002 8:47 AM
To: Rick Emery
Subject: Re: [PHP-DB] Submitting Dynamic Form

Hi Rick,

Here's a code snippet (less error checking). Basically there could be as
many as twnety questions listed with their associated listboxes of
possible answers. Also be aware that ms-sql identity type is similar to
mysqls autoincrement type except that once a record is deleted, that
identity value is never used again.

My problem is that since the question_id field is never guaranteed to be
sequential and seeing that I have named the answer listboxes with the
question_id field, what is a good way to extract the values from the
form and perform an update query ?

How can I do this if I don't know what the question_id values will be ?

-- CUT
$sql= "SELECT * FROM tbl_exam_questions ORDER BY question_id;";

$numrows = mssql_num_rows($result);

print '<form method="POST" action="submit_exam_answers.php" name="F1">';

## List All Questions and load the possible answers into list box.

for($cnt = 0; $cnt < $numrows; $cnt++){
        $row = mssql_fetch_array($result);
        print '<p>'.$row["question_text"].'</p>'; 
        print 'My Answer is: <select size="1"
name="'.$row["question_id"].'"><option selected value="0">- SELECT ONE

        ## Now fetch the possible answers and load it into the list box.

        $pa_sql = "SELECT * FROM tbl_exam_answers WHERE question_id =
        $pa_result = mssql_query($pa_sql,$conn1);

        $pa_numrows = mssql_num_rows($pa_result);

        for($pa_cnt = 0; $pa_cnt < $pa_numrows; $pa_cnt++) {
          $pa_row = mssql_fetch_array($pa_result);
          print '<option
        print '</select><br><hr><br>';
        ## Finished Loading Possible Answers into list box.


print '<p><input type="submit" value="Save Answer" name="Submit"><input
type="reset" value="Reset Form" name="Reset"></p></form>';      

-- CUT

Oh, and before I forget tbl_exam_questions.correct_answer holds the
relevent id entry from tbl_exam_answers

> what happened when you extracted the form field names from the DB?
>> Hi All,
>> I'm pretty new to the whole php thing and I'm currently making an
>> multiple choice exam type of thing with php/mssql.
>> The two tables  concerned are called tbl_exam_questions and
>> tbl_exam_questions which are defined like so.
>> [tbl_exam_questions] (
>>         [question_id] [int] IDENTITY (1, 1) NOT NULL ,
>>         [question_text] [varchar] (355) NOT NULL ,
>>         [correct_answer] [int] NOT NULL
>> [tbl_exam_answers] (
>>         [id] [int] IDENTITY (1, 1) NOT NULL ,
>>         [question_id] [int] NOT NULL ,
>>         [p_answers] [varchar] (255) NOT NULL
>> What I'm trying to do is to pull all the questions out and display them
>> with the possible answer in a html form and dropbox.
>> I'm displaying the entire exam on one page, with one submit button on
>> the bottom so that the student can review the answers before finally
>> submitting them.
>> The problem I have is that the form field names are dynamic in that they
>> are set up the value held in tbl_exam_answers.question_id and with
>> ms-sql the identity type is not guarenteed to be incremental, so how can
>> I reference them to send a query back to the DB to mark the student ?
>> Have I painted myself into a corner ? :-/

PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to