Getting Started With PHP and Dynamic Content
If you have taken any basic HTML courses or look through tutorials you know how to build a website…. Well sort of. HTML is pretty much the first and main stepping stone of websites but it is merely the beginning. In this post I will show you a few techniques to get you stated in using PHP to make your sites more dynamic and much easier to manage.
Lets take for example you have an information site about carving spindles. Your site consists of a home page, an about us page, a contact us page, and a content page that has the instructions on creating the spindles. This is a small site and your knowledge currently is mostly only on HTML and CSS but you are tired of having to go and edit something on all 4 pages to simply make one change on the site.
Here is your current home page
Obviously an actual site will have much more than this but this is a very basic setup. Now, as you can see the content that falls between the “header” div and the “footer” div is content that is going to be on every site. So say you wanted to change the “Tutorial” navigation to read “How To Carve Spindles”, you would have to open each file and change the text in each individual one. In this example only having 4 pages to your site it obviously would not be a huge deal but if you had 20+ pages it would be awful. So to avoid having to make multiple changes we can use PHP include(). PHP include does exactly what it says. It will include the file that you tell it to and the webserver will display it as one file. This way you can span one simple file across as many pages as you like.
Let me break this down to show you what I mean exactly. Lets first create our header.html. To do so we will want to take all of the upper code that we want to appear on each page of the site.
header.html
As you can see we did the exact same thing here. We started with the closing (
) div for the content div which is in the header.html. One thing that you always need to watch for when doing this is always have the same count of divs in the header as you do in the footer. This can get a little tricky if you have a design that has a lot of nested divs. Ok so now you might be wondering how exactly we would use this. So below will be our pages for this site.
index.php
php
include('header.html');
?>
All your information on the home page.
php
include('footer.html');
?>
tutorial.php
php
include('header.html');
?>
All your information on the tutorialpage.
php
include('footer.html');
?>
about-us.php
php
include('header.html');
?>
All your information on the about us page.
php
include('footer.html');
?>
contact-us.php
php
include('header.html');
?>
All your information on the contact us page.
php
include('footer.html');
?>
See now with this structure we no longer would have to change something on each page to be able to change something on each page. Say we have the first example of wanting to change the “tutorial” in the navigation to “how to carve spindles” then we would only have to change the header.html like such.
header.html
- Login om te reageren