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.
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;
<?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?
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]
<?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
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 ) but you can just as easily adapt that to using includes, flat content stored in arrays or even strings.
<?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.
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.