user1928251
user1928251

Reputation:

Switch statement in php switches to a specific page

<div id="navigation">
    <ul>
        <li><a href="?id=action">Action</a></li>
        <li><a href="?id=adventure">Adventure</a></li>
        <li><a href="#">Services</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Contact</a></li>
    </ul>
</div>
<?php
$id = (isset($_GET['id'])); set the value
    switch($id) {
        case 'actiune':include('/actiune/index.php');//first page
        break;
        case 'aventura':include('/aventura/index.php');//second page
        break;
    }
?>

It takes me to /action/index.php when i access adventure Why? Please I need a bit of help

Upvotes: 0

Views: 2101

Answers (1)

raina77ow
raina77ow

Reputation: 106375

Because you're switching on true/false here, as the result of isset - which is always boolean value - is assigned to $id directly. This line should be rewritten as...

$id = isset($_GET['id']) ? $_GET['id'] : '';

... where '' can be actually replaced with that default value.

In fact, you can redirect user at the very moment when you find that $_GET['id'] is empty, then analyze it's different values with switch. Like this:

if (! isset($_GET['id'])) { ... redirection ... }
switch ($_GET['id']) {
  case 'first': ...; break;
  case 'second': ...; break;
  default: ...;
}

Upvotes: 1

Related Questions