TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-01-2008, 09:54 PM   #1 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Help 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!
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 02-01-2008, 10:07 PM   #2 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Default

I see some other posts with similar thoughts... but not quite what i am looking for...
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 02-01-2008, 10:14 PM   #3 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

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
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 02-01-2008, 10:41 PM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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 ....
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
buildakicker (02-01-2008)
Old 02-01-2008, 10:51 PM   #5 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Smile

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

Quote:
Originally Posted by Salathe View Post
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 ....
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 02-01-2008, 10:42 PM   #6 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Big Grin

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.
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 02-01-2008, 10:50 PM   #7 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

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
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 02-01-2008, 10:54 PM   #8 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Default

Quote:
Originally Posted by Alan @ CIT View Post
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

__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 02-02-2008, 10:19 AM   #9 (permalink)
The Contributor
 
DeMo's Avatar
 
Join Date: Jan 2008
Location: Brazil
Posts: 77
Thanks: 14
DeMo is on a distinguished road
Default

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
    
}

Send a message via ICQ to DeMo Send a message via MSN to DeMo Send a message via Skype™ to DeMo
DeMo is offline  
Reply With Quote
Old 02-02-2008, 01:05 PM   #10 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

umm...fscanf?
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 02-04-2008, 05:01 PM   #11 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Default

thanks all,
got it working great.
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 02-04-2008, 07:17 PM   #12 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Default

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?
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:56 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design