tags:

views:

124

answers:

4

Hi guys, I have a bit of a problem with my PHP code, I am assigning values to variables in different states of the flow depending on what I receive, but for some reason it keeps getting stuck at one point, here is the code.

if (isset($session)) {  
    //if the user is in the database
    if ($row == 1) {

        $from = $_GET['from'];

            if (isset($from)) { 

                $page = $_GET['page'];

                switch ($page) {
                    case "game":

                        $page = "game";
                        sendVars($page);//send the variable

                        break;
                    case "gallery":

                        $page = "gallery";
                        sendVars($page);//send the variable

                        break;
                    case "leaderboard":

                        $page = "leaderboard";
                        sendVars($page);//send the Variable                     
                        break;

                }
        }else {             
                $page = "game";

                sendVars($page);//send the variable
            }

        //if the user is not in the database
        }else {

            //do this
        }

} else {

    //register
}

Now for some odd reason, it keeps setting the value of $page to game, even though I set the page variable to gallery like so http://www.mydomai.com/?from=set&page=gallery . the only reason for this that I can think of is that my switch is not working as it should? or it is bypassing the switch somehow?

Thanx in advance!

+1  A: 

Try doing a var_dump($page);exit; before the switch and see what it spits out.

Also you can do a var_dump($from) and see what that is spitting out - it may be that it goes to the else, so it may not even be getting to the switch.

xil3
indeed - since the else block forces it to "game" I'd be suspicious that it doesn't even get into the switch.
scunliffe
+5  A: 

I just ran your code after removing a few of the unessersary variable assignments:

<?php

// I added this function just for testing
function sendVars($page) {
  echo $page;
}

if (isset($_GET['from'])) {

  $page = $_GET['page'];

  switch ($page) {
    case "game":
      sendVars($page); //send the variable

      break;
    case "gallery":
      sendVars($page); //send the variable

      break;
    case "leaderboard":
      sendVars($page); //send the Variable
      break;
  }
} else {
  $page = "game";

  sendVars($page); //send the variable
}

And it all seems fine, xxx.php?from=1&page=gallery echos out "gallery", try doing a print_r($_GET) at the top of your script and see what it prints out and let us know.

On a side note I think the below may be shorter for you and still do the same thing:

if (isset($_GET['from'])) {
  // Check if $_GET['page'] exsists and is either game, gallery or leaderboard
  if (isset($_GET['page']) && in_array($_GET['page'], array('game', 'gallery', 'leaderboard')))
    sendVars($_GET['page']);
}
else
  sendVars('game');

I hope this helps

Cheers Luke

Luke
I am getting Array ( [from] => set [page] => gallery ) with that same URL.
Thats good news, did you try replacing the code inside if($row == 1){} with the code above? where exactly in the code did you print_r($_GET), from what I can see it should work if $_GET is populated as your above comment
Luke
I put it in right above the switch.
So if you do echo $page; right above the switch it is "gallery"? If so can you add echo "test"; into the gallery case, see if that case is ever active. If it echos out test then its all good, otherwise, is there any whitespace around gallery, struggling to see what else could be wrong.
Luke
+1  A: 

If this is inside a function, I personally prefer guard-style clauses than constantly increasing the levels of indentation. The idea is you pick out the bad conditions (ie if something is going wrong) to "protect" the larger block of logic.

In your case that's the switch statement.

if (!isset($session))
    return ...; // register

if ($row != 1)
    return ...; // do this

$from = $_GET['from'];
$page = $_GET['page'];

if (isset($from)) switch ($page) {
    case "game":
        $page = "game";
        break;

    case "gallery":
        $page = "gallery";
        break;

    case "leaderboard":
        $page = "leaderboard";                   
        break;
}

else $page = "game";

return sendVars($page);// don't need to repeat this if all cases do it!

It's just a style of code and it's not going to fix all (if any) of your problems. You actually don't need the switch block in there for this code. I can't see that it's doing anything.

Oli
If $page = 'foobar', you send it but it's not the case from kielie
M42
A: 

You don't necessarily need the switch statement. I can't see an obvious problem with your logic but I think this will do the same thing:

if (isset($session))
{  
    //if the user is in the database
    if ($row == 1)
    {
        $page = (in_array($_GET['page'],array('game','gallery','leaderboard'))) ? $_GET['page'] : "game";
        sendVars($page); //send the variable
    }
    else //if the user is not in the database
    { 
        //do this
    }
}
else
{
    //register
}

Just saw Luke used the same in_array method but 25 mins before me. Serves me right for being slow!

Brendan Bullen
Using this method, you could do things such as read the "allowed" pages from a database or config file and populate the array that's passed into the conditional. That way you can add pages without having to change your code
Brendan Bullen
That's great, will give it a try and report back, thanx for taking the time to help out!