Creating pages using php?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
kevin_newbie
Forum Newbie
Posts: 2
Joined: Mon Aug 14, 2006 2:59 am

Creating pages using php?

Post by kevin_newbie »

Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello,

I am new to this forum. I really like how this community is involved.

So here is my questions;

1) I have done work in php, but i want to know how do you guys normally set up your site for a 15-20 webpages (Your folder directories and how you link your pages). Like i normally would do a folder called php and i make a footer.php, header.php and so on. Then i add one more folder called a query.php and in that page would have stuff like this;

Code: Select all

<?php 
// This is a very basic query script.
// To make this work you would include this query.php on the index.php where you want your content to go.
// the links for your buttons would look like this index.php?id=idhere
// after index.php?id= you would put the ID name. for example, the about me page would be index.php?id=aboutme

// This one looks diffrent from the rest for a good reason!
// i cant go into if and or statements now. but they will be covered in that oreilly book!
// this one looks for an id in the address. if there is no ID(just index.php) it would load the home id. which is simply home
if (($_GET['id'] == "home") or ($_GET['id'] =="")) {
include("pages/home.php"); // i dont need to explain that one 
}

// now these dont have the or statement... so these only load if you call them.
// this one would be index.php?id=aboutme
if($_GET['id'] == "aboutme") { 
include("pages/aboutme/aboutme.php"); 
}

// index.php?id=webdesign
if($_GET['id'] == "webdesign") { 
include("pages/webdesign/webdesign.php"); 
}

// index.php?id=portfolio
if($_GET['id'] == "portfolio") { 
include("pages/port/port.php"); 
}

// index.php?id=rssfeed
if($_GET['id'] == "rssfeed") { 
include("pages/rssfeed/rssfeed.php"); 
}

// index.php?id=contact
if($_GET['id'] == "contact") { 
include("pages/contact/contact.php"); 
}

// index.php?id=conformation
if($_GET['id'] == "conformation") { 
include("pages/contact/conformation.php"); 
}

// index.php?id=credits
if($_GET['id'] == "credits") { 
include("pages/credits.php"); 
}
// also note that you can change the ID to anything... like if you wanted index.php?page=credits you would change each string like so
// if($_GET['page'] == "credits") { 
// instead of if($_GET['id'] == "credits") { 
// but for this id works fine 
?>
Is there an easier way of doing all of this setup. Please give me suggestioin on how you guys do it when you first create the page using php?

2) How can I shorten my url to look something like this http://myurl.com/contactus.php instead of this long url http://myurl.com/index.php?id=contact?
or even instead of the php for the short url do it like .html instead?

Please bare with me, I am still new to this kind of stuff I just want to make my work load simple as possible.

Thanks,

Kevin


Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Search the boards for "front controller".

At the very least, use a switch();

Code: Select all

switch ($_GET['page'])
{
    case 'about':
        include 'about.php';
    case 'help':
        include 'help.php';
    default:
        include 'default.php';
}
kevin_newbie
Forum Newbie
Posts: 2
Joined: Mon Aug 14, 2006 2:59 am

Post by kevin_newbie »

I search teh boards for "front controller" but i am not sure which one to take a look at. Can you please give me a link to look at?

What does the switch() do?

Thanks,

kevin_newbie
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Or use includes for the common elements of your site, like the header, footer and navigation. Something like:
Home.php:

Code: Select all

<?php
include ($_SERVER['DOCUMENT_ROOT'].'/inc/header.php'); 
?>
<div id="content">bla bla homepage</div>
<?php
include ($_SERVER['DOCUMENT_ROOT'].'/inc/footer.php'); 
?>
Then in header you have your top section and navigation for example.

If you take it a step further you can have custom elements even in the shared parts. Like a specific title for each page:
Home.php

Code: Select all

<?php
$pageTitle = 'home';
include ($_SERVER['DOCUMENT_ROOT'].'/inc/header.php'); 
?>
...
Header.php:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
  <title><?php echo $pageTitle; ?></title>
... etc
Combine this with some rewrite with htaccess and you have an easy to manage site with nice urls.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

If the content/display on each page is not dramatically different, you can use one page and pump all the content through it. That is how I build my front controllers. Basically, your script finds out what the user wants based on $_GET['page'], then serves that up. I usually have my content coming from a database and have it fill a template, so everything is uniform and there is not code spaghetti (mmm, spaghetti :lol:) but you can just as easily adapt that to using includes, flat content stored in arrays or even strings.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Here is an example of a simple front controller..

Code: Select all

<?php
$inc_path = '/var/www/mysite/html/includes/';
if (!empty($_GET['page']))
{
    if (file_exists($_GET['page']))
    {
        include $inc_path . $_GET['page'] . '.php';
    } else
    {
        include $inc_path . 'default.php';
    }
}
?>
The php manual wrote:The switch statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for.
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

Ok,

You should read more about fron controller pattern. The idea is not making a lot of if's or switching to get what you need, but to get from start what you need.

For example if your URL is looking: http://www.domain.com/contact/view you may consider to create a controller called contact that has implementation for action view. So where you are implementing the controller (using also another pattern called factory) your request should look something like:

$objDispatch->('contact/view');

And the method dispactch will have the implementation for the factory pattern.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

That might be a little advanced, in terms of the pattern references. How about you throw out an example?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

There is a very simple Front Controller (that I think I originally created) in the post below. It might work for you.

viewtopic.php?t=46498&start=0
(#10850)
Post Reply