简体   繁体   中英

Handling dynamic CSV headings in PHP/Mysql

I know the title isn't the most descriptive, but I can't think of a better way to describe it.

Ultimately what I would like to do is this:

I am making a system that handles import of data via CSV from several different companies. Naturally, the format from each company is slightly different, with different amounts of data in each column/heading and a different arrangement.

I could either:

  1. Hard code the file format into the program, which is quick and simple to program, but not abstract or future proof

  2. or provide "dynamicity" by allowing users to configure "file formats" in the system, by dragging and dropping CSV headings and saving presets for each incoming file format.

I would like to do option 2, but how would I go about doing this?

I am guessing a tagging style system would do it... but any advice would be appreciated.

I am using PHP, MySQL, and CodeIgniter 2.0.2 if that helps, and also have access to all the Zend libraries in my app.

Assuming that users will be submitting a subset of possible data types that you define up front, this shouldn't be too tough.

A simple form that allows the user to select the data type and give you the field name in their csv could be used to create the templates you mentioned. Store these in a database table associated with the user and make them choose a template before importing a csv. For customers who have csvs without header rows, a slight variation where they tell you the column numbers for each data type.

I'd also suggest providing pre-defined formats for customers who haven't defined their exports yet. That way they could build their exports to one of your formats.

You could also let users define their default template. That way they wouldn't have to select the template every time they import data if they're always importing data in the same format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM