MySQL Forums
Forum List  »  Perl

Re: Problem Printing Results using while loop
Posted by: Dexter Nelson
Date: April 23, 2008 09:26AM

Alright, I've made ALOT of progress. I did away with the second loop, and also broke down the select statements separately like so.

# =====================================================================
# Use the poll database to view the information and to update.
# =====================================================================
$sth = $dbh->prepare("select * from questions") or &dbdie;
$rv = $sth->execute;

while (my($id,$identifier,$instructions,$question,$type,$islast) = $sth->fetchrow_array) {
# Print the question
print qq(<p><b><small>$id. $question</small></b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);

$f = $sth->fetchrow_hashref;
my($identifier) = "$f->{identifier}";

$asth = $dbh->prepare("select * from answers where identifier = '$identifier'") or &dbdie;
$rv = $asth->execute;
while (my($id,$identifier,$answer) = $asth->fetchrow_array) {

# Display select header.
if ($type eq "select") {
print qq(<br>&nbsp;&nbsp;&nbsp;&nbsp;<select name="answer">);

# Print the answer options.
if ($type eq "radio") {
print qq(<br>&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="answer" value="$answer"><small>$answer</small></input>);
if ($type eq "checkbox") {
print qq(<br>&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" name="answer" value="$answer"><small>$answer</small></input>);
if ($type eq "select") {
print qq(<option value="$answer"><small>$answer</small></option>);

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

With 1 loop, I can loop through all the questions while they are true, and with the second select inside the loop after the question, I can select just the answers with the matching identifiers for that question.

In essence, I only needed 1 loop that selected the answer options after the question. Right now it's only showing the first question in the questionaire, but when I edit the select statement that should change.

I hope you figured yours out.

Edited 1 time(s). Last edit at 04/23/2008 10:02AM by Dexter Nelson.

Options: ReplyQuote

Written By
Re: Problem Printing Results using while loop
April 23, 2008 09:26AM

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.