Jump to content

Recommended Posts

I am making a quiz application with PHP and it has an external XML file that has questions and and a title and in the question tags has an attribute if it's the correct answer or not. I am NOT using MySQL, and do not want to for this application. Everything works fine until I put this all in a while loop.

 

Basically I want it to check it is at the last question, and when it gets there stop, but as you will see in the code below it has a break; at the else statement because it will go into an infinite loop if not by repeating Wrong. It shouldn't even get to wrong if the If statement is not true, right?

 

How can I get it to stop showing Wrong over and over infinitely? Another side note, I get an error on the last question saying that it is trying get the property of a non-object which basicly means it's looking for, e.g., question five when there is only 4 questions and trying to display it ON the LAST question only. Please help! I am very new to PHP! P.S. there are no syntax errors.

 

while($question_count > $question_num) {
	if($xmlData->item[$question_num]->questions->answer[$post_answer]['correct'] == 'true') {
		echo '<form action="index2.php" method="post">
		<fieldset>
		';

		$question_num = ++$question_num;
		foreach ($xmlData->item[$question_num]->title as $title) {
			echo '<h2>'.($title).'</h2>';
			foreach($xmlData->item[$question_num]->questions->answer as $answers) {
				echo 
				'<span>'.$answers.
				'
				</span><input type="radio" name="answer" value="'.$answer_num.'" /><br />';
				$answer_num++;
			}
		}

		echo '
		<input type="hidden" name="question" value="'.$question_num.'"/>
		<br />
		<input type="submit" value="Submit" />
		</fieldset>
		</form>';
	}
	else {
		echo'<b>Wrong</b>';
		break;
	}
}	

Link to comment
https://forums.phpfreaks.com/topic/98779-ifelse-problem-in-while-loop/
Share on other sites

How can I get it to stop showing Wrong over and over infinitely? Another side note, I get an error on the last question saying that it is trying get the property of a non-object which basicly means it's looking for, e.g., question five when there is only 4 questions and trying to display it ON the LAST question only. Please help! I am very new to PHP! P.S. there are no syntax errors.

 

the loop is becuase you stay on the same question, and the last question is becuase you check to see if your currect question is greater (in value) than the last question

 

see code comments

else {
		echo'<b>Wrong</b>';
		break;
	}

to

else {
		echo'<b>Wrong</b>';
		//break;
		$answer_num++;
	}

 

for the last question problem

change

while($question_count > $question_num) {

to

while($question_count >= $question_num) {

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.