I've been struggling with an odd problem for a couple days. I'm using the File_CSV_DataSource library to parse a csv file into an array ($data) and then transferring that data into a different array ($attributes) in order to load the records into a db.
When creating the $attributes array I'm getting an undefined index error on the first key in $data. Displaying the value of $data prior to this reveals that the key exists and has data, so I'm not sure why the error is being generated.
Any thoughts on what I'm doing wrong? Thanks!
Here is the class I'm working with:
class testArray extends CI_Controller
{
function index()
{
$this->load->view('testArray/upload_form', array('error' => ' ' ));
}
function upload_files()
{
$config['upload_path'] = '/var/www/uploaded_files/';
$config['allowed_types'] = 'csv|txt';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$config['max_size'] = '200000';
$config['file_name'] = 'test_data.csv';
$this->upload->initialize($config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$data['records'] = $this->import_data($data['upload_data']['full_path']);
$this->load->view('testArray/upload_success', $data);
}
}
function import_data($file = FALSE)
{
$i = 0; $y = 0;
if(!$file)
{
die('cannot import...the file was not passed to import_data');
}
$data = New File_CSV_DataSource;
if($data->load($file))
{
if($data->isSymmetric())
{
foreach($data->connect() as $item)
{
var_export($item);
// map the file headers to the table headers
$attributes = array(
'site_id' => $item['Site Id'],
'first_name' => $item['First Name'],
'last_name' => $item['Last Name'],
'address' => $item['Address'],
'city' => $item['City'],
'state' => $item['State'],
'zip' => $item['Zip'],
'qualification' => $item['Qualified/Unavailable'],
'program_name' => $item['Program Name'],
'confirmation_number' => $item['Confirmation Number'],
'arrival_date' => $item['Arrival Dt'],
'tour_id' => $item['Tourid'],
'owner_flag' => $item['Owner Flag']
);
var_export($attributes);
die();
}
}
else
{
$array = $data->getAsymmetricRows(); //get the rows that don't match the header
print_r($array);
die('The file has rows that don\'t match the length of the header');
}
}
return array('created' => $i, 'updated' => $y);
}
}
The above code creates this output:
array ( 'Site Id' => 'ANAHEIM', 'First Name' => 'Scott', 'Last Name' => 'McClung', 'Address' => '1 Main St', 'City' => 'Las Vegas', 'State' => 'NV', 'Zip' => '89148', 'Program Name' => 'Motel', 'Qualified/Unavailable' => 'UNQUALIFIED', 'Arrival Dt' => '04/24/2012', 'Tourid' => '', 'Confirmation Number' => '1234567RCNA', 'Owner Flag' => 'Y', )
A PHP Error was encountered
Severity: Notice
Message: Undefined index: Site Id
Filename: controllers/testArray.php
Line Number: 58
array ( 'site_id' => NULL, 'first_name' => 'Scott', 'last_name' => 'McClung', 'address' => '1 Main St', 'city' => 'Las Vegas', 'state' => 'NV', 'zip' => '89148', 'qualification' => 'UNQUALIFIED', 'program_name' => 'Motel', 'confirmation_number' => '1234567RCNA', 'arrival_date' => '04/24/2012', 'tour_id' => '', 'owner_flag' => 'Y', )