Page 1 of 1

function __import__csv

PostPosted: Thu Nov 27, 2008 7:33 am
by meta
Hi Steve, hi all,

i have a little problem when importing csv-data to my database. I was using the import-filter called function __import__csv like follows:

Code: Select all
function __import__csv(&$data, $defaultValues=array()){
    // build an array of Dataface_Record objects that are to be inserted based
    // on the CSV file data.
    $records = array();
   
    // first split the CSV file into an array of rows.
    $rows = explode("\n", $data);
    foreach ( $rows as $row ){
        // We iterate through the rows and parse the values to that they can be stored in a Dataface_Record object */
        list($v_b3_ID, $B3_ID_result, $time, $b_m3_FM, $m_m3_FM, $b_m3_oTM, $m_m3_oTM, $timestamp) = explode(';', $row);
        $record = new Dataface_Record('versuch', array());
       
        // We insert the default values for the record.
        $record->setValues($defaultValues);
       
        // Now we add the values from the CSV file.
        $record->setValues(
            array(
             'v_b3_ID'=>$v_b3_ID,
                'B3_ID_result'=>$B3_ID_result,
            'time'=>$time,
                'b_m3_FM'=>$b_m3_FM,
                'm_m3_FM'=>$m_m3_FM,               
            'b_m3_oTM'=>$b_m3_oTM,
                'm_m3_oTM'=>$m_m3_oTM,
            'timestamp'=>$timestamp
                 )
            );
       
        // Now add the record to the output array.
        $records[] = $record;
    }
   
    // Now we return the array of records to be imported.
    return $records;
}


It works fine apart from two mistakes you can see in the following picture.

1. The headline labels are imported which they don't.
2. There is imported one empty row too much.

How can I avoid this? I found http://xataface.com/forum/viewtopic.php?t=4156#20888 in the forum but was wondering if there could be done something in the function itself in my tables delegate class without changing the export_csv.php file. Here is my temporary table before importing the records:

Image

Thanks a lot for any hints solving these probs

Markus

PostPosted: Fri Nov 28, 2008 10:53 am
by shannah
You could just add some if statements to cover for these issues.

First, before the foreach statement you could do an
array_shift($rows);

to get rid of the first row.

Finally, you could do a check for empty rows inside the foreach loop

if ( !trim($row) ) continue;

-Steve

PostPosted: Fri Nov 28, 2008 11:34 am
by meta
Thank you very much.

Markus