Web Design Forums

Welcome! Please register or log in: Forgot your password? Why register?
You are here: Web Design Forums » Programming Help » PHP » Creating a static page template RSS

Creating a static page template

This thread was started by poker158149 and has been viewed 827 times, and contains 11 replies, with the last reply made by dedmoroz.
Post Reply
1
View poker158149's reputation
Posted January 10 '10 at 10:07 PM
      Posts: 22
Hi everyone.

I was wondering, how can one create a page in PHP that can be used as a template for the entire site?

I.E.
When I wanna make a change to the copyright or the navigation on each one of the pages on my site, I don't want to have to go to each individual page to do so.
I want to be able to change one thing on the index.php and it changes on each page.

Like, I see some sites that do this:
site.com/index.php
site.com/index.php?page=about
site.com/index.php?page=contact
etc.

Is this the type of thing I'm looking for? And if so, how can it be done?

Thanks!

Advertisement Register for free to hide these ads and participate in discussions!

2
2 points at 100%
Posted January 10 '10 at 10:41 PM
      Posts: 33
The sites that do site.com/index.php?page=about get their content from a database. This allows more room on the server for actual files, so instead of creating a file for each page, they input the content on that page into a MySQL database and then use the site.com/index.php?page=about method. This is useful if you have tons of pages or content.

To do what you're wanting to do, you need to create a php file with the code in it you want (for example footer or navigation). Then in every page you want that file to appear, use the php include function to include that php file. For example:

I create a footer.php file with the copyright info, styling, etc. Then in index.php, about.php, and contact.php at the bottom of the page I put this line: <? include("footer.php"); ?>

That will display the code of footer.php in every page that you put the above line of code. Then if you want to edit your footer, you just have to edit footer.php and it will update on all pages.

3
View poker158149's reputation
Posted January 10 '10 at 11:25 PM
      Posts: 22
Wow, thank you very much.

Now, can I do the <? include("footer.php"); ?> on pages that use MySQL databases type pages or not?

4
2 points at 100%
Posted January 11 '10 at 04:44 PM
      Posts: 33
Yes. Those pages just store the code in a database table instead of a file. It's the exact same code, just in a different format. You could copy/paste code from a php file into a database and it would work using the ?page=about format. Lots of Content Management systems use this method, the most noticable is PHP Fusion. It stores all your custom pages in the database, then displays each page using www.site.com/viewpage.php?page_id=X format. You can potentially have hundreds of pages stored in the database, but only use one file (viewpage.php) to view them. That's what makes php awesome

5
View poker158149's reputation
Posted January 11 '10 at 05:39 PM
      Posts: 22
Haha that's awesome.
I'm really going to have to learn how to create a site like that.
And one more question. Or, clearing up.
The <? include("footer.php"); ?> code: does this go inside or outside the <html> tags on the php page?
And what exactly do I put in "footer.php"? Like, do I start it with some php code? Do I have to include <html> tags again? How to I set it up?

Thanks for your help so far!

6
2 points at 100%
Posted January 11 '10 at 05:54 PM
      Posts: 33
PHP code can go anywhere in the page most of the time, I usually put mine in the <body> section. But for things like restricted access pages for registered users only, you should put the restrict code at the very top, even before your <html> tag. On my site I'm including the footer. The footer file can either be a complete html file or just a div, it works either way. Here's my footer.php:

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<style type="text/css">
A.footer:link {
text-decoration:none;
color:#14A1FF;
font-size:1.1em;}
A.footer:visited {
text-decoration:none;
color:#14A1FF;}
A.footer:active {
text-decoration:none;
color:#14A1FF;}
A.footer:hover {
text-decoration:none;
color:#75C7FF;}
</style>
</head>
<body>
<div>
<center><a class="footer" href="about.php">about</a> | <a class="footer" href="faq.php">faq</a> | 
<a class="footer" href="/help">help</a></center>
</div>
</body>
</html>
Now I did it that way because I needed a stylesheet for my footer, and I didn't know if the stylesheet would work if it wasn't in the <head> portion and I didn't have time to experiment . But you could just have a file with <div id="footer">copyright &copy; 2010</div> in it, include it, and it would display that line of code.

I set up a test website that used the ?page=page format a while before, letmme see if I can find it... If I can I'll post the code.
Last edited January 11 '10 at 05:55 PM by Unknown98 ("Spelling... ugh"). Reply

7
View poker158149's reputation
Posted January 11 '10 at 06:24 PM
      Posts: 22
Thank you so much for all of your help.
And if you do find that code, I'd be even more appreciative :P

8
2 points at 100%
Posted January 13 '10 at 03:42 PM
      Posts: 33
I think I deleted or overwrited it, sorry. I did find a tutorial on creating these kinds of pages though that may help you. You should be able to skip the steps where you install apache, mysql and php on your server, your host should already have these installed (You couldn't really create a website without them).

http://www.codewalkers.com/c/a/Datab...h-PHP-MySQL/1/

If that doesn't help much (I didn't really read through all of it), I may be able to write something up to show you.

9
View poker158149's reputation
Posted January 13 '10 at 05:54 PM
      Posts: 22
That's alright. Thanks so much for the link, I'll definitely look into it.
Thanks for your time!

10
2 points at 100%
Posted January 16 '10 at 11:28 AM
      Posts: 33
Erm, kinda off-topic me thinkies.

11
2 points at 100%
Posted January 23 '10 at 10:30 PM
      Posts: 33
Hey, I found those codes if you still want them. First block is just the css stuff. Second block is the first page (say index.php). Third block is the second page (say test.php). Ask me if anything's not working. And of course you can fetch the id number on the second block from a database, you don't have to manually name it id=1.

.container {
    border: 1px solid black;
    width: 80%;
    background-color: #FBFBFB;
    padding: 5px;
    margin: 0 auto;
}
.footer {
    width: 100%;
    background-color: #E8F4F9;
    text-align: center;
}


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<title>Test</title>
<link rel="stylesheet" type="text/css" href="default.css" /
</head>
<body>

<div class="container">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod 
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu 
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in 
culpa qui officia deserunt mollit anim id est laborum.</p>

<a href="test.php?id=1">Test</a>

<div class="footer">
<p>Copyright &copy; 2009. All Rights Reserved.</p>
</div>

</div>

</body>
</html>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<title>Test</title>
<link rel="stylesheet" type="text/css" href="default.css" /
</head>
<body>

<div class="container">

<?php
echo $_GET['id'];
?>

<div class="footer">
<p>Copyright &copy; 2009. All Rights Reserved.</p>
</div>

</div>

</body>
</html>

12
View dedmoroz's reputation
dedmoroz, WDF Noob Private message  
Posted January 27 '10 at 02:37 PM
      Posts: 2
I recommend to visit this resource http://phpforms.net/tutorial.html

Post Reply

Similar Threads
Thread Thread Starter Forum Replies Last Post
simple way to get a dynamic php page to autogenerate a static html one? Umfanekiso PHP 6 August 30 '08 08:44 PM
Need Help Creating a name and image upload page buckaholic2000 General Design Discussion 3 August 7 '08 11:05 PM
Page Error loading --- Memory issue? Janice HTML and CSS Help 3 February 27 '07 12:31 AM
Using Variables From Include Page. kleptos PHP 8 April 22 '03 04:30 PM