Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/155089-solved-printing-a-text-file-to-a-page/
Share on other sites

 

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?

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.