...

View Full Version : text from file



doctrin13th
10-27-2009, 07:36 AM
So I setup the page, plain html, with tables and cells width and height assigned and each cell's text formatting.

Now, on one cell I want to display the content of a text file. I want the text to be formatted with the corresponding cell text formatting.

Is it possible?

Can anyone give me the code?

Thank you!

abduraooft
10-27-2009, 08:14 AM
So I setup the page, plain html, with tables and cells width and height assigned and each cell's text formatting. Oh.. NO! Don't you know using tables for layout is very bad (http://www.hotdesign.com/seybold/)?

Now, on one cell I want to display the content of a text file. I want the text to be formatted with the corresponding cell text formatting.

Is it possible? To read from a text file, you'd need to use some server side code like PHP, ASP etc. Pure HTML can't do such operations.

doctrin13th
10-29-2009, 02:26 AM
can you point me to any basic tutorial about that? i'm afraid i'm a beginner in php,asp thing.

abduraooft
10-29-2009, 08:19 AM
Check http://php.net/fread and related links

doctrin13th
10-31-2009, 10:12 AM
i mean how would i use or insert that code into my page? can u give a very very simple php example? pleaseeeee

SB65
10-31-2009, 10:57 AM
Here we go then...

Assuming you have a text file called text.txt located in the same folder as the web page, then the code below opens the file and displays the content (in a table).

This assumes that your server supports php - it probably does, easiest thing is to try it. The file must be saved with a .php extension, not .htm, to identify it as a page containing php code.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>Reading from a file</title>
</head>

<body>
<?php
$file = fopen("text.txt", "r");//open the file
$contents = fread($file, filesize("text.txt"));//read the contents
fclose($file);
?>

<table>
<tr>
<td>Text file contents:</td>
<td><?php echo $contents;//display the contents?></td>
</tr>
</table>
</body>
</html>

Apostropartheid
10-31-2009, 04:58 PM
In this case, you could also use the include() function.

doctrin13th
11-02-2009, 09:56 AM
SB65
i tried to copy ur code, pasted it in a new notepad blank document,and saved it as index.php with "Save As" all type(not text document). then i made a text.txt in the same folder put some strings in it. when i open the index.php in a browser it only show the text "Text file contents:" and not the contents of the text.txt. what possible could be the problem?

SB65
11-02-2009, 10:12 AM
Is the file index.php on a web server that supports php? It won't do anything if you're just viewing it locally on a PC (unless you're using xampp or similar as a local server).

doctrin13th
11-02-2009, 12:29 PM
how can i set my pc as a local web server?

SB65
11-02-2009, 12:33 PM
You could install xampp (http://www.apachefriends.org/en/xampp.html). But to be honest, assuming you've got access to a live website somewhere, just try it on that. It's a lot less trouble.

babyjun347
11-02-2009, 08:56 PM
I have a question is it possible to use this method to bring up the contents of a webpage?

SB65
11-03-2009, 08:36 AM
If you are aiming to have, for example, a single file containing the menu for your site, and want to have this on multiple pages (assuming that's what you mean) then this can be done (as Apostropartheid mentions above) by the php include function.

Let's assume I have a file containing just my menu html (no header or anything like that), called menu.htm, stored in the same folder as my page. To include it in a page use:


<?php include("menu.htm"); ?>

and put this statement in the html where the menu should be. The file containing the php include must be saved as a .php file otherwise it won't work. It's frequent, of course, to want to include the menu file in multiple places which may not be all in a single directory, in which case I can use:


<?php include($_SERVER['DOCUMENT_ROOT']."/menu.htm"); ?>

which will work within a file in any location.

babyjun347
11-08-2009, 04:26 PM
If you are aiming to have, for example, a single file containing the menu for your site, and want to have this on multiple pages (assuming that's what you mean) then this can be done (as Apostropartheid mentions above) by the php include function.

Let's assume I have a file containing just my menu html (no header or anything like that), called menu.htm, stored in the same folder as my page. To include it in a page use:


<?php include("menu.htm"); ?>

and put this statement in the html where the menu should be. The file containing the php include must be saved as a .php file otherwise it won't work. It's frequent, of course, to want to include the menu file in multiple places which may not be all in a single directory, in which case I can use:


<?php include($_SERVER['DOCUMENT_ROOT']."/menu.htm"); ?>

which will work within a file in any location.

Ok well I made a custom table for news updates and I tried this. The table shows up on the the page but it's not where I want it is it because I'm using css and div layers for my layout?

Here's the page so you can see. I want the update just below the twitter updates but it shows up at the bottom of the page
http://docitalyltd.110mb.com/ASF/test.php

SB65
11-08-2009, 06:52 PM
There are a few problems with the html and css of that page. In general you have some serious errors - like no doctype at the top of your document for a start, and some divs missing closing tags.

Have a look at the validator errors (http://validator.w3.org/check?uri=http://docitalyltd.110mb.com/ASF/test.php).

You also seem to have div id="main" twice in your html and in your css.

I think all of this may be because you have included the doctype etc on your include file, which isn't correct.

Your test.php file, which contains the include statement should have a doctype, head and body just like any "normal" html page. However the included file - the one called via the include statement, needs only to contain the required html, not any header information or anything else which is already included in the first file. Think of the contents of this file just replacing the include statement in the html of your finished page.

Hope that makes sense...

babyjun347
11-08-2009, 11:28 PM
Ok I found out that the hosting site keeps adding in an extra html command with my script I'm not sure why but I'm gonna try it on a different host and see what happens but thanks for all the help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum