Command function not gathering

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
ipodman
Forum Newbie
Posts: 3
Joined: Thu Feb 05, 2009 6:17 am
Contact:

Command function not gathering

Post by ipodman »

Hey guys,

Sorry if this post is in the wrong area, It seems like the correct one.

Currently on a project but I seem to have run into a problem. I am still rather new at PHP, learning by the day. Well here is my problem.

I have created an index.php file[see below] that will gather pages to show in the body of the site with a set header and footer. On first load of the site, it does not gather the body text file, untill you click a link.

For example. If I load the site, It only shows the header and footer and no body [home.php] but if I click a link for home.php it will load it in the body.

Here's the code.

Code: Select all

<?php
 
$cmd = $_GET['cmd'];
if ($cmd=="")  { $cmd="home.php"; }
 
@include('header.php');
 
switch($cmd)
{
 
case "home":
@include('home.php');
break;
 
case "login":
@include('login.php');
break;
 
case "register":
@include('register.php');
break;
 
}
@include('footer.php');
 
The use of:-

Code: Select all

$cmd = $_GET['cmd'];
if ($cmd=="")  { $cmd="home.php"; }
I would have expected to gather the home page if nothing else is selected

Thanks

-Dan
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Command function not gathering

Post by josh »

Yes wrong forum. This goes in PHP

# if ($cmd=="") { $cmd="'home.php"; }
(snip)
case "home":

ps instead of case home: you should look into the "default:" construct ( read php.net/switch )
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Command function not gathering

Post by Chris Corbyn »

:arrow: Moved to PHP Code
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Command function not gathering

Post by susrisha »

change

Code: Select all

 
$cmd = $_GET['cmd'];
if ($cmd=="")  { $cmd="home.php"; }
 
to this

Code: Select all

 
if(!isset($_GET['cmd']))
{
$cmd="home.php";
}
 
This way if there are no get parameters, it will set the $cmd to home.php
Other solution is to set a default to the switch as home.php by which any of the unknown will be sent to home.php
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Command function not gathering

Post by josh »

susrisha wrote:change

Code: Select all

 
$cmd = $_GET['cmd'];
if ($cmd=="")  { $cmd="home.php"; }
 
to this

Code: Select all

 
if(!isset($_GET['cmd']))
{
$cmd="home.php";
}
 
This way if there are no get parameters, it will set the $cmd to home.php
Other solution is to set a default to the switch as home.php by which any of the unknown will be sent to home.php
No reason to change that, in fact it has nothing to do with why its not working :roll: :roll: :roll: :roll: :banghead:
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Command function not gathering

Post by susrisha »

@josh
I didnt get it. He is trying to include the pages depending on what is got at $cmd. If there is nothing set as $_GET['cmd'], how can he include "home.php".

@ipodman
By the way i found out one more thing:
in the switch statement its comparing everything like case 'home', case 'login'

where as in the default assignment at the top its given as $cmd = 'home.php'

which will not come under any category. So obviously it displays only the header and footer and nothing else.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Command function not gathering

Post by susrisha »

Code: Select all

<?php
 
$cmd = $_GET['cmd'];
 
[color=#FF4000]if ($cmd=="")  { $cmd="home.php"; }[/color]
 
@include('header.php');
 
switch($cmd)
{
 
[color=#FF0000]case "home":[/color]
@include('home.php');
break;
 
case "login":
@include('login.php');
break;
 
case "register":
@include('register.php');
break;
 
}
@include('footer.php');
 
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Command function not gathering

Post by josh »

Its a string typo, not a logic problem.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Command function not gathering

Post by susrisha »

yaa i figured it out late.. :crazy: :mrgreen:
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Command function not gathering

Post by josh »

yup, it took me a minute to find, I saw what you were saying and thought that was the problem at first too, thats why I wrote :banghead: hah
ipodman
Forum Newbie
Posts: 3
Joined: Thu Feb 05, 2009 6:17 am
Contact:

Re: Command function not gathering

Post by ipodman »

susrisha wrote:@josh
I didnt get it. He is trying to include the pages depending on what is got at $cmd. If there is nothing set as $_GET['cmd'], how can he include "home.php".

@ipodman
By the way i found out one more thing:
in the switch statement its comparing everything like case 'home', case 'login'

where as in the default assignment at the top its given as $cmd = 'home.php'

which will not come under any category. So obviously it displays only the header and footer and nothing else.
Now I feel rather embarrassed by the mistake I have made, But it now works:-)

Thanks a lot susrisha for solving my typo problem and thanks to all who replied
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Command function not gathering

Post by josh »

Yeah, define constants instead of hard coding strings and it won't happen :wink:
Post Reply