PHP Include Question

Status
Not open for further replies.

sickness

In Runtime
Messages
458
So on my main page, index.php, I have a PHP news script installed, FusionPHP, and the news displayed useing a simple PHP include code. I want to know how I can add other pages now, useing the "index.php?page=pageid" form. It's the best I can describe it as. Below is a link to my site.

Right now I have it set up useing Frames, but this is just until I can get the PHP working.

http://www.gamercontrol.com

Thanks.
 
All you need is a file called index.php which contains the following code:
Code:
<?php
   include(pagesArray[pageid]);
?>
where pagesArray is an array containing the paths to the pages - you may want to use a database here instead, but it is easier to explain with an array.

the array would look something like:
Code:
<?php
   pagesArray[0] = 'some/path/page.htm';
   pagesArray[1] = 'another/path/index.htm';
?>

and would need inserting in the page above the include statement. now the address:
"http://www.example.com/index.php?pageid=1"
will load the page 'another/path/index.htm' into /index.php.

However you wont be able to do this if register_globals is disabled (it is now disabled by default with php). register globals is what enables the passing of variables in the URL. To enable it go into your php.ini file and enable it. Of course if you dont have access to the php.ini file then you wont be able to do this.
 
However you wont be able to do this if register_globals is disabled (it is now disabled by default with php). register globals is what enables the passing of variables in the URL.

Thats wrong. You can still use this kind of naviagation with no problem. But you can't access the varible with $pageid you have to use $_GET['pageid']

The most common way this is done, is to with a switch statement.

PHP:
switch($_GET['pageid']
{
case "top10":
include("top10.php");
break;
case "chat":
include("chat.php");
break;
default:
include("main.php")
break;

}

And then you would form a link like this

Top 10 of E3

If you really wanted to use numbers for the page idea you would add an array into the middle like the prevouis poster did with the same swith structure but instead using

include(pagesarray[$_GET['pageid']);
 
The most common way this is done, is to with a switch statement.

I think there may be an easier way, otherwise, if you have a billion pages, its a bit hard to keep adding switch statements

Code:
<?php
if ($_REQUEST[pageid]) {
      require "$_REQUEST[pageid].php";
}
?>

That way, every time you need to add a new page you dont need to add another switch statement.

You link to it the same way.

Top 10 of E3

Hope that helps
 
Status
Not open for further replies.
Back
Top Bottom