function __import__csv

A place for users and developers of the Xataface to discuss and receive support.

function __import__csv

Postby meta » Thu Nov 27, 2008 7:33 am

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
meta
 
Posts: 30
Joined: Mon Nov 12, 2007 8:30 am
Location: Berlin, Germany

Postby shannah » Fri Nov 28, 2008 10:53 am

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
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby meta » Fri Nov 28, 2008 11:34 am

Thank you very much.

Markus
meta
 
Posts: 30
Joined: Mon Nov 12, 2007 8:30 am
Location: Berlin, Germany


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 9 guests

cron
Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved