Page 1 of 1

separating html desisng & php coding using include funct

Posted: Sat Oct 23, 2004 4:17 am
by developer_x
Sami | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

i had downloaded a script written for php5 and run it on php 4.3.2. its not working. plz let me know wats the problem?

 ********
Tutorial:

[i]PHP also offers two very useful functions to import files into a PHP script: the include() and require()functions. These functions can be used to suck external files lock, stock and barrel into a PHP script, which is very handy if, for example, you have a modular application which has its code broken down across files in separate locations. 

The best way to understand the utility of the include() and require() functions is with an example. Assume that on your Web site you have a standard menu bar at the top of every page, and a standard copyright notice in the bottom. Instead of copying and pasting the header and footer code on each individual page, PHP gurus simply create separate files for the header and footer, and import them at the top and bottom of each script. This also makes a change to the site design easier to implement: instead of manually editing a gazillion files, you simply edit two, and the changes are reflected across your entire site instantaneously. 

Let's see a real live example of this in action. Create the header in one file, called header.php: [/i]

Code: Select all

<html> 
<head><title><?php echo $page['title']; 
?></title></head>
<body> 
<!-- top menu bar --> 
<table width="90%" border="0" cellspacing="5" cellpadding="5"> 
<tr> 
<td><a href="#">Home</a></td> 
<td><a href="#">Site Map</a></td> 
<td><a href="#">Search</a></td> 
<td><a href="#">Help</a></td> 
</tr> 
</table> 
<!-- header ends -->



Next, create the footer with the copyright notice in a second file, footer.php:

Code: Select all

<!-- footer begins --> 
<br />
<center>Your usage of this site is subject to its published <a href="tac.html">terms and conditions</a>. Data is copyright Big Company Inc, 1995-<?php echo date("Y", mktime()); ?></center> 
</body> 
</html>



Finally, create a script to display the main content of your site, and include() the header and footer at appropriate places:

Code: Select all

<?php 
// create an array to set page-level variables
$page = array(); 
$page['title'] = 'Product Catalog'; 
/* once the file is imported, the variables set above will become available to it */ 

// include the page header 
include('header.php'); 
?> 

<!-- HTML content here --> 

<?php 
// include the page footer 
include('footer.php'); 
?>

Now, when you run the script above, PHP will automatically read in the header and footer files, merge them with the HTML content, and display the complete page to you. Simple, isn't it?

Sami | added italics to the tutorial text

Posted: Sat Oct 23, 2004 4:22 am
by vigge89
what error(s) do you get?