MySQL Forums
Forum List  »  Perl

Re: Problem Printing Results using while loop
Posted by: Randy Clamons
Date: April 28, 2008 02:35PM

I see you have found a way to solve your problem, but it involves making several queries which probably adds significantly to the processing time. Another way would be to establish a control variable for the question. Then you can tell when the question changes and only print it then, once for each question. Here's your original code with a variable named $control. I left out the db calls.

my $control = '';
# Print form header.
print qq(<form action="pollanswer.cgi" method="POST">);
while (my($id,$identifier,$instructions,$question,$type,$islast,$ansid,$ansident,$ansas) = $sth->fetchrow_array) {
 if ($control ne $question ) {
  # Display each question.
  print qq(<p><b><small>$id. $question</small></b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);
  $control = $question;
 }
 # Display select header.
 if ($type eq "select") {
  print qq(<br>&nbsp;&nbsp;&nbsp;&nbsp;<select name="answer">);
 }

 if ($type eq "radio") {
  print qq(<br>&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="answer"  value="$ansas"><small>$ansas</small></input>);
 }
 if ($type eq "checkbox") {
  print qq(<br>&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" name="answer" value="$ansas"><small>$ansas</small></input>);
 }
 if ($type eq "select") {
  print qq(<option value="$ansas"><small>$ansas</small></option>);
 }

 # Display select footer.
 if ($type eq "select") {
  print qq(</select>);
 }
}

Options: ReplyQuote


Subject
Written By
Posted
Re: Problem Printing Results using while loop
April 28, 2008 02:35PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.