开发者

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

开发者 https://www.devze.com 2023-03-15 18:34 出处:网络
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 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 crea开发者_如何学Pythonte 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>";
?> 


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


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消