Reputation: 21
I have a list(data1.php) which contains data like below:
06/26/2015,33.51718,-81.71856,0.0,4001,15:39:57,39256,1,0.0,13.6,
06/26/2015,33.51721,-81.71858,0.0,4001,15:40:57,39256,1,0.0,13.7,
06/26/2015,33.51720,-81.71860,0.0,4001,15:41:57,39256,1,0.0,13.6,
06/26/2015,33.51719,-81.71858,0.0,4001,15:42:58,39256,1,0.0,13.7,
06/26/2015,33.51720,-81.71860,0.0,4001,15:43:58,39256,1,0.0,13.6,
I am trying to retrieve only the 2nd and 3rd data(lat and long). The out put file has results like below:
Lat:33.51718
Long:-81.71856
Lat:33.51720
Long:-81.71860
Lat:33.51720
Long:-81.71860
It is skipping every other line. Here is my code. Please help.
<?php
$fn = fopen("data1.php",'r') or die("fail to open file");
$fp = fopen('output.php', 'w') or die('fail to open output file');
while($row = fgets($fn)) {
echo fgets($fn). "<br";
$num = explode(",", $row);
$lat = $num[1];
$long = $num[2];
echo "<p>Lat: {$lat}</p>";
echo "<p>Long: {$long}</p>";
fwrite($fp, "Lat:$lat\n");
fwrite($fp, "Long:$long\n");
}
fclose($fn);
fclose($fp);
?>
Upvotes: 1
Views: 38
Reputation: 4302
Try this one :
<?php
$fn = fopen("data1.php",'r') or die("fail to open file");
$fp = fopen('output.php', 'w') or die('fail to open output file');
$stop = "0";
while($row = fgets($fn)) {
$num = explode(",", $row);
$lat = $num[1];
$long = $num[2];
if($stop == "1" or $stop == "2"){
echo "<p>Lat: {$lat}</p>";
echo "<p>Long: {$long}</p>";
fwrite($fp, "Lat:$lat\n");
fwrite($fp, "Long:$long\n");
}else{}
$stop++;
}
fclose($fn);
fclose($fp);
?>
Upvotes: 0
Reputation: 1063
Your problem is right here:
while($row = fgets($fn)) {
echo fgets($fn). "<br";
You read 1 line from the file and immediately after that you read and echo another line without actually processing it. Remove the echo and you should be fine
Upvotes: 1