into a database * Friendly user interface to import CSV data into a database * Introduction
Did you know there are data exchange formats besides XML? If the data to exchange is flat, i.e. is not in an hierarchical structure, CSV (comma separated values) format is a good candidate format to import or your export your application data. You can find out more about CSV format in Wikipedia:
http://en.wikipedia.org/wiki/CSV
http://www.php.net/fgetcsv
<?php $arrResult = array(); $handle = fopen("data.csv", "r"); if( $handle ) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $arrResult[] = $data; } fclose($handle); } ?> Note that locale settings may affect how fgetcsv() function works. As noted in the PHP manual, if LANG locale setting is for instance "en_US.UTF-8", files in one-byte encoding are read wrong by this function, so be aware.
An even more user-friendly alternative to import CSV data into a database consists in using the application "Quick CSV import with visual mapping". It is based in the class above. This application helps importing CSV files into a database table allowing to define with CSV file columns are mapped to which columns of the database table. This application can even suggest automatically separator character besides the comma. You may learn more about this application in the following page:
http://i1t2b3.com/2009/01/14/quick-csv-import-with-mapping/
Written by Kumar S
Posted November 4, 2006 at 8:00 am // open the text file $fd = fopen ("pilots.csv", "r"); // initialize a loop to go through each line of the file while (!feof ($fd)) { $buffer = fgetcsv($fd, 4096); // declare an array to hold all of the contents of each //row, indexed echo "n"; // this for loop to traverse thru the data cols // when this is re-created with MySQL use the mysql_num_fileds() function to get // this number for ($i = 0; $i < 5; ++$i){ if ($buffer[$i] == ""){ $buffer[$i] = " "; } // print 's with each index echo "$buffer[$i]n"; } echo "n"; } fclose ($fd); ?>
$file_handle = fopen("Mai2010.csv", "r"); while (!feof($file_handle) ) { $data = fgetcsv($file_handle, 1024); print $data[0] . $data[1]. $data[2] . $data[3] . $data[4] . $data[5] . $data[6] . $data[7] . $data[8] . $data[9] . $data[10] . $data[11] . $data[12] . $data[13] . $data[14] . "; } fclose($file_handle); ?> With this code, Ive been shown the CSV file so I will. My next step is to get changed $data[0], $data[1] and so on, for example, $Flight $Plane. How can one do this?