Convert HTML to PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
McCordRM
Forum Newbie
Posts: 3
Joined: Sun Aug 22, 2010 3:57 am

Convert HTML to PHP

Post by McCordRM »

I come from a background using Frontpage (yea, I know) and would like to convert my site
from it's .HTML format to .PHP. Every page on my site has exactly the same code except for
the Content area. This area is an Include (.inc) file containing said content. Is here is an
example of a page:

Code: Select all

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title> Author: Richard M. McCord II</title>
<meta name="Author" content="Richard M. McCord II" />
<link rel="stylesheet" type="text/css" href="Code/layout.css" />
<link rel="shortcut icon" href="favicon.ico" />
<script type="text/javascript" src="Code/jquery-1.4.2.js"></script>
<script type="text/javascript" src="Code/jquery.easing.1.3.js"></script>
<script type="text/javascript" src="Code/jquery.lavalamp-1.3.4b2.js"></script>
</head>

<body>
		<!-- START PAGE -->

<div id="wrapper">

		<!-- START HEADER -->
<div id="Header">

	<div id="Header-Left">
	<a href="http://richardmccord.com"> <img src="Graphics/logo.png" alt="" img border="0"/></a>
	</div>

<img src="Graphics/separator.png" alt="" img border="0"/>

	<div id="Header-Right">
	My thoughts. My words. My world.
	</div>
	
</div>
		<!-- END HEADER -->


		<!-- START Menu -->
<!--#include file="MainMenu.inc" -->

	<script type="text/javascript">
	$(function() {
	$('ul#menu').lavaLamp();
	});
	</script>
		<!-- END Menu -->


		<!-- START CONTENT -->

	<div id="Content">
	<div id="Content-BG">
	<div id="Content_top">
	<div class="title">
		<p>Welcome</p> 
	</div>
	</div>
		<div id="left">

<!--#include file="Welcome.inc" -->

		</div>

<div id="right">
</div>

	<div id="Content_bottom"></div>
	</div>
	</div>

		<!-- END CONTENT -->


		<!-- START Footer -->
<div id="Footer">
&copy; 1998-2010 Richard M. McCord II
</div>
		<!-- END Footer -->
</div>
		<!-- END page -->

</body>
</html>
A couple of things to note:
1. The Content area contains a left and right Div. If a particular section requires a Sidebar
menu, then the Right Div houses that file. Otherwise, it's empty.
2. The images and files loaded by the page must have their code updated every time I
go one folder deeper. So: Images/FILENAME becomes: ../Images/FILENAME. And so on.

What I'm trying to do is create one PHP file that will work regardless of how deep I go
in my directory structure so that the only thing I need to keep up with (maintain) is which
Include file is loaded based on the menu choice a visitor makes. I keep hearing that PHP
can accomplish this, and also do so without changing the URL to show the full location of
where the user is. For example, to only show: http://www.richardmccord.com instead of:
http://www.richardmccord.com/Opinions/Opinions.html
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Convert HTML to PHP

Post by requinix »

You don't need PHP for that. Just change your URLs (the various srcs and hrefs) so that they start with a slash. Like

Code: Select all

<link rel="stylesheet" type="text/css" href="/Code/layout.css" />
McCordRM
Forum Newbie
Posts: 3
Joined: Sun Aug 22, 2010 3:57 am

Re: Convert HTML to PHP

Post by McCordRM »

I tried that... it doesn't work. I have to use
../Images/FILENAME
../../Images/FILENAME
Etc.

The site, itself, it pretty simple. By that, I mean I don't have a
lot going on... no Ad banners or Twitter feeds, none of that crap.
But still, I'd like to make adding to the site or maintaining it simpler.

Right now, every page actually has two files:
The HTML file and the INC file with the content for that page. I figure
there's got to be an easier way without switching to a CMS. I hate relying
on someone else to update their stuff to stay "current". Plus, when I tried
Wordpress I had massive failures relying on my web host's database backups.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Convert HTML to PHP

Post by requinix »

So you're concerned about moving the site around a lot and need an easy way to change the site "base" location?
McCordRM
Forum Newbie
Posts: 3
Joined: Sun Aug 22, 2010 3:57 am

Re: Convert HTML to PHP

Post by McCordRM »

I wouldn't say a "lot". Even if I only have to move it once, not having
a reliable database would mean a lot of work. I'd rather do a bit of work
now and avoid any potential issues. For me, this means not using a
database and not using a third-party software system like a CMS and
the like.

What I was hoping to discover was a system I could implement, for example,
where my Menu links actually assigned a variable and my one PHP page would
use that variable to pull that content into the page. So when you go to my
website, it loads index.php. The content would be the default content for
the opening page, like Welcome. Then, if you click the About the Author link,
the content would change from the Welcome.inc file data to the Biography.inc
file data. Something like that.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Convert HTML to PHP

Post by requinix »

Alright then, there really is a use for PHP here.

Create a file config.php (in the root of your web directory). You can put whatever you want in there, such as

Code: Select all

define("URLBASE", "/site/"); // http://www.example.com/site/*
In every file that you need whatever PHP variable, or constant as I did there, make sure to:
1. Rename the file to .php. If this poses a problem then there is a way around it.
2. Include the config.php file by putting

Code: Select all

<?php include $_SERVER["DOCUMENT_ROOT"] . "/config.php"; ?>
at the top. There is an alternative to this for Apache web servers: auto_prepend_file.

To use the example constant I gave, you'd have

Code: Select all

<link rel="stylesheet" type="text/css" href="<?php echo URLBASE; ?>Code/layout.css" />
With that basic "framework" in place you can do a lot more, too. It's not the best solution but it's definitely the simplest.
Post Reply