I’ve made my sites’ template in HTML and have saved it as template.php.
Is there a way that I can make .php files for each page with just the content in them (i.e. the text and possibly an image or two) and link the nav buttons so when clicked on, they refresh template.php but with the desired page content in, depending on which button the user selected?
This way I could cut down on file sizes instead of having a template for each page.
Is this do-able and if so, how would I go about linking the buttons etc?
here’s a simple example. If you save the code as a file named index.php you can load the files with the following url: index.php?page=home etc.
<?
$allowed_pages = array("home","news","links", "contact");
// include the page header
include("header.php");
/*include the requested file, but first check if it is a valid request
this one checks if:
- the page is set (index.php?page=file)
- the page variable not is empty (index.php?page=)
- the page variable is allowed to be included
*/
if(isset($_GET['page']) && $_GET['page'] != '' && in_array($_GET['page'],$allowed_pages))
{
$file = $_GET['page'] . ".php";
}
// now the request form is valid, but the requested page is not.
elseif(isset($_GET['page']) && ($_GET['page'] == '' || !in_array($_GET['page'],$allowed_pages)))
{
$file = "filenotfound.php";
}
// when $_GET['page'] not is set include the homepage
else
{
$file = "home.php";
}
// include the file
include($file);
// include the footer
include("footer.php");
?>