OK, so for the first time I am building a full website and working with php. Now I encounter a problem:
Let’ss say my site is simply has a header with a menu and a area with content. Of course, I would like to have a header.php and several content files like content1.php content2.php and content3.php for example. This way you only have to change the menu in 1 file as you would understand.
How then is it best to build the site:
A. put something like this in every content file:
<?php include 'header.php'; ?>
here the content of the content page 1
B. make an index file with something like:
<?php include 'header.php'; ?>
<?php include 'content1.php'; ?>
how then is it done that when in the menu the link to content2.php is clicked the header is still on that page too?
C. something else? Maybe a good tutorial on how to make these kind of pages?
If you’re looking to maximize the benefit of reusing code/elements, then you’re on the right track with your second option:
Here’s how (a simplistic example):
Route all of the similar requests (content1,2,3) through your
index.phpscript using the query string –mod_rewritecan make this pretty. Then serve the main content section based on the request.For example a link:
And detecting the content to serve:
Not only is there a single place to change your header, but now there’s a single place to change your entire layout.
Of course this is just a simplistic example, there are many PHP frameworks that do all this for you (using MVC).