Survey 239

Chapter 9: Survey 239

determined by the values passed by forms or links. The first time through there will

be no variables passed, so a list of the current questions will be presented along with a form for entering a new question. Each of the links to questions that already exist in the database look like this:

<a href=”admin_questions.php?what=edit&question_id=2” > When a link like this is clicked, and the admin_questions.php script is run again,

the very bottom of the script will run, as shown here: else

{ $qform_title = “Edit A Question : $question_id”; fetch_question($question_id);

} print subtitle($qform_title); print start_form(“admin_questions.php”); print paragraph(“<b>Question:</b>”,

text_field(“question”,$question,60)); Notice how you can get all the information associated with $question_id with

one function call ( fetch_question() ). Because of the way the functions have been created, this automatically gives you a global variable for $ question.

Next, go into this loop:

$acount = 0; if ($question_id > 0) {

$result = safe_query(“select answer_id, answer from answers where question_id=$question_id order by answer_id

“); while (list($aid,$atxt) = mysql_fetch_array($result)) {

$acount++; print text_field(“answer_text[$acount]”,”$atxt”,60); print hidden_field(“answer_id[$acount]”,”$aid”); print “ ($aid)<br>\n”;

240 Part III: Simple Applications

The number 10 here limits the number of answers to each question to 10. This block gets the answers for the selected question and prints them out inside text fields. Additional information is put inside hidden fields. When printed out the result for one answer will look like this:

<input type=”text” name=”answer_text[1]” value=”Answer” size=”60” > <input type=”hidden” name=”answer_id[1]” value=”10”>

When this form is submitted, $answer_text will be an array. $acount will see that the key of the array is incremented by 1 for each additional form field. Note that we need to make use of a hidden form element here. That is because each answer requires three pieces of information: what the answer number is (1-10), the answer text, and if the answer came from the database, we need to know the pri- mary key of the row the answer came from. The hidden field will create an array named $answer_id. The value in each element of that array will be the primary key of the row storing the answer. The index of that array will be the match with the index of $answer_text. So in code it looks like this,

$i = 1; $answer_text[$i]; $answer_id[$i];

You’d know that $answer_id[$i] contains the primary key of a row, and $answer_text[$i] is the answer text that belongs in that row. The previous section of code will print out form elements only where there is an answer. But you should offer blank form elements so the administrator can enter new answers.

while ($acount < 10) {

$acount++; print text_field(“answer_text[$acount]”,””,60); print hidden_field(“answer_id[$acount]”,0); print “<br>\n”;

} This will complete the form, giving all the blank elements you need. For these

blank answers, the form will contain the following: <input type=”text” name=”answer_text[8]” value=”” size=”60” >

<input type=”hidden” name=”answer_id[8]” value=”0”><br>