PDA

View Full Version : csv-class for import-/export-functions in own addons


Vossi
06 Aug 2008, 11:19
I coded this csv-class for vB because I needed a decent import-/export-function for my own addons. This class is published as a so-called 'development-class' and it would be nice if this class is going to be more developed with your help.

Coders can use this simple class in their addons if they need to create backups of tables in the database or import data into it.

Importing a .csv-file:

The class is going to be called like this:
require_once(DIR . '/includes/class_vbcsv.php');
$vbcsv = new vBCSV($vbulletin, $importfile, $optionfields);
$vbcsv->importcsv($importtable);
and the variables are:
$vbulletin = registry object
$importfile = filename incl. path
$optionfields = array with 3 values each line
$importtable = name of table in database to import into
The array $optionfields ist constructed in the following way:
1. value = id of field (0=first field, 1=second field ....)
2. value = option (0=ignore field|1=fieldcontent of 3. value)
3. value = content which replaces the source content if option 1 ist set in in 2. value
For example look at this .csv-file:
id,name,city,userid,username,tel
81,max test,hamburg,302,max,110
82,moritz test,bremen,309,moritz,112
This data needs to get imported with the following options:
$optionfields = array();
$optionfields[0] = '0,0,0'; // field 0 (id) ist getting ignored while import
$optionfields[1] = '3,1,1'; // content of field 3 (userid) ist getting changed into '1' while import
$optionfields[2] = '4,1,admin'; // content of field 4 (username) is getting changed into 'admin' while import
Field id is getting ignored while import (in cases where you don's want the id to get imported and to avoid db-errors), the values for userid and username are getting changed into 1 and admin while import.

If $optionfields is not defined while the class is getting called, the table will be imported like it is without any .

Export into a .csv-Datei:

This function is similar to the import-function:
require_once(DIR . '/includes/class_vbcsv.php');
$vbcsv = new vBCSV($vbulletin, $exportfile, $optionfields);
$vbcsv->exportcsv($exporttable);
and the variables are:
$vbulletin = registry object
$exportfile = filename incl. path
$optionfields = array with 3 values each line
$exporttable = content which replaces the source content if option 1 ist set in in 2. value
The array $optionfields ist constructed in the following way:
1. value = id of field (0=first field, 1=second field ....)
2. value = option (0=ignore field|1=fieldcontent of 3. value)
3. value = content which is getting used while export when option 1 ist used in 2. value.
The way of using $optionfields is similar to the import-function.

If $optionfields is not defined while using the export-function, the table is getting exported without changes.