简体   繁体   中英

PHP for read csv file and put data array into session variables

I have this csv file with; separated fields. I want to be able to read the file and put the values into session variables to insert them into a mySQL database.

I have this code for reading de file and create the array. And now I want to create the session variables to pass them to the insert.php file.

Content of my csv file:

"S11PBMS02.14045";16.93;5;NULL;NULL;1;NULL;217.64;1;NULL;NULL;1;NULL;1;"52X45-CAR10/1XCAR10/1";0;0;0;"140";"PB Mini Stripe 02";46;#;"S11PBMS02.14045";"PB Mini Stripe 02";"Vichy Estofos";"PBMS02";"140";"52X45-CAR10/1XCAR10/1";"090";217.64;330.63;233.73;330.63;"C147";"02(Fawn)";41012;41144;;;;;;;;;;;;;;;"*";41012;;;;;;;

My code:

<?php
$arrCSV = array();
if (($handle = fopen("style.dai", "r")) !==FALSE) {
$key = 0;
while (($data = fgetcsv($handle, 0, ";")) !==FALSE) {
   $c = count($data);
   for ($x=0;$x<$c;$x++) {
   $arrCSV[$key][$x] = $data[$x];
   }
   $key++;
} 
fclose($handle);
} 
echo "<pre>";
echo print_r($arrCSV);
echo "</pre>";
?> 

Just write your $arrCSV to the session variable, like so: $_SESSION['arrCSV'] = $arrCSV

If you haven't called session_start(), do so first.

Its pretty much the same:

<?php
session_start();
$arrCSV = array();
if (($handle = fopen("style.dai", "r")) !==FALSE) {
$key = 0;
while (($data = fgetcsv($handle, 0, ";")) !==FALSE) {
   $c = count($data);
   for ($x=0;$x<$c;$x++) {
   $arrCSV[$key][$x] = $data[$x];
   $_SESSION['values'][$x] = $data[$x];
   }
   $key++;
} 
fclose($handle);
} 
echo "<pre>";
echo print_r($arrCSV);
echo print_r($_SESSION['values']);
echo "</pre>";
?> 

You could also just set the session value to be the same as $arrCSV

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM