Welcome Guest, Not a member yet? Register   Sign In
read csv file problem
#1

[eluser]ciPhil[/eluser]
the code below works until I read the data from a file.
php notice is "undefined offset"
If I use an extra foreach loop all is ok
it is only when I try and access an individual field say $text_field[2]
that this notice is generated, the echo $text_field[2] prints its data ok.

I am still trying to learn and dont know if this problem is my code, a file problem, ci or php.

$this->load->helper('file');

$this->output->enable_profiler(TRUE);



//$fake_file = "l1f1~l1f2~l1f3~l1f4\nl2f1~l2f2~l2f3~l2f4";
$fake_file = read_file('./uploads/fakefile.txt');

$line_text = explode("\n",$fake_file);

foreach ($line_text as $single_line)
{
$text_field = explode('~',$single_line);

echo $text_field[2];

echo "<br />";


}

Thanks for reading
cheers
Phil
#2

[eluser]skattabrain[/eluser]
Hi Phil, I'm just tagging along for the ride on this, are you taking users CSV files, uploading them and preparing them for a DB insert or something similar?
#3

[eluser]Sean Murphy[/eluser]
If you're trying to work with a CSV file I suggest using the core PHP function fgetcsv: http://us2.php.net/manual/en/function.fgetcsv.php
#4

[eluser]ciPhil[/eluser]
[quote author="skattabrain" date="1203716069"]Hi Phil, I'm just tagging along for the ride on this, are you taking users CSV files, uploading them and preparing them for a DB insert or something similar?[/quote]

Yes you are spot on, trouble is incoming data is not in great shape
#5

[eluser]ciPhil[/eluser]
Thanks Sean,
fgetcsv solves the problem,
cheers
Phil
#6

[eluser]skattabrain[/eluser]
[quote author="ciPhil" date="1203721430"][quote author="skattabrain" date="1203716069"]Hi Phil, I'm just tagging along for the ride on this, are you taking users CSV files, uploading them and preparing them for a DB insert or something similar?[/quote]

Yes you are spot on, trouble is incoming data is not in great shape[/quote]

yeah i hear that, i've customized a script called php csv importer (abandoned script) and my users send me some files in pretty poor shape as well. plus ms excel exports to csv rather poorly if you ask me, try telling a user MS is the bad guy, they laugh. but in the end my efforts seem to be in vain as they rather email me the files directly. grrrrr.




Theme © iAndrew 2016 - Forum software by © MyBB