View Full Version : yourpage.php?page=blah


tail451
01-08-2006, 04:35 PM
How do I get my pages like that?

putnamehere
01-08-2006, 05:07 PM
Well it's done in php

<?
$name = $_GET['IT']
?>

were IT is the name of what you want in the address
then take someone to the page like this
page.php?IT=this

The general idea is that impotant data is being sent via the url and it is then used on the page.
so thers not much point useing it for show.
if you are jsut useing it to for your site to look good then jsut creat a normal php page put html in it and send people to
page.php?this=this&that=that
coz thers no point useing the code above if it isent needed

Merike
01-08-2006, 06:56 PM
If you're still interested in it then here's a tutorial: http://codegrrl.com/tutorials/php/dynamic_includes.php

djou
01-08-2006, 08:15 PM
The way I have done it, supposing your file is called index.php

<?php
$page = $_GET['page'];

if ($page == "something") { ?>
content of index.php?page=something

<?php } elseif ($page == "other") { ?>
content of index.php?page=other

<?php } else { ?>
content of index.php as well as any index.php?page= followed by something that was not defined, like index.php?page=stuff

<?php } ?>

bejayel
01-08-2006, 11:30 PM
a way to make this even better is to do somethign liek this:

<?
$page = $_GET['page'];

if($page == "page1")
include('page1.html');
elseif($page === "page2")
include('page2.html');
else
include('home.html');
?>

If you do it that way, then you can easily modify page1 without having to search through all of your code for other pages. It also lets you do somehtign called skinning, and allows for you to EXTREMELY easily change layouts.

The reason for being able to change layouts easily is because you use the includes for nothign but simple documents or simple layout changes, or including other scripts. yoru index.php will have all of th elayout information, and then the others will jsut have page information.

I generally have a include for the main body text and the links. Some people have header and footer, and all of that garbage...

Tracey
01-08-2006, 11:41 PM
Well here's what I use:<?
if($page) { include("$page.php"); } else { include("mainpagehere.php"); }?>

nice and simple

harmor
01-09-2006, 12:01 AM
I wouldn't suggest "shockertwin009" or "Tracey" way unless your code is long within the if or else statement.

"djou" way is the way to write it but I have another way.

save this as "test.php"
<?php

//if user types "test.php" then show main page
if(empty($_GET['page']))
{
$_GET['page'] = "main";
}

//if the conditional above is true then show the code between this statement
if($_GET['page'] == "main")
{
echo "this is the index";
}

//display content when user types "test.php?page=page2"
if($_GET['page'] == "page2")
{
echo "this is a sub-page for \"page2\" ";
}

//if user types "test.php?page=info"
if($_GET['page'] == "info")
{
echo "this is a sub-page for imformation about me";
}

?>

Douglas
01-09-2006, 12:09 AM
There is another way:


<?php
$pagename=(isset($_GET['p']) ? $_GET['p'] : 'index');
include($pagename.".php");
?>


Then you can just have it so if you went to:

page.php?p=bleh

It would include bleh.php which you have made. I set the default one in that script to index ;)

bejayel
01-09-2006, 05:16 AM
I wouldn't suggest "shockertwin009" or "Tracey" way unless your code is long within the if or else statement.

"djou" way is the way to write it but I have another way.

save this as "test.php"
<?php

//if user types "test.php" then show main page
if(empty($_GET['page']))
{
$_GET['page'] = "main";
}

//if the conditional above is true then show the code between this statement
if($_GET['page'] == "main")
{
echo "this is the index";
}

//display content when user types "test.php?page=page2"
if($_GET['page'] == "page2")
{
echo "this is a sub-page for \"page2\" ";
}

//if user types "test.php?page=info"
if($_GET['page'] == "info")
{
echo "this is a sub-page for imformation about me";
}

?>

All of yoru if's should be elseif's. Otherwise you are using unnecessary, useful processing power.

Though i do agree, dont use includes unless you are breaking like 30-40 lines of code. My includes are all like 100-1000 lines of code so i have a decent reason to use them. Both my links and news use a script tha ti wrote myself, so it easier to manage them seperately.

harmor
01-09-2006, 04:59 PM
I'm not sure if using "elseif" will increase the performance.
The server still has to read over the entire file when it's called.

Another way to write it is use switch with or without functions.
IMO I like functions because it's cleaner.
This method will take more processing time but not a lot.

In this tutorial I will explain how to make use the switch statement and functions.

Note: everything will be in one file.


Save as test.php
<?php

switch($_GET['act'])
{
default:
index();
}

function index()
{
echo "Text goes here";
}
?>


See $_GET['act'] What this will do is add ?act after the php name (test.php?act). If you would like it to have test.php?lala just replace "switch($_GET['act'])" with "switch($_GET['lala'])".

Notice index(); This is being called by function index()

Now I'll show you how to get another page

<?php

switch($_GET['act'])
{
case "otherpage":
otherpage();
break;
default:
index();
}

function index()
{
echo "Text goes here <a href='test.php?act=otherpage'>";
}

function otherpage()
{
echo "This is my other page";
}

?>


Notice this section case "otherpage": This is put ?act=otherpage in the url. You can change the name to whatever you prefer.
If I wanted to change it to my name then I just do case "harmor":
Below the "case" I am calling a function with otherpage();
You can change the name of this as well just make you have the (); at the end and you rename the function it's calling. In this case it's calling function otherpage()

Here is the whole thing with another case

<?php

switch($_GET['act'])
{
case "otherpage":
otherpage();
break;
case "harmor":
harmor();
break;
default:
index();
}

function index()
{
echo "<a href='test.php'>Home</a> <a href='test.php?act=harmor'>harmor's page</a> <a href='test.php?act=otherpage'>Other Page</a>
<br /><br />
Text Goes here";
}

function otherpage()
{
echo "This is my other page <a href='test.php'>Home</a> <a href='test.php?act=harmor'>harmor's page</a> <a href='test.php?act=otherpage'>Other Page</a>
<br /><br />
Text goes here";
}

function harmor()
{
echo "<a href='test.php'>Home</a> <a href='test.php?act=harmor'>harmor's page</a> <a href='test.php?act=otherpage'>Other Page</a>
<br /><br />
Text goes here";
}

?>



If you want to use the links in a variable then you would need to use global
notice the variable for the links and after "global"

<?php

$links = "<a href='test.php'>Home</a> <a href='test.php?act=harmor'>harmor's page</a> <a href='test.php?act=otherpage'>Other Page</a>";

switch($_GET['act'])
{
case "otherpage":
otherpage();
break;
case "harmor":
harmor();
break;
default:
index();
}


function index()
{
global $links;

echo "$links <br /><br /> Text Goes here";
}

function otherpage()
{
global $links;

echo "$links <br /><br /> Text Goes here";
}

function harmor()
{
global $links;

echo "$links <br /><br /> Text Goes here";
}

?>

tail451
01-11-2006, 10:03 PM
I used this:
<?
if (isset($_GET['page']) && isset($_GET['id']) && file_exists('mods/'.$_GET[id].'/'.$_GET[page].'.php')) {
include 'mods/'.$_GET[id].'/'.$_GET[page].'.php';
}
else {
echo "Page Not Found";
}
?>

Douglas
01-11-2006, 10:16 PM
That looks fine, except make it like this:


<?
if (isset($_GET['page']) && isset($_GET['id']) && file_exists('mods/'.$_GET[id].'/'.$_GET[page].'.php')) {
include("mods/".$_GET['id']."/".$_GET['page'].".php");
}
else {
echo "Page Not Found";
}
?>