Page 1 of 1

Structuring a site with PHP

Posted: Mon Jun 30, 2003 12:53 am
by griz
I'm currectly working on a new project just starting out with PHP. I am alittle confused on how to structure my website with directories and files. I'm interested in figuring out the most logical way to structure my site.

I noticed some sites link there sections like this:

index.php?topic=home
index.php?topic=parts
index.php?topic=contact

If the pages are linked as above, then how would you add another variable to the URL?

Some sites have completely different files like:

index.php
parts.php
contact.php

and other have:

/index.php
/parts/
/contact/

how would one go about making each page look the same for each of this examples? Would they have to have a file that includes each page separately like this:

<? include("header.php"); ?>

<? include("page_to_include.php"); ?>

<? include("footer.php"); ?>

Do most sites set it up like this? If this is true then if you wanted to include say a control_panel.php on every page you would have to go back to each individual one and paste in: <? include("controlpanel.php"); ?>. THERE MUST BE AN EASIER WAY.
Any info would be great.
index.php contains:
<? include("header.php"); ?>
<? include("home.php"); ?>
<? include("footer.php"); ?>

parts.php contains:
<? include("header.php"); ?>
<? include("parts.php"); ?>
<? include("footer.php"); ?>

contact.ph contains:
<? include("header.php"); ?>
<? include("contact.php"); ?>
<? include("footer.php"); ?>

Posted: Mon Jun 30, 2003 12:58 am
by qartis
Quite often, you'll see sites do something like this:

index.php

Code: Select all

<?
include ("header.php");
switch ($_GET['page']){
    case "forums":
        include ("forums.php");
        break;
    case "articles":
        include ("articles/index.php");
        break;
    default:
        include ("index2.php");
        break;
}
include ("footer.php");
?>
Then call the pages like index.php?page=articles, index.php?page=forums. A major downside to this, is that header.php is already included by the time articles.php is executed, so if it determines that it needs to redirect the user, it can't send any more headers. There's always output buffering, but that should be reserved as a last resort, or a quick fix.


Oh, and if you wanted to add $user_id to index.php?page=articles, you'd do it in the form "index.php?page=articles&user_id=yourname"

Posted: Mon Jun 30, 2003 1:10 am
by trollll
I prefer to use a mix of the two, importing the navigation based on the capabilities of the browser/os but keeping each section a separate php page. Kind of like implementing server-side style sheets for content rather than layout. I just find it much easier to manage content, scripts and permissions that way.

Posted: Mon Jun 30, 2003 1:51 am
by griz
Thanks qartis!! That solved my problem. Do you know anything about implementing something like that with a directory structure?

Also I found this other post of a similar topic and someone said to use this:

<?php include("$page.php"); ?>

then call the page by index.php?page=whatever

What would the differences be between this method and the switch statement.

THanks

Posted: Mon Jun 30, 2003 2:40 am
by qartis
the switch statement allows you to use index.php?page=2, or ?id=2. Also, if register_globals is turned off, then your little snippet won't work, because $page won't equal ?page. The only surefire way to access variables passed in the url is to use the $_GET array, like $_GET['page'].

Posted: Mon Jun 30, 2003 3:20 am
by Drachlen
Well, ive never used the switch thingermajig, but i use an if statement per page, and sometimes include other pages. ill make an example

Code: Select all

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

if($p == 'News') {
echo "NEWS";
}
if($p == 'FAQ') {
echo "FAQ";
}
?>
Theres the basic setup. You'd probably also want a if($p == '') { statement, so you can forward them to the new page or whatever. Normally, i would write out my html document, and then start the php where ever i want to display it(ie: in the middle of the table that contains all the informations). So above or below this PHP throw in some links like <a href=Index.php?p=News>News</a>-<a href=Index.php?p=FAQ>FAQ</a>
This is how i do it atleast

Posted: Mon Jun 30, 2003 4:48 am
by m3mn0n
qartis wrote:Quite often, you'll see sites do something like this:

index.php

Code: Select all

<?
include ("header.php");
switch ($_GET['page']){
    case "forums":
        include ("forums.php");
        break;
    case "articles":
        include ("articles/index.php");
        break;
    default:
        include ("index2.php");
        break;
}
include ("footer.php");
?>
Then call the pages like index.php?page=articles, index.php?page=forums. A major downside to this, is that header.php is already included by the time articles.php is executed, so if it determines that it needs to redirect the user, it can't send any more headers. There's always output buffering, but that should be reserved as a last resort, or a quick fix.


Oh, and if you wanted to add $user_id to index.php?page=articles, you'd do it in the form "index.php?page=articles&user_id=yourname"

I love that method and i recommend it for structure.

Just a note about it though:

You don't have to have index.php there to make it work. So yoursite.com/?page=home would be the same as yoursite.com/index.php?page=home

:)

Posted: Mon Jun 30, 2003 4:52 am
by m3mn0n
Drachlen wrote:Well, ive never used the switch thingermajig, but i use an if statement per page, and sometimes include other pages. ill make an example

Code: Select all

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

if($p == 'News') {
echo "NEWS";
}
if($p == 'FAQ') {
echo "FAQ";
}
?>
One of the reasons the switch() function is so great is for cases like this. Instead of making a new if for every single page, you have 1 switch() function and many cases'. Plus if you're using a DB to generate links/text for the site, displaying an array of contents into a switch() function is much easier than using the if() way. 8)