I got my code for the quiz set up, just one problem I don't like.

function create_question($q_id, $set_id, $fq_id)
{

#this function takes the input of a question id number ($q_id), set id
number ($set_id),
#and form question number ($fq_id) and creates a question with answers
for the form
#this function assumes $q_id is a valid question id number for the
database

(deliberately ommitting how I connect to the database)

$question_name_query = "SELECT question FROM tgs_questions WHERE q_id
= $q_id";
#this query pulls out the actual question

$question_name_result = mysql_query($question_name_query) or
die("Unable to run Question Name Query");

$question_name_row=mysql_fetch_array($question_name_result);

$question = $question_name_row['question'];

$answers_query = "SELECT DISTINCT tgs_answers.ans_id, tgs_answers.answer
                  FROM tgs_set_answers, tgs_answers
                  WHERE tgs_set_answers.set_id = $set_id
                  AND tgs_set_answers.ans_id = tgs_answers.ans_id
                  ORDER BY RAND()";
#this query pulls out the answers for the questions and puts them in
random order

$answers_result = mysql_query($answers_query) or die("Unable to run
Answers Query");

$prepared_question = "<dt><input type='hidden' name='Questions[]'
id='Questions[]' value='" . $q_id . "' />\n";
$prepared_question .= "<input type='hidden' name='Sets[]' id='Sets[]'
value='" . $set_id . "' />\n<p>";
$prepared_question .= $question . "</p></dt>\n";
$count = 0;

   while($answers_row=mysql_fetch_array($answers_result))
   {
     $answer_text=$answers_row['answer'];
     $answer_nr=$answers_row['ans_id'];
     $prepared_question .= "<dd><input type='radio' name='Answer" .
$fq_id . "' id='" . $answer_nr . "' value='" . $answer_nr . "' />\n";
     $prepared_question .= "<label for='" . $answer_nr . "'>" .
$answer_text . "</label></dd>\n";
   }
   
return $prepared_question; 
        
}

function create_form()
{
#this function creates the form necessary to take the trivia quiz

(once again, deliberately ommitting how I connect to the database)

$random_questions_query = "SELECT DISTINCT q_id, set_id FROM tgs_quest_set
                           ORDER BY RAND() LIMIT 0, 5";
                           
$random_questions_result=mysql_query($random_questions_query) or
die("Unable to run Random Questions query\n");
                           
$form = "<form
action='http://tgs.gargoyles-fans.org/secure-php/display/trivia.php'
method='post'>\n";
$form .= "<input type='hidden' name='TotQuest' id='TotQuest' value='5'
/>\n";
$count = 0;

  while($random_questions_row=mysql_fetch_array($random_questions_result))
  {
    $form = $form . "<p></p>\n";
    $count = $count + 1;
    $question_id = $random_questions_row['q_id'];
    $set_id = $random_questions_row['set_id'];
    $question = create_question($question_id, $set_id, $count);
    $form = $form . $question;
  }

  $buttons = "<input type='submit' id='grade' name='grade' value='Take
the quiz!' />";
  $buttons = $buttons . "\n - <input type='submit' id='reset'
name='reset' value='Reset Questions!' />";
  $form = $form . "<p></p>\n" . $buttons . "\n</form>";
  
  return $form;

}

The above is how I generate the form.  The problem is I would like to
set up a loop to pull in all the values selected by user from the
radio buttons.

Right now, I'm doing the following to pull out the information
submitted from the form:

$total_questions = $_POST['TotQuest'];
$questions = $_POST['Questions'];
$sets = $_POST['Sets'];
$ans1_id = $_POST['Answer1'];
$ans2_id = $_POST['Answer2'];
$ans3_id = $_POST['Answer3'];
$ans4_id = $_POST['Answer4'];
$ans5_id = $_POST['Answer5'];

Is there anyway on my coding to set things up so I can use the POST
method to pull out an array of all the answers so I wouldn't have to
manually code to pull each answer in?  I'd rather use a loop to pull
out individual q_id's, set_id's, ans_id's and pass them along to my
grading function inside said loop instead of pulling each answer out
one at a time.




Community email addresses:
  Post message: [email protected]
  Subscribe:    [EMAIL PROTECTED]
  Unsubscribe:  [EMAIL PROTECTED]
  List owner:   [EMAIL PROTECTED]

Shortcut URL to this page:
  http://groups.yahoo.com/group/php-list 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/php-list/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/php-list/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:[EMAIL PROTECTED] 
    mailto:[EMAIL PROTECTED]

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 

Reply via email to