user7553556
user7553556

Reputation: 25

PHP Get Page Not Working

    if (isset($_GET['page']) && $_GET['page'] == 'myProfile') {     // Account Profile
        $page = 'myProfile';
    } else {                                                        // Dashboard
        $page = 'dashboard';
    }

if (isset($_GET['page']) && $_GET['page'] == 'coupon') {        // Settings
        $page = 'settings';
    } else {                                                        // Dashboard
        $page = 'dashboard';
    }

The code above, If i go to ?page=myProfile it leads to the dashboard rather myProfile same for the settings one?

There is code further down that leads myProfile to the .php file etc.

If i remove the 2nd bit of the code (settings) the account profile bit works? This is using a framework called FrameWorx

Upvotes: 0

Views: 92

Answers (2)

Zunderscore
Zunderscore

Reputation: 158

In your example you are first setting the $page variable, and in the second you potentially overwrite that same variable.

Try using a switch in stead of an if. This makes it a lot cleaner and easier to understand.

if(!isset($_GET['page'])) {
    $page = 'dashboard';
} 

switch($_GET['page']) {
    case 'myProfile':
         $page = 'myProfile';
    break;
    case 'coupon':
         $page = 'settings';
    break;
    default:
         $page = 'dashboard';
}

Upvotes: 1

u_mulder
u_mulder

Reputation: 54841

Second if rewrites value of $page which has been set in previous if. Change your ifs to this, for example:

if (isset($_GET['page']) && $_GET['page'] == 'myProfile') {
    $page = 'myProfile';
} elseif (isset($_GET['page']) && $_GET['page'] == 'coupon') {
    $page = 'settings';
} else {
    $page = 'dashboard';
}

Upvotes: 3

Related Questions