Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with loop...partially executes

Status
Not open for further replies.

rizza

Programmer
Jul 16, 2002
66
US
I'm trying to display questions from a MYSQL (database)questions and loop through them writing them out. Then while writing them out i'm trying to loop through another Result set(answers) and print our all the answers that realte to the recid of the question.

It seems to accomplish everthing i need accept it will only display the answers for question 1. Then it just displays the rest of my questions.

I have no idea what i wrong with my loops and why there not working...anybody help???



while ( $questiondata = mysql_fetch_array($rsquestion))
{$quesrecid=$questiondata['recid'];
$question=$questiondata['question'];
$questionid=$questiondata['questionnum'];
echo&quot;<tr><td class=QuestionCell>$question</td</tr>&quot;;
while($answersdata = mysql_fetch_array($rsanswers))
{
$ansquesid = $answersdata['questionid'];
$answer = $answersdata['answer'];
if ($ansquesid == $quesrecid)
{ echo (&quot;<tr><td class=AnswerCell>QuestionId: $quesrecid AnswerQuestiondId: $ansquesid Answer:$answer </td></tr>&quot;); }
}
}
 
What I would do, is first query the database for the questions you are looking for, and start into a loop to display them.

Inside that loop query the database for all the questions pertaining to that question.

Something linke

$dbh = <connect to server and select database>;

$rh1 = mysql_query (&quot;SELECT <the questions and an id which designates each question>&quot;, $dbh);

while ($question = mysql_fetch_assoc($rh1))
{
<output question>;

$rh2 = mysql_query (&quot;SELECT <answers to that question using that question's ID, available in $question['<id>']>&quot;, $dbh);

while ($answers = mysql_fetch_assoc($rh2))
{
<output answers>;
}
} ______________________________________________________________________
TANSTAAFL!
 
I would let MySQL do the work. A query like this should work:
Code:
SELECT q.recid, q.question, a.answer FROM questions_table AS q, answers_table AS a WHERE q.recid=a.questionid
Then just do something like
Code:
<?php
/* Connect and everything */
$rs = mysql_query(&quot;SELECT q.recid, q.question, a.answer FROM questions_table AS q, answers_table AS a WHERE q.recid=a.questionid&quot;);
while ($qanda = mysql_fetch_assoc($rs))
{
    echo '<tr><td class=&quot;QuestionCell&quot;>' . $qanda['question'] . '</td</tr>' . &quot;\n&quot; .
         '<tr><td class=&quot;AnswerCell&quot;>Answer: ' . $qanda['answer'] . '</td></tr>' . &quot;\n&quot;;
} 
?>
//Daniel
 
This is displaying a survey. I want to get the questions out of a db this way the survey can be as many questions as i want. I also wanted to have the same flexibility with the answers.

i was thinking call a function to load the answers $dbresult) into a multidimensional array and then return that as my new answers array.

If i query the db and get the questions. loop through them the same way i was using a

while($data=mysql_fetch_array($dbresult)
{
display question

then using my answers array i could run a test condtion to
only display the results that are equally to question

}

Maybe i'm losing it over this stupid thing, when i could always use regular html to make the survey page....

Thanks for your help guys...

Rizza
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top