Noob question - CSV import

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

Noob question - CSV import

Postby demon » Fri Dec 26, 2008 10:11 pm

I read the articles at Documentation, but i still cant understand how i can create a delegate class to import csv files.

Could you explain me step-by-step.

Don't need to explain the source code of function: function __import__csv
demon
 
Posts: 1
Joined: Fri Dec 26, 2008 10:04 pm

Postby shannah » Mon Dec 29, 2008 8:50 pm

The __import__xxx() methods are intended for you to define how to import data in a specific format.

In the simplest example, suppose you want to be able to import a list of email addresses in a text file of the form:



Then we might write an import filter as follows:
Code: Select all
function __import__emails($data){
    // $data is a list of email addresses - one per line

    // Split the addresses into an array.
    $addresses = explode("\n", $data);

    // Create an array to house our records for importing.
    $records = array();

    foreach ( $addresses as $addr ){

        // Create record on the 'addresses' table
        $record = new Dataface_Record('addresses', array());
        $record->setValue('email', $addr);
        $records[] = $record;
        unset($record);
    }
    // Return the array of records that we will be importing.
    return $records;
}


In the case of CSV files it's just a little more complicated because we need to parse the CSV format, rather than just taking one value per line as we did in this example. But we make use of PHP's fgetcsv() function to help us with this.
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 20 guests

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