 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
02-01-2008, 09:54 PM
|
#1 (permalink)
|
|
The Acquainted
Join Date: Jan 2008
Posts: 119
Thanks: 21
|
Echoing Text File Rows into Array Display...
Hi all
Right now i can display each row of my text file using echo... I have my rows divided up by ||... for example:
me||10-02-07||information on php
you||1-12-07||some more info...
I would like to make it look like:
Who posted: ME
Date: 10-02-07
Comment: information on php
Right now I am using a foreach loop to grab the data.
PHP Code:
$fp = fopen("comments.txt", "r+");
while(!feof($fp)){
$line_of_text = fgets($fp);
$parts = explode('||', $line_of_text);
foreach($parts as $part){
echo $part;
}
}
fclose($fp);
I need to have 2 arrays here right? $part[0][1]; or something like that?
Thanks! 
|
|
|
|
02-01-2008, 10:07 PM
|
#2 (permalink)
|
|
The Acquainted
Join Date: Jan 2008
Posts: 119
Thanks: 21
|
I see some other posts with similar thoughts... but not quite what i am looking for...
|
|
|
|
02-01-2008, 10:14 PM
|
#3 (permalink)
|
|
The Frequenter
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
|
Is there a particular reason why you want the file format to be in that structure? It's not an ideal structure to read  For example, to read a structure like that you'd have to read each 3 line block. This means that if you decided to add a 4th line to future blocks, the script would break quite badly.
If you really want that format is it possible that your app could use XML instead? That would make it a lot easier to read/write to and save a lot of work if you decide to change the format in the future
Alan
|
|
|
02-01-2008, 10:41 PM
|
#4 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
How about :
PHP Code:
<?php header('Content-Type: text/plain; charset=UTF-8');
$comments = array();
$fp = fopen('comments.txt', 'r+'); if ( ! $fp) die ('Unable to load comments'); while ( ! feof($fp) && $comments[] = array_filter(fgetcsv($fp, 2048, '|'))); fclose($fp);
foreach ($comments as $comment) { $comment = array_combine(array('user', 'date', 'body'), array_values($comment)); echo 'Who posted: ', $comment['user'], "\n", 'Date: ', $comment['date'], "\n", 'Comment: ', $comment['body'], "\n\n"; }
The important one being line 7: while ....
|
|
|
|
|
The Following User Says Thank You to Salathe For This Useful Post:
|
|
02-01-2008, 10:51 PM
|
#5 (permalink)
|
|
The Acquainted
Join Date: Jan 2008
Posts: 119
Thanks: 21
|
Thanks for the post. Is that system more streamlined that what I have?
Quote:
Originally Posted by Salathe
How about :
PHP Code:
<?php header('Content-Type: text/plain; charset=UTF-8');
$comments = array();
$fp = fopen('comments.txt', 'r+');
if ( ! $fp) die ('Unable to load comments');
while ( ! feof($fp) && $comments[] = array_filter(fgetcsv($fp, 2048, '|')));
fclose($fp);
foreach ($comments as $comment)
{
$comment = array_combine(array('user', 'date', 'body'), array_values($comment));
echo 'Who posted: ', $comment['user'], "\n",
'Date: ', $comment['date'], "\n",
'Comment: ', $comment['body'], "\n\n";
}
The important one being line 7: while ....
|
|
|
|
|
02-01-2008, 10:42 PM
|
#6 (permalink)
|
|
The Acquainted
Join Date: Jan 2008
Posts: 119
Thanks: 21
|
For right now I would. I know XML is the better choice, but I don't have the grasp yet on it to do this.
I have it working:
PHP Code:
<?php
$fp = fopen("comments.txt", "r+");
while(!feof($fp)){
$line_of_text = fgets($fp);
$parts = explode('||', $line_of_text);
echo "<h4>Road #: " . $parts[4] . "</h4> <span class='small'>Posted by: " . $parts[0] . "<br /> Date Posted: " . $parts[1] . "<br />" . "Location: " . $parts[3] . "</span><p><strong>Comments:</strong> " .$parts[5] . "</p>";
}
fclose($fp);
?>
However, onload, it gives me blank Road #: Posted by: etc... How can I get it to show nothing if the file is empty?
I have tried: if(!empty($file)){die}
But it doesn't work.
I would like it not to loop, unless there is at least 1 post.
|
|
|
|
02-01-2008, 10:50 PM
|
#7 (permalink)
|
|
The Frequenter
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
|
I know that you want to use text files for storing your data at the moment, but for future use and for others looking to do similar, I'd like to suggest using SQLite instead - TalkPHP - Introduction to SQLite
Alan
|
|
|
02-01-2008, 10:54 PM
|
#8 (permalink)
|
|
The Acquainted
Join Date: Jan 2008
Posts: 119
Thanks: 21
|
Quote:
Originally Posted by Alan @ CIT
I know that you want to use text files for storing your data at the moment, but for future use and for others looking to do similar, I'd like to suggest using SQLite instead - TalkPHP - Introduction to SQLite
Alan
|
I looked at the php config on our server, doesn't look like they offer SQLite. I wish they did. I have been told to use that before. our version is: PHP Version 4.3.11

|
|
|
|
02-02-2008, 10:19 AM
|
#9 (permalink)
|
|
The Contributor
Join Date: Jan 2008
Location: Brazil
Posts: 77
Thanks: 14
|
Just an addition, you can check if a file exists using the file_exists() function, and the filesize() function will tell you the file size in bytes.
PHP Code:
$file = '/path/to/file.txt';
if (file_exists($file)) {
// the file exists, get the size
$size = filesize($file);
if ($size == 0)
// the file is empty
} else {
// the file is not empty
}
}
|
|
|
02-02-2008, 01:05 PM
|
#10 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
|
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
|
|
|
|
02-04-2008, 05:01 PM
|
#11 (permalink)
|
|
The Acquainted
Join Date: Jan 2008
Posts: 119
Thanks: 21
|
thanks all,
got it working great.
|
|
|
|
02-04-2008, 07:17 PM
|
#12 (permalink)
|
|
The Acquainted
Join Date: Jan 2008
Posts: 119
Thanks: 21
|
I guess I do have one more question... Is there a way to read the file backwards in order to post the older posts at the end and the new ones first inside the text file?
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|