PHP Classes

PHP CSV to Data Dictionary: Generate data dictionary from CSV file

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 324 All time: 7,202 This week: 60Up
Version License PHP version Categories
csv-dictionary 1.0GNU General Publi...5PHP 5, Databases, Files and Folders
Description 

Author

This class can generate data dictionary from CSV file.

It parses the CSV file and attempts to detect the types of data on each column. It can detect text strings, integers, floats and booleans.

The class can optionally extract the field names from the header row.

It can also generate a SQL query to create a table with the structure of fields of the detected types, as well SQL insert statements to to insert the parsed data into the table rows.

The class can also output the data dictionary in a human readable format on a Web page.

Innovation Award
PHP Programming Innovation award winner
July 2015
Winner


Prize: One subscription to the PDF edition of the PHP Architect magazine
Many applications can import data exported by other programs in the CSV format.

When you want to import CSV data into a database, you need to create the necessary tables to insert the records of CSV data.

This class can help performing that task by examining a CSV file to determine the names and types of the fields of the records of the table to be created.

Manuel Lemos
Picture of Dave Smith
  Performance   Level  
Name: Dave Smith is available for providing paid consulting. Contact Dave Smith .
Classes: 51 packages by
Country: United States United States
Age: 59
All time rank: 608 in United States United States
Week rank: 48 Up7 in United States United States Up
Innovation award
Innovation award
Nominee: 32x

Winner: 7x

Example

<?PHP
/*
instantiate the class
*/
include('csvdict.class.php');
$dict = new csvDict;

/*
process the csv file
we are using the file name for the table name
we could provide our own table name...
$dict->processCSV('sample.csv','myTable');
*/
$dict->processCSV('sample.csv');

/*
inform class of the primary column
we are using the first field in the csv file as the primary column
we could also name it, if the name is known...
$dict->setPrimColumn('id');
*/
$dict->setPrimColumn(0);

/*
inform class that primary column is auto incremented
*/
$dict->isAuto = true;

/*
displaying data dictionary
*/
$display = $dict->getDisplay();
echo
$display.'<hr>';

/*
displaying create table query
*/
$query = $dict->getTableQuery();
echo
$query.'<hr>';

/*
displaying row insert queries
the dataRows property will not be available if the includeData property is false
*/
foreach( $dict->dataRows[0] as $key=>$data ){
   
$query = $dict->getInsertQuery($key);
    echo
$query.'<br>';
}

?>



Details

Class: csvdict Version: 1.0 5/15/2015 Copyright 2015 Wagon Trader This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. Description: This class generates a data dictionary from data supplied in a csv file. It can display a human readable data dictionary. It can return the SQL query statement to create the database table. It can return the SQL query statements to insert the data into the table. Files: csvdict.class.php - Main class example.php - Example usage script sample.csv - Sample csv file Installation: Upload files to a web accessible location on your server (eg. public_html) Configuration: The class is configured to work with most server configurations. Usage: The included example.php file includes examples on instantiating the class and basic operation instructions. Complete method usage is documented in the class.

  Files folder image Files (5)  
File Role Description
Plain text file csvdict.class.php Class Main Class
Accessible without login Plain text file example.php Example Example Usage
Accessible without login Plain text file license.txt Lic. License
Accessible without login Plain text file manual.txt Doc. Documentation
Accessible without login Plain text file sample.csv Data Sample data

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:324
This week:0
All time:7,202
This week:60Up