Jump to content

query in a loop issues


thefortrees

Recommended Posts

I am creating a survey that pulls questions and responses from a DB. When I query the DB for each question and response from the mysql command line, all the desired data are returned.

 

As soon as the query is implemented into the PHP code below, the first response that should have been retrieved from the DB is not. (There are multiple responses to each question - it does not retrieve the first response for the first question, but all responses after that are retrieved.

 

$query = "SELECT question, response, position FROM questions, responses, survey_instances " .
		"WHERE questions.questions_id = responses.questions_questions_id " .
		"AND questions.questions_id = survey_instances.questions_questions_id ORDER BY position";

$result = mysql_query($query);
$row = mysql_fetch_array($result);

$responses = array();

//Print out questions. If position/question has already been echoed, do not echo again
//so that one question is not echoed multiple times.	
while ($row = mysql_fetch_array($result)){

	if ( !in_array($row['question'], $responses) ){
		echo
			"<table>" .  
			"<tr>" . $row['position'] . ") " . $row['question'] . "</tr>" .
			"<tr><td>" . $row['response'] . "</tr></td>";

		$responses[] = $row['question'];
	}

	echo 
		"<tr><td>" . $row['response'] . "</td>" .
		"</table><br>";
}

Link to comment
https://forums.phpfreaks.com/topic/56456-query-in-a-loop-issues/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.