Click to See Complete Forum and Search --> : Clear answer needed: HTML to PHP?
outrbanx
08-02-2006, 09:04 PM
Another newbie PHP question for ya!
I get different opinions when I search for an answer to this. Since I am creating some php pages for my site, is it possible to change the rest of the pages from HTML to PHP to take advantage of using a .inc file to handle some attributes (such as the menus), that are identical to all pages? I currently use CSS to handle the fonts and colors. Will that affect it?
As it is now, simply adding a new item to the menu is to copy and paste the new code in every single page that exists. Sorta counter-productive.
If there is a better way, I am always open to suggestions. :)
aussie girl
08-02-2006, 11:39 PM
Yes you can, the browser will display.php file will the same as HTML it's only the code in between the <?PHP ?> tags that get executed by the PHP interpreter..
AcousticJames
08-02-2006, 11:41 PM
Since what you want doesn't really have to happen with PHP, have you looked into Server Side Includes? Google should give you some decent results.
emoritz
08-02-2006, 11:59 PM
There two ways you could do this.
First, you could make one php file that handles all requests. Say you link to the file page.php.
You could add some GET options too, like the page you wanted to view. page.php?file=main.htm.
The code for this is simple:
<?php
$file = $_GET["file"];
# this is where the page header goes
virtual('header.ext');
# read file and display contents
# use $contents = file_get_contents($file); if you have PHP 5
$fp = fopen($file,"r");
$contents = fread($fp);
$fclose($fp);
echo $contents;
# optional footer?
virtual('footer.ext');
?>
I used virtual() because you're only displaying html, use include() if you need to pass variables. Don't forget to use the correct file paths.
The other way to do this would be to make a one-time use script that actually edits the file, appending the header. You could use a recursive directory-listing function, or just list the files yourself. Here's a sample:
<?php
# array of filenames
$file_list = array('file1.ext','file2.ext','file3.ext','file4.ext');
#header text, optional use file
$text = "Some string or file";
foreach ($file_list as $file) {
$fp = fopen($file,"r+");
$contents = fread($fp);
# appends $text to beginning of file
fwrite($fp,$text.$contents);
$fclose($fp);
}
?>
- much faster than copy/pasting.
There are many ways of optimizing / utilizing, but this is the basic concept.
As for SSI, yeah, it works, but security is tight nowadays and some servers don't support it. Go php!
outrbanx
08-03-2006, 06:42 AM
Thanks to all for your replies.
There two ways you could do this.
First, you could make one php file that handles all requests. Say you link to the file page.php.
You could add some GET options too, like the page you wanted to view. page.php?file=main.htm.
The code for this is simple:
<?php
$file = $_GET["file"];
# this is where the page header goes
virtual('header.ext');
# read file and display contents
# use $contents = file_get_contents($file); if you have PHP 5
$fp = fopen($file,"r");
$contents = fread($fp);
$fclose($fp);
echo $contents;
# optional footer?
virtual('footer.ext');
?>
I used virtual() because you're only displaying html, use include() if you need to pass variables. Don't forget to use the correct file paths.
Okay. I think I like this idea. Keeping in mind that I am still within my first 10 days or so of using PHP, can anyone provide details? I mean, the above example makes sense, but would I create a page like this for each exisitng page with only the content variables changed?
I understand the code given by emoritz (for the most part), I am just in the tall weeds as to how to go about implementing it based on my site right now. I have not yet completed the changeover to PHP for the two or three pages that were HTML, but this is probably a good time to make these types of changes sitewide. I never was one for small changes. Go big or go home, I guess. I'm like a kid with a new (PHP) toy. ;)
emoritz
08-03-2006, 10:58 AM
The main idea is that there is one page main.php that loads all of your other existing pages.
Say you have the following files:
main.php
file1.htm
file2.htm
file3.htm
To view a file, you would link to main.php with the syntax main.php?file=FILENAME.EXT. For example, file1.htm would be main.php?file=file1.htm.
This can be inserted in hyperlinks too, such as <a href="main.php?file=file2.htm">File2</a> So you'd only need one php script to handle all other files.
I hope this clears things up a bit. If you have any more questions concerning this or any other topic you can contact me directly.
outrbanx
08-03-2006, 03:40 PM
Gave it a shot, but I am getting:
Fatal error: Call to undefined function: virtual()
Ideas?
Sheldon
08-03-2006, 03:59 PM
ok, thats isnt the best way to do it.
Read up about includes, http://php.net/includes
Even if you are on your first day of php this is what o start with.
Dont use one page to call all the other pages if you want any decent sort of google rankings.
I would have 3 common files
header.php,
menu.php
footer.php
in each page write it like this
//index.php
<?php
inculde("header.php");
?>
content for this page
content
content
we want the menu next
<?php inculde("menu.php"); ?>
more content after the menu
end of page image
<?php
inculde("footer.php");
?>
Then when you make a new page, just add the link in to the menu.php and its done.
To change the loook of the whole site change the CSS and the header.php and footer.php and if you have done it right it should all still work seamlessly and still be good for SEO.
outrbanx
08-03-2006, 04:08 PM
Thanks, Sheldon. I believe that is the same as what Aussie Girl suggested.
I was able to take an existing .html page on my site, rename it to .php, remove the header (this includes all of my javascript, dhtml, css and menu), place in it an .inc file, add an include statement calling the header.inc file into the .php and it all seemed to work flawlessly.
Is there any disadvantage to this? Can css codes, dhtml codes and javascripts live happliy in this .inc file, or is it it best to keep the menu and all of the scripts seperate? So far, there doesn't seem to be a problem for them....