tags:

views:

125

answers:

2
<?php

if ($_GET['user_id']) $content = $_GET['user_id'];
else $content = $_GET['content'];


switch($content) {

//The default page
default:
include_once('main.php');
break;

//The news related stuff
case 'news':
include_once('news.php');
break;

//Show the profile of a user
case 'user_id':
include_once('profile.php');
break;
 }
?>

index.php?user_id=id won't work. Any ideas?

+3  A: 

the default case needs to be the last one.

switch($content) {
    //The news related stuff
    case 'news':
        include_once('news.php');
    break;

    //Show the profile of a user
    case 'user_id':
        include_once('profile.php');
    break;

    //The default page
    default:
        include_once('main.php');
    break;
}

Plus, you said that this doesn't work: index.php?user_id=id

The $content variable is being populated by the value of user_id in the _GET string, therefore if you wanted to see something other than the default page, you'd have to do one of these:

index.php?user_id=news
index.php?user_id=user_id

This probably isn't how you want it to behave, but given your code, this is what it's doing...

nickf
still dont work :/
You haven't specified what you mean when you say that it "dont work". What does it do, and how does that differ from what you expect it to do? Do you get any error message?
Guffa
The default case doesn’t need to be the last one.
Gumbo
well, if you want the other cases to fire, then it needs to be after them.
nickf
hmm ok, I just looked it up and apparently you're right: it's not a technical problem, though it is a readability problem IMHO.
nickf
+3  A: 

Maybe you intended this:

if (isset($_GET['user_id'])) $content = 'user_id';

Instead of:

if ($_GET['user_id']) $content = $_GET['user_id'];
else $content = $_GET['content'];

Then the switch would execute the user_id case and include profile.php.

Gumbo
thats what i meant thanks
You should have said that in your question.
Gumbo