PDA

View Full Version : Can't read last row of a text db


Alex Piotto
08-19-2002, 03:41 PM
Hi everybody!

I am trying to read a db like this:

id;author;title;usdprice;
0;Agatha Cristie;Evil under the sun;50;
1;Arthur Conan Doyle;A study in Scarlet;75;
2;Raymond Chandler;Farewell my Lovely;28;
3;Stefano Benny;Hearth;35;
4;Alex Ross;Maputo by night;15;

with this code:

<?
$fname="test.txt";
$fp=fopen($fname,"r") or die("Error!");
$line = fgets($fp,1024);
while(!feof($fp))
{
list($id,$author,$title,$usdprice) = split( ";", $line, 4 );

echo "$id - $author - $title - $usdprice<br>";

$line = fgets( $fp, 1024 );
}
fclose($fp);
?>

but I can't see the last row of the db......?

What's wrong?

Alex :confused:

Ökii
08-19-2002, 05:58 PM
Does the textfile actually have a carriage return right at the end?
If it ends on the same line as the last bits of data, the final line wouldn't be read.

When you build the file, do you do
$writestring .= $id.';'.$author.';'.$title.';'.$usdprice.'\n';
or
$writestring .= '\n'.$id.';'.$author.';'.$title.';'.$usdprice;
?

Only the first method would allow the last line to be read.

On a subnote, you might want to look at the fgetcsv() function (http://www.php.net/manual/en/function.fgetcsv.php) in the manual.

Alex Piotto
08-19-2002, 07:36 PM
Thanks for answering!
Well, there is not a return after the last line... and now I know why I can't read the last line. I'll check the fgetcsv function...

Alex