PHP Template

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mayajewe
Forum Newbie
Posts: 13
Joined: Sun Feb 01, 2009 12:56 pm

PHP Template

Post by mayajewe »

Hi,

I hope I am in the right area for this question, I don't need any code I just need to understand exactly how something works. I have followed 2 tutorials on creating PHP forms and I still don't think that I understand the theory. I have 3 content.php pages and all I want is for them to use the same header and menu so that any updates I make to the header or menu will be applied to all of my pages from one place.

I created :
head.html (my header)
menu.html (left navigation menu)

then I created
page1.php
page2.php
page3.php

(which had the template information and an includes statement which pointed to one of my 3 php content pages.)

content1.php
content2.php
content3.php

This is what I understood of the tutorial but is it right? It doesn't seem right that I have created one php file with an includes statement and all the template information for each content.php file.

How does it work? What should my approach be?

Any suggestions very welcome, I only started using PHP last week.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: PHP Template

Post by susrisha »

okay here is what i understood from your post.

You have all the headers in head.html (my header)
Your menu written in menu.html (left navigation menu)

Now you want to write three pages page1.php, page2.php, page3.php each containing head, menu and also some content which you have typed in content1.php, content2.php and content3.php respectively.

What you are trying to follow is include head.html and menu.html in each of the page.php files and then include the content.php files. That will actually suffice to what you are trying to do..

page1.php

Code: Select all

 
<?php
include_once("head.html");
include_once("menu.html");
include_once("content1.php");
?>
 
so further if you have to change anything in the head area, just change the head.html file and it will be effected in all the files that include the head.html file.

Divide and conquer buddy.. ;)
asaini09
Forum Newbie
Posts: 4
Joined: Sat Feb 07, 2009 1:08 pm

Re: PHP Template

Post by asaini09 »

Hey,

I have a similar problem. Except I'm trying to do something slightly different.

I've created a layout in the main file - index.php
Now what I want to do is have ONLY the content area change.
I have seen many websites do it... and the url usually looks like this:

http://www.mywebsite.com/index.php?page=aboutus
http://www.mywebsite.com/index.php?page=contactus
http://www.mywebsite.com/index.php?page=downloads
etc.

How do I go about setting it up in the same way?
Seems like that one index.php calls on the other pages to its content area.

I've tried using a few tutorials but I can't quite seem to get it right.

Any help is appreciated!
mayajewe
Forum Newbie
Posts: 13
Joined: Sun Feb 01, 2009 12:56 pm

Re: PHP Template

Post by mayajewe »

Thanks for the help susrisha,

The tutorials had an index.php file that had the template info and an includes statement pointing to the default home text. Because of this I thought that the index.php was the only template file and somehow all the pages were routed through it. I now have 3 template pages all .php and I have typed the content directly into them and used include to get the header and menu into them. So instead of 6 pages I now have only 3 which seems better and it does works. I thought that the tutorial I was following worked in the way asaini09 was talking about but it wasn't that is why I was a bit lost. I would be very interested finding out the way that asaini09 wants to create the templates as this seems like it would require less repetition of code and may be better for me too.
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: PHP Template

Post by Skoalbasher »

asaini09 wrote:Hey,

I have a similar problem. Except I'm trying to do something slightly different.

I've created a layout in the main file - index.php
Now what I want to do is have ONLY the content area change.
I have seen many websites do it... and the url usually looks like this:

http://www.mywebsite.com/index.php?page=aboutus
http://www.mywebsite.com/index.php?page=contactus
http://www.mywebsite.com/index.php?page=downloads
etc.

How do I go about setting it up in the same way?
Seems like that one index.php calls on the other pages to its content area.

I've tried using a few tutorials but I can't quite seem to get it right.

Any help is appreciated!
This may be stupid, so correct me if I'm wrong. But couldn't you do something like that with Iframes?
asaini09
Forum Newbie
Posts: 4
Joined: Sat Feb 07, 2009 1:08 pm

Re: PHP Template

Post by asaini09 »

I believe it is possible with iFrames, however i don't think it offers the same flexibility as a php template. For example, if I was to change the layout, then I would need to change each of the pages that I call as an iFrame to match background colours, etc. of the new layout. Whileas with php, I change the layout on index.php, and my content will still appear accordingly.
In any case, I'm sure there are advantages and disadvantages of both and so I appreciate the suggestion :D

But for now, I need a way to have one file - index.php. And then have separate files which go into the content area.

Thanks for the continuing help guys!
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: PHP Template

Post by susrisha »

the design of php as said by asaini09 like index.php?page=something is a kind of redirect one..
the index.php will get the value of the requested page like $page =$_GET['something'] and then redirects the request accordingly. This can be done in two ways. Store the individual pages in php and redirect them accordingly like

Code: Select all

 
switch($page):
{
case 1: 
//redirect to page1
case 2:
redirect to page 2
default:
//stay in the same page
 
}
 
there is also another design by which the content is all stored as a page field in mysql and the content is accessed

Code: Select all

 
 
$content = mysql_query("select content from page_contents where page = 'pagegiven'");
 
//then print the whole content into the page.
 
 
if you need more info on any of these designs post me back
mayajewe
Forum Newbie
Posts: 13
Joined: Sun Feb 01, 2009 12:56 pm

Re: PHP Template

Post by mayajewe »

Those two examples are really useful, I have a basic wCMS system that allows the webpage content to be updated (at the moment it consists of a wysiwyg textarea which reads and writes to an mySQL db.) I think that your second example would be the best to get text out of the database and onto my pages.

Just two questions:

1. In the first option which uses case. I want the page contents to change depending on the menu item selected, how do I get the menu item to pass on the information to the case code?

2. In the second option. The page content is rarely going to be updated. (perhaps once a month for the home page and once a year for the others.) Is there a better approach than using an SQL query every time someone accesses a page, when that page is not changing very often? (I have used a wCMS system so that the owners of the site can make the updates themselves as they are not very IT literate.)
asaini09
Forum Newbie
Posts: 4
Joined: Sat Feb 07, 2009 1:08 pm

Re: PHP Template

Post by asaini09 »

Hey,

I think I'm looking for something similar to the first option you mentioned.

Here's what I had so far from a tutorial that I used

Code: Select all

 
<?php
 
//default include file
$page="p0.php";
 
//get input variable
if($_GET['p']){
$page="p".$_GET['p'].".php";
}
 
//check to make sure the file exists
if (file_exists($page)) {
// OK, the file exists
} else {
// file does not exists, so put back to default
$page="p0.php";
}
?>

Then in the content area, I put

Code: Select all

<?php include($page);?>
 
So this results in urls such as http://www.mywebsite.com/index.php?p=1
So this results in urls such as http://www.mywebsite.com/index.php?p=2
etc.

However, I do not want to be putting in my content files as numbers (i.e. 1, 2, 3, etc.). Instead like I mentioned previously I want to have an actual filename (i.e. something.php) that has the content. How do I adjust my coding to have that instead of p=1,2,3?

Thanks again!
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: PHP Template

Post by watson516 »

asaini09 wrote:

Code: Select all

 
<?php
 
//default include file
$page="p0.php";
 
//get input variable
if($_GET['p']){
$page="p".$_GET['p'].".php";
}
 
//check to make sure the file exists
if (file_exists($page)) {
// OK, the file exists
} else {
// file does not exists, so put back to default
$page="p0.php";
}
?>

Then in the content area, I put

Code: Select all

<?php include($page);?>
 

If you don't want to use p#.php, you simply put in the filename into the GET variable and omit that first if statement and replace it with an assignment statement to get $page to equal the GET value.

Code: Select all

<?php
 
//default include file
$page="main.php";
 
//get input variable
if($_GET['p']){
$page=$_GET['p'];
}
 
//check to make sure the file exists
if (file_exists($page)) {
// OK, the file exists
} else {
// file does not exists, so put back to default
$page="main.php";
}
?>
Or...

You could simplify all that with the simple switch statement as shown above.

Note:
Use [ code=php]...[ /code] when posting php to get syntax highlighting
asaini09
Forum Newbie
Posts: 4
Joined: Sat Feb 07, 2009 1:08 pm

Re: PHP Template

Post by asaini09 »

Thanks for the information susrisha!

And your modification worked absolutely perfectly watson516 .... exactly what i wanted .... thanks! Much appreciated!

Thanks to everyone else in this thread as well! :D
Post Reply