Page 1 of 1

Making a simple GET php style template

Posted: Fri May 27, 2005 12:46 am
by Gappa
Hello all,

I am in the process of making a simple template system using PHP (the reason being to the website be easier to update when something needs to be changed or added to the Navigation Bar).

I am basically just using an IF statement and GET function.

Code: Select all

<?php 
	if ($_GET[‘Name’] == "")
 	{
    	include(“home.inc”);
 	}
	else
 	{
    	include(“$_GET[‘Name’].inc”); 
 	}
	?>
Ofcourse the links I have created will look something like: index.php?Name=NEWS_recent

I decided to use an IF statement so that the normal index.php would load up the home.inc file. However, this at the moment Is not working at all. The error I am recieving is as follows.

Parse error: parse error, unexpected T_VARIABLE in /home2/dordor/public_html/vta_test/index.php on line 264

LINE 264 Being: include(“$_GET[‘Name’].inc”);

If you guys could offer some help that would be great cheers :)

PS: I don't even know if the IF statement thing I am trying will even work as the varible might not even exist??

Posted: Fri May 27, 2005 1:55 am
by Gappa
Well I have changed my previous code, and not longer get the parse error :)

Code: Select all

<?php 
	if ($_GET[‘Name’] == "")
 	{
    	include 'home.inc';
 	}
	else
 	{
    	include '$_GET[‘Name’].inc'; 
 	}
	?>
I my newer code. It now loads the page and includes the home.inc file. However it always does this, so even If i click a link changing the url to index.php?Name=NEWS_recent it still includes the home.inc rather than NEWS_recent.inc so my if statement is probably out of wack.

Also I think the line

Code: Select all

include '$_GET[‘Name’].inc';
Still does not work, I have tried leaving that as the only line of php and it doesn't work. I got that line of code from a PHP&MySQL Manual (but it is for version 5 of PHP which I am not using). I have also tried using double quotes on the above line, pretty much gave the same effect.

Any help with my IF and INCLUDE/GET would be very appreciated. :)

Posted: Fri May 27, 2005 3:10 am
by phpScott
the . in php is used for concatination so in your first attempt you where adding .inc to the varaible and inc wasn't a variable.

In your second attempt you had used single quotes around everything and the single quote doesn't interpret php variables.

In the code below i moved the single quote and used the . to concatinate on '.inc'

Code: Select all

<?php 
    if ($_GET[‘Name’] == "")
     {
        include 'home.inc';
     }
    else
     {
        include $_GET[‘Name’].'.inc'; 
     }
    ?>

Posted: Fri May 27, 2005 3:32 am
by Gappa
ahh thanks alot Scott.

I still do believe my IF statement is all screwy

Code: Select all

if ($_GET[‘Name’] == "")
As when loading "index.php" the $_GET variable will not even be in exsistance? So checking if it is equaly to "" may not work, I am not even sure that this is the correct way to test it if it did exist.

What way should this be done?

Posted: Fri May 27, 2005 5:45 am
by Bennettman
The best way is to set $Name and then include it:

Code: Select all

// set $Name to either the input, or the default
$Name = ($_GET['name']) ? $_GET['name'] : 'home';
// tertiary operator (similar to IF-ELSE)
// "if ($var)" is the same as "if (!empty($var))"

include $Name . '.inc';

Posted: Fri May 27, 2005 5:50 am
by phpScott

Code: Select all

if(isset($_GET['Name']) && $_GET['Name'] != "")
{
   include $_GET[‘Name’].'.inc'; 
}
else
{
   include 'home.inc';
}
now if the $_GET['Name'] is there and its not equal to blank it should include the right page otherwise it will default to home.inc.

You might want to do some checking on $_GET['Name'] to make sure it is what you would be expecting.

Posted: Fri May 27, 2005 12:13 pm
by Skara
you guys write too many lines. :P

Code: Select all

include (isset($_GET['Name']) && $_GET['Name'] != '' ? $_GET['Name'] : 'home') . '.inc';
Of course, I'm insane about writing shorter code. ^^;

Posted: Fri May 27, 2005 12:19 pm
by JAM
Skara wrote:Of course, I'm insane about writing shorter code. ^^;
Well, then let me help you.

Code: Select all

include (!empty($_GET['Name']) ? $_GET['Name'] : 'home') . '.inc';

Posted: Fri May 27, 2005 1:56 pm
by Skara
haha, well while we're at it,

Code: Select all

include (empty($_GET['Name'])?'home':$_GET['Name']).'.inc';
:mrgreen:

Posted: Fri May 27, 2005 4:20 pm
by timvw
i like the version i can understand the fastest/easiest when i'm speedreading...

my points go to phpscott ;)

Posted: Fri May 27, 2005 6:13 pm
by Bennettman
Skara wrote:haha, well while we're at it,

Code: Select all

include (empty($_GET['Name'])?'home':$_GET['Name']).'.inc';
:mrgreen:
How about...

Code: Select all

include (($_GET['Name'])?$_GET['Name']:'home').'inc';
You know it makes sense ^_-