Hello all,
I have a CSV file that is uplaoded to my server where a PHP script will parse and return output to JavaScript via AJAX.
The CSV file is made up of two rows. First row contains the column names and the second row contains the data. I am trying to write a script that will parse it in a form that will be usable by Javascript to generate text fields and labels populated with this data. The current method that I have is this:
if ( $_FILES['parameter_file']['tmp_name'] ) {
$paramfile = fopen($_FILES['parameter_file']['tmp_name'], 'r');
$header = fgets($paramfile);
$temp = explode(',', str_replace('"', '', rtrim(ltrim($header))));
$userdata = fgets($paramfile);
$temp2 = explode(',', str_replace('"', '', rtrim(ltrim($userdata))));
for ($k = 0; $k <= sizeof($temp) - 1; $k++) {
$userparam[strtolower($temp[$k])] = $temp2[$k];
}
fclose($paramfile);
}
I can see loads of room for general improvement, feel free to point them out. But the main question is would a json_encode be all I need. Anything more efficient? A better idea?
Thanks all