Reading CSV file to string or javascript array

I am having a very specific problem, so I hope someone can help. I am creating a CakePHP map application. I'm currently trying to import some data into my application for manipulation, I have a google docs CSV file (I also have a local version), and I also use a custom js library. So, I have an ad that looks like this:

mapbox.markers.layer().csv('csvstring');

csvstring matches the standard string of CSV data, I looked at some examples on the mapbox website, but all had hardcoded data, for example. csvstring = 'lat,lon\n,0,0';

Is there an easy way to read either a google document, or read a file from my CakePHP directory in a string variable or, if not, an array? I tried the internet for several days and could not find a solution. If it can be allowed by PHP to do the dirty work and then pass the string to js var, that would be great too. In the end, it should be a string or an array of strings.

I saw a lot of people recommending getting to know the Google Docs API, but in fact, I am nooby with both CakePHP and javascript, and it took a lot of work to go this far, my head is full and I just I don’t think I can have another big reading session, time is running out on my project. Also, don’t assume that I know ANYTHING, the complete response of idiots would be greatly appreciated, thanks in advance.

0
source share
2 answers

If the file is local to where your application is running (located on the same server), you can use simple input / output of PHP files.

<?php
$filename = "/YOUR/FILE/URL/HERE.csv";
$file = fopen( $filename, "r" );
if( $file == false ) {
    echo "Error!";
    exit();
}
$csv_string = fread( $file, $filesize );
fclose( $file );
?>

mapbox.markers.layer().csv("<?php echo $csv_string; ?>");
//Make sure you put the quotes around the php tag. Otherwise the JavaScript will not recognize it as a string.

(Produced largely from http://www.tutorialspoint.com/php/php_files.htm ). Hope this is what you are looking for.

0
source

Source: https://habr.com/ru/post/1528887/


All Articles