PHP redirect no longer working with counter

Hey all, so recently I have been setting up a little quiz in PHP and it runs off of a counter. When a user gets a question right the counter goes up and displays the next question if not the counter returns to 0 and sets the user back to the first question to start over. I decided to add echos of html image code so when the user gets a question right it will display an image according to what the answer is. Doing this however broke my redirect, I basically had it set up so when it reaches the last question it redirects to a page and it was working until the addition of the images. Here is my total php code for not including the arrays as they are not important for my issue:

[php]
// current question
$currentQuestion = 0;

if(isset($_POST[“currentQuestion”])){
$currentQuestion = $_POST[“currentQuestion”];
if(isset($questionsAndAnwsers[$currentQuestion])){
$currentAnswer = $questionsAndAnwsers["$currentQuestion"][“answer”];

 if($currentQuestion==17){
    header("Location: http://students.purchase.edu/martin.mcnicholas/scriptingfortheweb/index2.html"); /* Redirect browser */
    exit();
 }else if($_POST["guess"] == $currentAnswer){
     $currentQuestion++;
     $guess = $_POST['guess'];
     print ("<span class='Stylize2'>Your answer: $guess <br>"); 
     print("The answer expected: $currentAnswer<br>");  
     print("Answer Correct $answerCorrect<br><br>");
     if($currentQuestion==1){
     echo '<img src=MickeyMouse.png height="200"><br><br>';
   }
   if($currentQuestion==2){
     echo '<img src=Philo.jpg height="200"><br><br>';
   }
   if($currentQuestion==3){
     echo '<img src=warner.jpg height="200"><br><br>';
   }
   if($currentQuestion==4){
     echo '<img src=superman.jpg height="200"><br><br>';
   }
   if($currentQuestion==5){
     echo '<img src=own.jpg height="200"><br><br>';
   }
   if($currentQuestion==6){
     echo '<img src=scooby.png height="200"><br><br>';
   }
   if($currentQuestion==7){
     echo '<img src=garfield.jpg height="200"><br><br>';
   }
   if($currentQuestion==8){
     echo '<img src=pok1.jpg height="200"><br><br>';
   }
   if($currentQuestion==9){
     echo '<img src=peanuts.jpg height="200"><br><br>';
   }
   if($currentQuestion==10){
     echo '<img src=sam.jpg height="200"><br><br>';
   }
   if($currentQuestion==11){
     echo '<img src=haim.jpg height="200"><br><br>';
   }
   if($currentQuestion==12){
     echo '<img src=back.jpg height="200"><br><br>';
   }
   if($currentQuestion==13){
     echo '<img src=joe.jpg height="200"><br><br>';
   }
   if($currentQuestion==14){
     echo '<img src=r.png height="200"><br><br>';
   }
   if($currentQuestion==15){
     echo '<img src=wild.jpg height="200"><br><br>';
   }
   if($currentQuestion==16){
     echo '<img src=car54.png height="200"><br><br>';
   }
     print("Next Question Below<br></span><br><br>");
   } 
   else {
     $currentQuestion=0;
     $guess = $_POST['guess'];
     print ("<span class='Stylize'>Your answer: $guess <br>");   
     print("You have failed..<br>"); 
     echo '<img src=angry.gif height="200"></span><br><br>';
   }
   }else{
      exit("Question not found!");
   }
}

[/php]

Are the new if statements that echo the html images now conflicting with with php redirect? It is also maybe worth noting that I did add the error checking to see if the questions and answers exist and if not to show an error message so maybe that is another potential thing conflicting with the header redirect. I would appreciate any help and if I need to post my arrays and all of my code including the base html I will.

First off, you are doing a lot of extra typing that isn’t needs and gets more complicated to add to.

[php]$images = [
1 => ‘MickeyMouse.png’,
2 => ‘Philo.jpg’,
3 => ‘warner.jpg’,
4 => ‘superman.jpg’,
];

if( array_key_exists($currentQuestion, $images))
echo “$images[$currentQuestion]

”;
else
echo “Not found”;
[/php]

The only reason a header would break, is if you output ANYTHING to the page before the header is called. Based on the code you posted, I don’t see that. That doesn’t mean that you are not doing it however. Now, a shortcut to the header redirect it to use an html redirect. But, you have some more clean up to work on first, and it is best to fix the issue if you can before you hack something to force it to work.

Thank you, yes i should have used a key array in order to form the images. While cleaning up I noticed something which is that the redirect was pointing to a question that did not exist and once I cut it down to 15 instead of 17 it successfully redirected. Since I decided to reform the images I had to count to create them for each question and that is where I realized my mistake.

Well, using the array method will make that even easier,

[php]$r = [ 1=>‘a’,2=>‘b’,3=>‘c’,4=>‘d’];
$const = 4;

if(array_key_exists($const, $r)){
end($r);
if($const == key($r)) {
echo “We have arrived!”;
} else {
reset($r);
echo “Still more questions to go!”;
}
}[/php]

First, it verifies the key exists. Next, it checks to see if it is at the end of the array. More will need to be done, but it should give an idea of the simplification.

Sponsor our Newsletter | Privacy Policy | Terms of Service