quarantine Posted April 21, 2009 Share Posted April 21, 2009 I am trying to figure out how to print a text file line by line on a HTML page when it loads. This is the basic setup of the page I have ..... <html> <head> <title>Guest Book </title> </head> <body> <form action="guestbook.php" method="post"> Name:<input type="text" name="name" /> <br /> Message:<input type="text" name="body" /> <input type="submit" value="Sign Guest Book" /> </form> <br /> <br /> <?php // set file to read $file = 'guestbook.txt' or die('Could not read file!'); // read file into array $data = file($file) or die('Could not read file!'); // loop through array and print each line foreach ($data as $line) { echo $line; } ?> </body> </html> I tried using foreach() and reading the text file line by line, but when I go to the html page nothing happens. Am I putting this php in the wrong place? How can I best archive this? *EDIT* I would like to text file to appear line by line, instead of printing all on one line. Quote Link to comment https://forums.phpfreaks.com/topic/155089-solved-printing-a-text-file-to-a-page/ Share on other sites More sharing options...
The Little Guy Posted April 21, 2009 Share Posted April 21, 2009 Like this? http://de.php.net/manual/en/function.fgets.php#function.fgets.examples Quote Link to comment https://forums.phpfreaks.com/topic/155089-solved-printing-a-text-file-to-a-page/#findComment-815775 Share on other sites More sharing options...
quarantine Posted April 21, 2009 Author Share Posted April 21, 2009 Like this? http://de.php.net/manual/en/function.fgets.php#function.fgets.examples Thanks, that works great, but it is printing all on the same line like...... texttextextextextext I was trying to get it to print line by line like.... text text text text Is there a way to do that? Quote Link to comment https://forums.phpfreaks.com/topic/155089-solved-printing-a-text-file-to-a-page/#findComment-815782 Share on other sites More sharing options...
premiso Posted April 21, 2009 Share Posted April 21, 2009 echo $line . "<br />"; Quote Link to comment https://forums.phpfreaks.com/topic/155089-solved-printing-a-text-file-to-a-page/#findComment-815784 Share on other sites More sharing options...
quarantine Posted April 21, 2009 Author Share Posted April 21, 2009 echo $line . "<br />"; ahhh thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/155089-solved-printing-a-text-file-to-a-page/#findComment-815786 Share on other sites More sharing options...
Maq Posted April 21, 2009 Share Posted April 21, 2009 You could have also put the output in tags. Quote Link to comment https://forums.phpfreaks.com/topic/155089-solved-printing-a-text-file-to-a-page/#findComment-815787 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.