TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Echoing Text File Rows into Array Display... (http://www.talkphp.com/general/2168-echoing-text-file-rows-into-array-display.html)

buildakicker 02-01-2008 09:54 PM

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!:-D

buildakicker 02-01-2008 10:07 PM

I see some other posts with similar thoughts... but not quite what i am looking for...

Alan @ CIT 02-01-2008 10:14 PM

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

Salathe 02-01-2008 10:41 PM

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($fp2048'|')));
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 ....

buildakicker 02-01-2008 10:42 PM

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.

Alan @ CIT 02-01-2008 10:50 PM

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

buildakicker 02-01-2008 10:51 PM

Thanks for the post. Is that system more streamlined that what I have?

Quote:

Originally Posted by Salathe (Post 10108)
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($fp2048'|')));
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 ....


buildakicker 02-01-2008 10:54 PM

Quote:

Originally Posted by Alan @ CIT (Post 10113)
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

*!*

DeMo 02-02-2008 10:19 AM

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
    
}



xenon 02-02-2008 01:05 PM

umm...fscanf?

buildakicker 02-04-2008 05:01 PM

thanks all,
got it working great.

buildakicker 02-04-2008 07:17 PM

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?


All times are GMT. The time now is 09:48 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0