Martyk
Martyk

Reputation:

if statement switch

<?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?

Upvotes: 0

Views: 320

Answers (2)

Gumbo
Gumbo

Reputation: 655489

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.

Upvotes: 3

nickf
nickf

Reputation: 546253

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...

Upvotes: 3

Related Questions