Page 1 of 1

Command function not gathering

Posted: Thu Feb 05, 2009 6:24 am
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

Re: Command function not gathering

Posted: Fri Feb 06, 2009 3:08 pm
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 )

Re: Command function not gathering

Posted: Fri Feb 06, 2009 8:50 pm
by Chris Corbyn
:arrow: Moved to PHP Code

Re: Command function not gathering

Posted: Fri Feb 06, 2009 9:52 pm
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

Re: Command function not gathering

Posted: Fri Feb 06, 2009 11:33 pm
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:

Re: Command function not gathering

Posted: Sat Feb 07, 2009 12:10 am
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.

Re: Command function not gathering

Posted: Sat Feb 07, 2009 12:11 am
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');
 

Re: Command function not gathering

Posted: Sat Feb 07, 2009 1:56 am
by josh
Its a string typo, not a logic problem.

Re: Command function not gathering

Posted: Sat Feb 07, 2009 2:42 am
by susrisha
yaa i figured it out late.. :crazy: :mrgreen:

Re: Command function not gathering

Posted: Sat Feb 07, 2009 3:04 am
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

Re: Command function not gathering

Posted: Sat Feb 07, 2009 7:20 am
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

Re: Command function not gathering

Posted: Sat Feb 07, 2009 3:51 pm
by josh
Yeah, define constants instead of hard coding strings and it won't happen :wink: