0

I'm having an issue with some PHP code. Basically I have an array of id values, $challengeIDs, and a value $number which stores the current location in this array (it affects which page is loaded into the body). I want to check that I haven't reached the end of the array before I move to the next ID value.

if (($number+1) < (count($challengeIDs)-1)) {
    $n = $number+1;
    echo "window.location = './challenge.php?n={$n}';";
} else {
    $_SESSION['n'] = 0;
    $_SESSION['playlist'] = "";
    echo "window.location = './index.php';";            
}
echo "});}</script>";

I've put a console.log() statement immediately above this snippet which reports that the value of $number is 1 and count($challengeIDs) is 4. But for some reason the page is going to index.php rather than going to the next page in the list. It may be elsewhere in the code, but any ideas? Anything suggestions are gratefully received :)

EDIT: Here is the console output. The values are output the line before this snippet, and the page changes according to the value. It works no problem from page 1 (n=0) to page 2 (n=1), but fails and goes to index.php going to page 3 (n=2).

Screenshot of console

SOLUTION: Ended up working it out - sorry guys, it wasn't related to this code at all. There was a check elsewhere which was incorrectly resetting the values based on a GET parameter.

13
  • 1
    Why are you combining PHP and JS in this manner? You should be using PHP's header function. Commented May 2, 2016 at 18:01
  • 1
    console.log() is JS not PHP?! Commented May 2, 2016 at 18:01
  • @JayBlanchard I'm doing that because I need the script to take some values from within the session cookie/sessionStorage. I'm building the whole script on the page using PHP - I couldn't find another workaround in this case. Commented May 2, 2016 at 18:03
  • @Rizier123 yeah but it's the text argument within a PHP echo statement....I'm printing the script to the page with PHP Commented May 2, 2016 at 18:04
  • Then you should use AJAX. Commented May 2, 2016 at 18:06

1 Answer 1

1

This may sound stupid but I think you may be using wrong the if clause In the code I can see

if (($number+1) < (count($challengeIDs)-1)) {

and you wrote that

count($challengeIDs) is 4.

So when page is 3 (n=2) where there is your problem, it goes to the else clause because

if (($number+1) < (count($challengeIDs)-1)) 

means

if ((2+1) < (4-1)) 

and 3 is not lesser than 3. So change the if clause.

Sign up to request clarification or add additional context in comments.

1 Comment

Because the PHP was printing to the page not actually doing the redirect itself, this is what is meant to happen. When it is on page 3, the javascript save() function being created in the code shown is written to take it to the index.php, not to the next challenge but this only happens when the save function is called, not when the PHP executes. I've solved the issue now. Thanks for trying to help - not a stupid point at all - I spent a long time checking and double checking those values in case I was getting confused, or the execution times for the php/js were happening at unexpected times

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.