Forum Moderators: coopster

Message Too Old, No Replies

need help processing a text file, line by line with fgets...

fgets and while loop only working once

         

vacorama

8:09 pm on Jun 18, 2005 (gmt 0)

10+ Year Member



hello,
is there anyone out there who can help me out with this thing? goin nuts over here. i got a long text file, comma delimeted with each line representing a product i need to import.. i've done a bunch of other things with this file so far using the method below, but now i want to change the price of each item by exploding a line, changing the price element, then imploding back to a string for 'fputs' in my destination file. It works on the first line of text fine, and loops through all the other lines and 'fputs' them as well with no errors, but doesn't operate on the price like with the first line. all i can see is that my $line array keeps adding to the end instead of resetting.. i tried using reset and unset, but wasn't working either, was probably doing it wrong.. am i missing something obvious?

$fs = fopen( 'source.txt', 'r' );
$formatted= fopen('destination.txt', 'w');

while (!feof( $fs) )
{

$line = fgets( $fs);
$line = explode(",", $line);
$line[3] = $line[3] * 1.80; # change the price of item
print "$tmp[3]"; # test result

$line = implode(",", $line); #change back to string

fputs($formatted, $line);

}

#closes files
fclose( $fs );
fclose( $formatted );

Timotheos

8:45 pm on Jun 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This thread has the basics of what you need
[webmasterworld.com...]

madpenguin2

3:02 am on Jun 19, 2005 (gmt 0)

10+ Year Member



I've had problems like this before. I usually solved it by casting the csv field value. I think i used floatval().. but, then again, it's been a while..

$line[3] = floatval($line[3]) * 1.80

Essentially, you're telling php that the $line[3] variable is a float value.

Brett

vacorama

5:30 pm on Jun 27, 2005 (gmt 0)

10+ Year Member



ahh... thanks a lot guys.. the floatval didn't work though. I'll check out that CSV thing, definitley looks better suited for what i'm doing.. thanks a bunch..