php code error, please help.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

php code error, please help.

Post by tomsace »

Hey, I wish to get multiple pages from one php file.

This is the code I have...

Code: Select all

<?php
 
$cmd = $_GET['cmd'];
 
if ($cmd=="") { $cmd = "page1.php"; } 
 
@include('header.php');
 
switch($cmd)
{
 
case "page1":
@include('page1.php');
break;
 
case "page2":
@include('page2.php');
break;
 
case "page3":
@include('page3.php');
break;
 
}
@include('footer.php');
 
?>
In header .php it has 3 links. Linking to page1 page2 and page3. It all works fine except when the page loads it doesnt by default load page 1 which it what I want it to do. I thought the

Code: Select all

if ($cmd=="") { $cmd = "page1.php"; }
code was to load this page when loaded but it doesn't work!

Thanks alot.
Tom.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: php code error, please help.

Post by Burrito »

Code: Select all

 
$cmd = isset($_GET['cmd']) ? $_GET['cmd'] : "");
 
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: php code error, please help.

Post by tomsace »

Thanks, but I cant seem to get this to work?

And ideas?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: php code error, please help.

Post by Burrito »

I missed a paren:

Code: Select all

 
$cmd = (isset($_GET['cmd']) ? $_GET['cmd'] : "");
 
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: php code error, please help.

Post by tomsace »

Still doesn't come up!?

Should this replace the line:

Code: Select all

if ($cmd=="") { $cmd = "page1.php"; }
Sorry but im new to php.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: php code error, please help.

Post by Burrito »

not it should replace this one:

Code: Select all

 
$cmd = $_GET['cmd'];
 
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: php code error, please help.

Post by tomsace »

I have tried this but still no success??

Any more ideas?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: php code error, please help.

Post by Burrito »

replace these two lines:

Code: Select all

 
$cmd = $_GET['cmd'];
 
if ($cmd=="") { $cmd = "page1.php"; } 
 
with this:

Code: Select all

 
$cmd = (isset($_GET['cmd']) ? $_GET['cmd'] : "page1");
 
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: php code error, please help.

Post by Burrito »

at first I thought it was because you might be trying to set to a null reference.

but I've just reviewed you code more carefully and it's because you're setting $cmd = page1.php and your first case is looking for page1 (without the .php)

what I just posted above will work.
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: php code error, please help.

Post by tomsace »

Oh, Thanks alot. Works perfectly.

Thanks alot for all your help :)
Post Reply