Jump to content

duplicating results on screen


woodplease

Recommended Posts

I am creating a quiz system whereby a user enters a quiz title, and then questions and answers so that other users can complete them later on. I have 3 tables, one holding quiz names, 1 with the questions and 1 with answers. When I add the first quiz, everything displays fine e.g

question 1

    answer 1

    answer 2

    answer 3  etc

 

The problem is when I add another quiz, the answers start duplicating e.g

question 1

    answer 1

    answer 1

    answer 2

    answer 2 etc

 

Could anyone help solve why this is happening. I assume its something to do with the for loop, but i dont know what

 

<?PHP

for($i = 0; $i < $numofrows; $i++) {
$row = pg_fetch_array($result2); //get a row from our result set
if($i % 2) { //this means if there is a remainder
	echo "<TR bgcolor=\"#666666\">\n";
} else { //if there isn't a remainder we will do the else
	echo "<TR bgcolor=\"#777777\">\n";
}
echo "
<p>".$row['questionno']."</p>
<p>".$row['question']."</p>
\n";

$query2 = "SELECT questions.questionno, questions.question, questions.quizref, questions.questionref, answers.answerno, answers.answer, answers.answervalue, answers.questionno, answers.quizref, answers.answerref, quiz.quizref, quiz.name, quiz.createdby FROM questions
   JOIN answers ON questions.questionno = answers.questionno JOIN quiz ON quiz.quizref = answers.quizref
   WHERE questions.questionno = ".$row['questionno']. " AND quiz.quizref = ".$id;




$result3 = pg_query($query2) or die ("Query failed");
//let's get the number of rows in our result so we can use it in a for loop
$numofrows2 = pg_num_rows($result3);

for($j = 0; $j < $numofrows2; $j++) {
	$row2 = pg_fetch_array($result3); //get a row from our result set
	if($j % 2) { //this means if there is a remainder
		echo "<TR bgcolor=\"#666666\">\n";
	} else { //if there isn't a remainder we will do the else
		echo "<TR bgcolor=\"#777777\">\n";
	}
	echo "
	<p><input type=\"radio\" name=\"" . $row['questionno'] . "\" id=\"selection[]\" value=\"" . $row2['answervalue'] . "\" />".$row2['answerno']. "–".$row2['answer']."</p>
	\n";

}
}

?>

 

Thanks

 

 

Link to comment
https://forums.phpfreaks.com/topic/190462-duplicating-results-on-screen/
Share on other sites

   $result3 = pg_query($query2) or die ("Query failed");
   //let's get the number of rows in our result so we can use it in a for loop
   $numofrows2 = pg_num_rows($result3);

   for($j = 0; $j < $numofrows2; $j++) {
      $row2 = pg_fetch_array($result3); //get a row from our result set
      if($j % 2) { //this means if there is a remainder
         echo "<TR bgcolor=\"#666666\">\n";
      } else { //if there isn't a remainder we will do the else
         echo "<TR bgcolor=\"#777777\">\n";
      }
      echo "
      <p><input type=\"radio\" name=\"" . $row['questionno'] . "\" id=\"selection[]\" value=\"" . $row2['answervalue'] . "\" />".$row2['answerno']. "–".$row2['answer']."</p>
      \n";

 

I think your problem is in this code here. I notice you named your thread "duplicate results on screen". Does this mean you do not have duplicates in the database? As it appears as though you may have duplicate entries in the database.

 

To start, debug and check the value of $numofrows2 as this is the value that defines how many loops are done when outputting an answer. I can't see it giving the right amount you expect and because of this it would mean your SQL query is pulling too many results from the DB in the first place, so you may have to debug and check your query string.

 

PS: Didn't know about $x % 2 to check for a remainder - that's a handy one I'll use in the future. Thanks.

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.