New to PHP. Couple of basic questions

Hi guys,

Thank you for checking this out.

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?

Thank you very much and I hope to hear from you.

All the best,

Mark

you could create 3 files:

header.php

myContent.php

footer.php

where myContent.php will be your changing content, that way when you change the header or footer it will change sitewide.

Dunno if this is what you are looking for though, I think the php for inlcude is:

<? include(‘filename.php’); ?>

thanks for that mate - that’s brilliant. so can i put multiple files in the code of mytemplate.php. i.e.

<?php include ‘home.php’;?>
<?php include ‘news.php’;?>
<?php include ‘links.php’;?>
<?php include ‘contact.php’;?>

…so when, say, the news button is clicked on, the browser will refresh mytemplate.php and display news.php within the page?

if so, how would i go about linking the buttons?

thank you very much - that’s great.

mark

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");
?>