SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2003
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    formatting help needed - importing .txt from database

    The site i am working on at the moment, has a number of large summary and questionaire pages.
    the way i am doing this is through a database, which links to the .txt file and simply includes it in the htm/php template file when required.
    this all works well, except that the text is turned into a single line, i loose all new-line / paragraph etc information.
    is there a way to rectify this, google has failed to help me in this matter.

    many thanks

    ps - when linking to the .txt files, should i use relative or absolute locations?

  2. #2
    Chessplayer kleineme's Avatar
    Join Date
    Apr 2004
    Location
    Germany
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    you could use nl2br() but then you would have to quit including the files. Instead you would have to use
    Code:
    $text = file_get_contents($link_to_file);
    echo nl2br($text);
    Never ascribe to malice,
    that which can be explained by incompetence.
    Your code should not look unmaintainable, just be that way.

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2003
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the response, but i'm a little confused by "quit including the files" do you mean i would have to stop saving the files as .txt files?
    i did a search for "nl2br()" and found a few pointers i have changed

    HTML Code:
    <td valign=&quot;top&quot;><?php include(&quot;archive/sum/testing123.txt&quot;); ?></td>
    sum_templat.php

    into
    HTML Code:
      		<? $myFile = file(&quot;archive/sum/testing123.txt&quot;);
      // Now $myFile is an array looking like
      // [0] => &quot;row 1&quot;;
      // [1] => &quot;row 2&quot;;
      // [2] => &quot;row 3&quot;;
      // ..... 
      for ($i = 0; $i < count($myFile); $i++) { print &quot;Row &quot;.($i+1).&quot;: &quot;.$myFile[$i]; }; 
      ?> 
    sum_template2.php

    which is a start, as it least it is recognising the line breaks - just not doing anything about them. if you could point me in the way of a more in depth tutorial or tell me what i'm doing wrong i'd be grateful.

    Many Thanks

  4. #4
    Chessplayer kleineme's Avatar
    Join Date
    Apr 2004
    Location
    Germany
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    no, that's not what I meant to say. You still can save your files as .txt files. But you would have to change the way you are accessing those files.

    include("file.txt") won't work because you can't use nl2br() on an include. The best way, in my opinion, would be the use of file_get_contents, as this returns the whole file as a string which you can use nl2br() on. Using file() would be a bit more complicated.

    Modifying your example it would look like this:

    Code:
    $myFile = file_get_contents("archive/sum/testing123.txt");
    print nl2br($myFile);
    You would use this code where you've used include() previously.
    Never ascribe to malice,
    that which can be explained by incompetence.
    Your code should not look unmaintainable, just be that way.

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2003
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    excellent - worked perfectly.
    thanks.


Bookmarks

Posting Permissions

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