Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: text from file

  1. #1
    New Coder
    Join Date
    Oct 2009
    Location
    Rizal Province, Philippines
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    text from file

    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!

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    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?
    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.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Oct 2009
    Location
    Rizal Province, Philippines
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    can you point me to any basic tutorial about that? i'm afraid i'm a beginner in php,asp thing.

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Check http://php.net/fread and related links
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    New Coder
    Join Date
    Oct 2009
    Location
    Rizal Province, Philippines
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i mean how would i use or insert that code into my page? can u give a very very simple php example? pleaseeeee

  • #6
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,957
    Thanks
    9
    Thanked 727 Times in 721 Posts
    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.

    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($filefilesize("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>

  • Users who have thanked SB65 for this post:

    babyjun347 (11-02-2009)

  • #7
    The Apostate Apostropartheid's Avatar
    Join Date
    Oct 2007
    Posts
    3,215
    Thanks
    16
    Thanked 265 Times in 263 Posts
    In this case, you could also use the include() function.

  • #8
    New Coder
    Join Date
    Oct 2009
    Location
    Rizal Province, Philippines
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #9
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,957
    Thanks
    9
    Thanked 727 Times in 721 Posts
    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).

  • #10
    New Coder
    Join Date
    Oct 2009
    Location
    Rizal Province, Philippines
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    how can i set my pc as a local web server?

  • #11
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,957
    Thanks
    9
    Thanked 727 Times in 721 Posts
    You could install xampp. 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.

  • #12
    New to the CF scene
    Join Date
    Jan 2009
    Location
    Detroit, MI
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I have a question is it possible to use this method to bring up the contents of a webpage?

  • #13
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,957
    Thanks
    9
    Thanked 727 Times in 721 Posts
    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:

    Code:
    <?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:

    Code:
    <?php include($_SERVER['DOCUMENT_ROOT']."/menu.htm"); ?>
    which will work within a file in any location.
    Last edited by SB65; 11-03-2009 at 09:39 AM.

  • Users who have thanked SB65 for this post:

    babyjun347 (11-08-2009)

  • #14
    New to the CF scene
    Join Date
    Jan 2009
    Location
    Detroit, MI
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by SB65 View Post
    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:

    Code:
    <?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:

    Code:
    <?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
    Last edited by babyjun347; 11-08-2009 at 04:29 PM.

  • #15
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,957
    Thanks
    9
    Thanked 727 Times in 721 Posts
    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.

    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...


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •