Jump to content

Getting CSV text into variables so i can insert into database


AdRock

Recommended Posts

I have a CSV file and I want to get each part of the line of text into different variables

 

I have found some code but can't get it to put into seperate variables

 

I have a form with 2 fields where the user can add there own data but if they click a button iwant this function called

 

    function get_start() {
$file_handle = fopen("test.csv", "r");
while (!feof($file_handle) ) {
    $line_of_text = fgetcsv($file_handle, 1024);
    $s_lat == $line_of_text[0];
    $s_long == $line_of_text[1];
}
fclose($file_handle);
    }

 

here is a cut down version of my code

    $s_lat = $_POST['s_lat'];  // what the user can enter
    $s_long = $_POST['s_long'];   // what the user can enter
    //variables for end of route
    $e_street = $_POST['e_street'];
    $e_postcode = $_POST['e_postcode'];
    $e_lat = $_POST['e_lat'];   // what the user can enter
    $e_long = $_POST['e_long'];   // what the user can enter
    //variable for departure time
    $depart = $_POST['time'];

    function get_start() {
$file_handle = fopen("test.csv", "r");
while (!feof($file_handle) ) {
    $line_of_text = fgetcsv($file_handle, 1024);
    $s_lat == $line_of_text[0];
    $s_long == $line_of_text[1];
    print $s_lat." ".$s_long;
}
fclose($file_handle);
    }

    function get_end() {  
        $file_handle = fopen("test.csv", "r");
while (!feof($file_handle) ) {
    $line_of_text = fgetcsv($file_handle, 1024);
    $e_lat == $parts[0];
    $e_long == $parts[1];
    
}
fclose($file_handle);
    }

This regex isn't by me, so I can't gaurantee anything..

 

<?php

$contents = file_get_contents('path/to/your/csv.txt');
preg_match_all('/(?:^|,)(\\"(?:[^\\"]+|\\"\\")*\\"|[^,]*)/', $contents, $matches);

print_r($matches);

?>

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.