954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How do I open a .txt file in php in a set format?

I am successfully reading and displaying a .txt file with 3 variables, lets say for example name, age and comments.

When the file is written, 1 name, 1 age and 1 comment gets written on a line. Any further additions get added as a new line.

When this gets read in my php page, it displays the info but all on the same line.

I really want to have an image box with name and age underneath and then comments to the right hand side. Also, added css to have comments in speech marks (which would be a small image) would be perfect.

Any help?

This code reads and displays the file:

<?php
$myFile = "location.txt";
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
fclose($fh);
echo $theData;
?>

ptemedia
Light Poster
29 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

You could separate your fields with a comma or some other unique character (when the file is written) and then use explode when you read the line to separate the fields.

chrishea
Nearly a Posting Virtuoso
1,429 posts since Sep 2008
Reputation Points: 210
Solved Threads: 230
 

Yes, it is common to have that kind of files looking like this

Peter;26;This guy is awesome!

or

Peter,26,This guy is awesome!

or you could specify multilined sections like this, but your need doesn't seem like this is any better unless you of course like the syntax.

{
Peter
26
This guy is awesome!
}
Excizted
Posting Whiz
309 posts since Oct 2009
Reputation Points: 94
Solved Threads: 27
 

ah, awesome. Cheers guys. That's very useful and so simple! I can't believe I couldn't find how to do that on google!

Only, I'm now having problems displaying more than 1 line.

I'm gussing i need some sort of loop in place, working on it now but any help would be appreciated

ptemedia
Light Poster
29 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Can't edit above post :(

Here is my code so far, it works, but obviously only returns 1 line of the text file. Each line contains 1 name, 1 age and 1 comment:

<?php
$myFile = "textfile.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
$messages = explode(";", $theData);
fclose($fh);
?>


Name: <?php echo "$messages[0]"?>

Age: <?php echo "$messages[1]"?>

Message: <?php echo "$messages[2]"?>

ptemedia
Light Poster
29 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

I have worked it out and posted it here for anyone else who stumbles across this thread and needs answers:

<?php
$myFile = "mytextfile.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
$messages = explode("\n", $theData);
    foreach ($messages as $individual){
        $individual = explode(";", $individual);
        echo "<p>Name: $individual[0]";
        echo "Age: $individual[1]";
        echo "Message: $individual[2]</p>";
        }
fclose($fh);
?>


Many Thanks anyway, Patrick Taylor-Edwards www.patricktayloredwards.com

ptemedia
Light Poster
29 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Good job :)

Excizted
Posting Whiz
309 posts since Oct 2009
Reputation Points: 94
Solved Threads: 27
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: