Using PHP to add Included files to HTML code.

General discussion on how to create or maintain a website. Different site creation tools and editors may also be discussed here.

Using PHP to add Included files to HTML code.

Postby Spock » Sun 2008 Jan 13 1:23

Ref: http://www.developtheweb.org/forums/sho ... php?t=2918

I have known for some time how I wanted to change the code for my website but have never had the time (or the knowledge) to actually make any changes. I am approaching a week off of classes, so now have the time and, fortunately for me, I have found someone who has supplied the knowledge.

I will try to recreate the information here but, in case I make any mistakes, the original topic in the other forum where I found the help I needed is in the reference link above.

Since I want to keep this as simple as possible, I am not going to create a "real world" example. The initial HTML code will be a page called from another HTML page and the information broken off into the include file will be trivial. All of this should make the progression from pure HTML to an HTML-PHP hybrid a little easier to follow ... I hope. I will be posting this example to multiple posts, so try not to respond until I'm finished.

First, the "original" HTML code ...
User avatar
Spock
Forum Admin
Forum Admin
 
Posts: 2417
Joined: Tue 2005 Jan 18 10:47
Location: MD, USA

Re: Using PHP to add Included files to HTML code.

Postby Spock » Sun 2008 Jan 13 1:35

The original file I have created for this example is listed below:

helloworld.html
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
   <title>HTML to HTML-PHP Conversion: File 0</title>
</head>

<body>

   <p>Hello World!</p>

</body>
</html>


As you can see, the code is extremely trivial, all the better to make the conversion stand out.

What I want to do is extract the body contents to become an include file so I can simply change the include file without having to change the main HTML file.

Why would I want to do something so simple? Well, my actual application uses an 84 line, 3,298 byte include file that is duplicated in over 20 HTML files. The include file contains my entire Navigation Bar as well as such minor things as the website copyright. In the past, when I wanted to change the copyright notice, I had to open, modify, save, and close each individual file. Using the include file, now I only have to modify a single file to make the same change in all the others!

Next, the changed HTML file and the include file ...
User avatar
Spock
Forum Admin
Forum Admin
 
Posts: 2417
Joined: Tue 2005 Jan 18 10:47
Location: MD, USA

Re: Using PHP to add Included files to HTML code.

Postby Spock » Sun 2008 Jan 13 1:55

The original file I created was helloworld.html. The modified version not only has different code, it also has a different extension, see below:

helloworld.php
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
   <title>HTML to HTML-PHP Conversion: File 1</title>
</head>

<body>

<!-- Include file starts here -->
<?php include("helloworld.inc") ;?>
<!-- Include file ends here -->

</body>
</html>



The include file looks like this:

helloworld.inc
Code: Select all
<!-- Include insert starts here -->
   <p>Hello World!</p>
<!-- Include insert ends here -->


When the modified helloworld.php is executed, the php extension tells the computer that it needs to look for php code and replace it with whatever it is told to replace it with. In this case, it is told to replace the code with the include file called helloworld.inc. Once the replacement has been made, it should become just another simple HTML file again and then it is executed.

The extension of the include file is arbitrary. In this case I wanted it made obvious that the file was only to be used when included within another file. The extension could just as easily been php. Also, the name of the include file is totally arbitrary. I felt that it made a lot of sense to have the same name and a different extension so they would show in a directory list together so I wouldn't have to search for either file.

The only other requirements are that the computer this is to run on should be capable of running php programs and, as I said earlier, the main program, in this case helloworld.php, needs to be called from an html program. The second requirement is not necessary if the file is a standard index.html file.

As you know, if a folder not containing an index.html file is accessed on the Internet, it will display a list of files. If the index.html file is there and executable, it will be run, thereby hiding the file list. An index.php file will work the same way.

My only problem with the above is that my local computer is not capable of running php code, so I would have to make changes, upload it to my host, and then test to verify that everything was working as required. Since I like to make modifications on my local machine and test them before uploading them to my host, this will require a change in the way I maintain my website but I think it will be well worth it.

Comments are welcome and any mistakes in presentation are all mine.
User avatar
Spock
Forum Admin
Forum Admin
 
Posts: 2417
Joined: Tue 2005 Jan 18 10:47
Location: MD, USA

Re: Using PHP to add Included files to HTML code.

Postby Ben Last » Sat 2008 Feb 23 8:05

To view PHP files on your local computer you need some web server software with which to process the PHP code.
Many webservers are capable of doing this, one of these is XAMPP, designed for development use (not production) which uses Apache, mySQL, PHP and Perl.

It is available from: http://sourceforge.net/projects/xampp/

It is also possible to get a version which does not require installing, which is good if you just want to test it out, get this from:
http://portableapps.com/apps/development/xampp

Note: I seem to recall it's quite a large download.
No trees were harmed in the construction of this message, however many electrons were terribly inconvenienced.
User avatar
Ben Last
Consulting Ambassador
Consulting Ambassador
 
Posts: 475
Joined: Fri 2005 Jan 21 7:47
Location: Stowmarket

Re: Using PHP to add Included files to HTML code.

Postby Spock » Sat 2008 Feb 23 9:07

Ben Last wrote:... one of these is XAMPP ...


Thank you. I've had a copy since version 1.5.3a and just grabbed a copy of 1.6.6a so I would have the latest. The problem is, I keep putting off installing it. Not that I don't have the resources, it's just that I always find something else I'd rather do than install Server software. :dunno:

... soon ... ish ... :twisted:
User avatar
Spock
Forum Admin
Forum Admin
 
Posts: 2417
Joined: Tue 2005 Jan 18 10:47
Location: MD, USA


Return to Web Development

Who is online

Users browsing this forum: No registered users and 1 guest

cron