Jump to content
xisto Community
yordan

How Can I Include A File In A Html Page ? a fast changing text in several pages, how can I put it in a file and

Recommended Posts

A often-changing text in a static page, or a same set of lines in several pages...How can I include a file inside a html page ?#include will probably be a wrong syntax, but...i would have to have mything.html which is often changing, and I would like it to be inserted in main.html as well as in menus.html and in news.html.Could I insert something like #include mything.html in each page, and then, if I want to modify myghing.html the modifications are seen in all the pages where it's included ?Or do I ask something stupid ?

Share this post


Link to post
Share on other sites

If you are using pure HTML, I believe it can only be done with frames, which is so not good, nor professional. However, you could also use a small portion of PHP, and that would make your life easier.

 

There's a function in PHP called include(), and it just includes a file in the page. So, instead of having the same text on multiple pages, replace it with <?php include("text.html"); ?>. Then create a text.html file with the appropriate content.

Share this post


Link to post
Share on other sites

I agree include is great in PHP. Be careful how you use it however, there's a difference in 'include' and 'require'. You should like into those 2. That's how I do it and it works great for me. Either use include files for missing chunks of similiar html or make a standard include file and put functions in it to print out the html for you. Whatever you prefer.

Share this post


Link to post
Share on other sites

Take a look at this and see if it is along the lines of what you are wanting to do.

CMS101 - Content Management System Design

 

If that is what you are wanting to do then you got some work to do...

First, you'll have to change the extention of all of the files that want to use php in from .html to .php!

Second, you'll have to edit all of those pages again but this time to replace the frequently edited material with the php include code.

Third, create the various files that you wish to include in you web pages.

Forth, add a rewrite rule to your .htaccess file to forward page requests for your .html files to your new .php files.

This seems like a lot of work but the end result will really save you a bunch of time in the future because like you said, you could edite a single file to make major changes to your entire website. This type of system can be as simple as just inserting the file contents at the place specified in the webpage to allowing you to use a single template file with various modules and files for website tools and content.

 

I can help you with the basic coding for either option but the overall system will be up to you to design since you are the one that has to use it.

 

Just let me know what information you are interested in and I'll try my best to explain how to do it. If you are intersted in a more complex system where a single template file is used, this will require the use of complex urls like index.php?this=1&that=2&other=3

 

Also, have you considered using a CMS program like Joomla?

 

vujsa

Share this post


Link to post
Share on other sites

Forth, add a rewrite rule to your .htaccess file to forward page requests for your .html files to your new .php files.

Nope... you could always just have it reversed where .html is parsed as .php. Then you don't need to bother changing any file extensions and the what not. You can keep it all the same.

 

[N]F

Share this post


Link to post
Share on other sites

Nope... you could always just have it reversed where .html is parsed as .php. Then you don't need to bother changing any file extensions and the what not. You can keep it all the same.
[N]F

Yep, and here is why. PHP should be parsed as PHP and HTML should be left alone to do its thing.

Problems begin to arrise when you start messing around with your handlers. If you slowly begin to convert your website over to PHP generated, then eventually you'll have all PHP file anyhow. I guess my point is that if you are going to do something like this, then you might as well do it right the first time. Eventually, most of you HTML requests will disapear and only PHP requests will be left. The rewrite rule is just to prevent you old links from being dead. If you set it up correctly, the search engine spiders will even update your links instead of creating new ons in their database.

My advice is to update your files with whatever PHP you want or need to use and just Save As a PHP file instead of HTML. You'll have to edit most of your files anyhow to insert the PHP that is required so it wont really be much more work to save it as PHP other than updating any internal links from .htm to .php.

vujsa

Share this post


Link to post
Share on other sites

OK, seems to need more work than expected.
Also SSI should work, as said here http://www.keynote.com/
The syntax "<!--#include virtual="other_page.htm" -->" looks exactly what I would like to do. Unfortunately, you cannot check the result on your own laptop, you should see it only after downladed on the web server, and even then SSI could happen not to work.
Yet another great idea which is not easy to implement. sigh !

Share this post


Link to post
Share on other sites

One additional problem with SSI is that most servers require you to rename your file extentions to .shtml! :D

How many files do you have?

It is possible to write a small script that change all of your internal links and save the file with the .php extention. ;)

As for the forwarding of your old HTML request to your new PHP files take a look here:
http://forums.xisto.com/topic/88716-topic/?findpost=1064337857

What was it that seemed to be so much work? If it is changing all of the extentions, then I think I can help out with a simple script to change everything over. If it is editing your files to insert the PHP code, I can't really help there because more than likely there isn't enough of a pattern to your sight for a search and replace script. If all of your pages use pretty much the same layout, then it might be possible to replace your existing content with a PHP code. For example if you have the exact same menu on every page, then we can do a search and replcae based on that pattern.

I'm more than happy to give you a hand with this if you want and I can show you the most simplified methods possible.

Remember, if we are going to forward all of your old HTML requests to your new PHP files, then fixing all of the internal links doesn't have to happen right away since the request will get to the right place anyway. ;)

So just by adding the forwarding rule and changing all of your extentions, your website would continue to work but would now be PHP ready. :)
Your PHP files don't actually have to contain PHP to work. :)

Hope this helps.

vujsa

Share this post


Link to post
Share on other sites

One additional problem with SSI is that most servers require you to rename your file extentions to .shtml! :)

True. Internet Information Services (Windows servers) don't really care what the extension is. Just another reason why Windows servers shouldn't be allowed...

[N]F

Share this post


Link to post
Share on other sites

Well, to be honest I haven't read much of the replys but this is probably obvious to some and may very well have been mentioned already to some degree, but this is how I do it.My web host (co-incidentaly Asta-Host heh heh) supports PHP, so I use includes, for my header/footer/left navigation/shoutbox and so on. Soon I will also be using them to have seperate meta tag data for different pages that could benefit from it (and also with more individualised titles).It's fairly simple to use with .php pages, where you want the source brought in just use: include 'myfile.html'; or include 'myfile.php'; etc. Which I believe just directly injects the source of the file you wish to include at that point.I use this also for the inclusion of my database connection file, instead of having that code re-written lots of times through several documents.

Share this post


Link to post
Share on other sites

Thanks for all these answers, folks. Now I start being slightly less ignorant.

How many files do you have?

Sorry, I asked the question because I wanted to know how to do this things.I am only learning how to create a website, and I am wondering how should I design it in order to make serviceability easy.
I currently have only five files, so to be honest it's really nothing modifying all these files. Currently I do "vi main.html news.html forum.html working.html", do the first modification, and simply
:w
:n
.
:w
:n
. until "no more files to edit".
However, just for architecture point of view as well as for learning purposes, I wanted to know if I could do the same way I do when doing c programming using include files.
So, basically, my idea is good and it should work.
Unfortunately, for testability and portability reasons I will forget about it. Because I cannot test it on my laptop under windows, whereas I can test pure stupid simple html pages.
(By the way, I say "stupid simple" because I currently know only how to do very simple things. But I hope I will impore myself, I have plenty of time because it's for my own pleasure).

Share this post


Link to post
Share on other sites

yordan,

I don't think you should give up on the easy maintainence option so quickly.
I know that the idea of trying to develope a website based on a laguage that you are just starting to learn is probably pretty scary. Since you don't have a lot of pages to edit, I think you have a great opportunity to learn as you go.

There are various tutorials at Xisto that can show you how to set up an Apache server on your local system complete with PHP and MySQL that can be used to test scripts offline. The server is rather easy to set up and doesn't require many system resources unless it is serving webpages.

You might give it a shot and play around with it for testing.
That's how I learned.

Another thing to remember is that your PHP file doesn't actually have to contain any PHP! So you can name your files .php but they will just be HTML.
You can insert little bits of PHP into your pages without much programming knowledge.
index.php

<html>
<head>
	<title>
		Sample HTML / PHP Webpage
	</title>
	<meta name="description" content="">
	<meta name="keywords" content="">
	<meta name="author" content="vujsa">
</head>
<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080">
	Begin content here, but don't forget to add the date: <?php echo date('l dS \of F Y h:i:s A'); ?><br>
	Add more content here!
</body>
</html>



This would give you a result like so:

Begin content here, but don't forget to add the date: Monday 15th of August 2005 03:12:46 PMAdd more content here!


Pretty simple huh.

Now here is what you were interested in:
content.php
<?php echo "Begin content here, but don't forget to add the date: ". date('l dS \of F Y h:i:s A') ."<br>\nAdd more content here!";?>

index.php
<html>
<head>
	<title>
		Sample HTML / PHP Webpage
	</title>
	<meta name="description" content="">
	<meta name="keywords" content="">
	<meta name="author" content="vujsa">
</head>
<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080">
<? include("content.php"); ?>
</body>
</html>



You still get the same output in the web browser when you view index.php:

Begin content here, but don't forget to add the date: Monday 15th of August 2005 03:12:46 PMAdd more content here!


Now if you want to change the content, you only need to edit content.php.
This isn't as useful as it would be with either a template system which I think you aren't interested in yet or if you have the same chunk of data on every page like a menu.

Now that we have further reduced your ignorance, you might be less apprehensive about trying to do this.

Any questions?

vujsa

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.