Jump to content

Extracting data from CSV file


UG1234

Recommended Posts

Hi, i am new to PHP and i am struggling with importing data from a CSV file. Basicly i am creating a map and trying to draw a shape on it and i am pulling the coordinates to plot the shape from a CSV file.

 

My test.csv file consists of 3 set of coordinates and looks like this;

 

337339, 403450

354810, 433189

224600, 689900

 

i would like to pull in each line to fill variables p1, p2 and p3.

 

Below is my first attempt of this

 

var p1 = new OpenLayers.Geometry.Point(

<?PHP

$file_handle = fopen("test.csv"="r");

 

$line_of_text = fgetcsv($file_handle, 1024);

print $line_of_text[0] . $line_of_text[1]. "<BR>";

}

fclose($file_handle);

?>

);

 

var p2 = new OpenLayers.Geometry.Point(

<?PHP

$file_handle = fopen("test.csv"="r");

 

$line_of_text = fgetcsv($file_handle, 1024);

print $line_of_text[2] . $line_of_text[3]. "<BR>";

}

fclose($file_handle);

?>

);

var p3 = new OpenLayers.Geometry.Point(

<?PHP

$file_handle = fopen("test.csv"="r");

 

$line_of_text = fgetcsv($file_handle, 1024);

print $line_of_text[4] . $line_of_text[5]. "<BR>";

}

fclose($file_handle);

?>

);

 

Thanks for any help in advance

 

Link to comment
https://forums.phpfreaks.com/topic/189635-extracting-data-from-csv-file/
Share on other sites

The first issue I see is the fact that you are opening and closing the file each time and each time you open, you will read the same line.

 

The second issue I see is that you expect the array index to have advanced each time, when in fact, as you have 2 values on each line, then it will use indeces [0] and [1] for each line read

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.