below sample table i've been working on,
table1
╔════╦══════════════╦═══════════╗════════════════════╗ ║ id ║ file_id ║fieldname ║ from_column_number ║ ╠════╬══════════════╬═══════════╣════════════════════╬ ║ 1 ║ 5566533 ║ mid ║ 4 ║ ║ 2 ║ 5566533 ║ old ║ 2 ║ ║ 3 ║ 5566533 ║ fossil ║ 1 ║ ║ 4 ║ 3232534 ║ old ║ 4 ║ ║ 5 ║ 3232534 ║ mid ║ 3 ║ ║ 6 ║ 3232534 ║ new ║ 2 ║ ║ 7 ║ 3232534 ║ fossil ║ 1 ║ ╚════╩══════════════╩═══════════╝════════════════════╝ i have csv file's id's in column file_id ,their header names , order appear in files in column's fieldname , from_column_number respectively. ie,in above case i've shown 2 file's file_id 5566533 , 3232534.
they have header order
5566533:
fossil old mid 3232534:
fossil new mid old i need load files below table,rearranging way loads each file using above information
for example,the file having id 5566533 has field mid in fourth position in above case.
table2
╔════╦══════════════╦═══════════╗════════════════════╗ ║ id ║ mid ║old ║ fossil ║ ╠════╬══════════════╬═══════════╣════════════════════╬ ║ 1 ║ 422342343 ║ 12222 ║ 342342 ║ ║ 2 ║ 234234 ║ 43234 ║ 3243 ║ ║ 3 ║ 345435355445 ║ 234234 ║ 32432 ║ ║ 4 ║ 3455544534 ║ 2343245 ║ 4234 ║ ║ 5 ║ 345345544454 ║ 432234 ║ 324545 ║ ║ 6 ║ 554345345 ║ 34243 ║ 453242 ║ ║ 7 ║ 77w345544533 ║ 34234455 ║ 5245345 ║ ╚════╩══════════════╩═══════════╝════════════════════╝ i can use plain sql , bit of php purpose.g
i think figured out asking. first, convert field mapping table array. i've done following manually should demonstrate trying do:
// load sample data regarding input file field ordering // --------------------------------------------------------- $csvinfo = array(); $csvinfo[5566533]['mid'] = 4; $csvinfo[5566533]['old'] = 2; $csvinfo[5566533]['fossil'] = 1; $csvinfo[3232534]['old'] = 4; $csvinfo[3232534]['mid'] = 3; $csvinfo[3232534]['new'] = 2; $csvinfo[3232534]['fossil'] = 1; with field mapping information in place can walk through list of files, load them, , extract data care using field mapping information:
// process each of file names loaded csvinfo // -------------------------------------------------- echo "processing input files<br>\n"; foreach ($csvinfo $fileid => $fldmap) { $raw = @file_get_contents("$fileid.csv"); if ( !strlen($raw) ) die("error: unable load $fileid.csv contents!\n"); $data = explode("\n",$raw); $i=0; echo "<br>\n"; echo "importing $fileid.csv<br>\n"; foreach ($data $line) { // skip header line // ---------------- if ( !$i++ || !strlen(trim($line)) ) continue; $flds = explode(',',$line); $midval = trim($flds[$fldmap['mid']-1]); $oldval = trim($flds[$fldmap['old']-1]); $fosval = trim($flds[$fldmap['fossil']-1]); // write fields table here // -------------------------- echo "... line " . ($i-1) . " $line fossil $fosval mid $midval old $oldval<br>\n"; ; } echo ($i-1) . " lines imported $fileid.csv<br>\n"; } the code above generates following debug information:
processing input files importing 5566533.csv ... line 1 f812, o998, b234, m1892 fossil f812 mid m1892 old o998 ... line 2 f915, o123, b454, m9817 fossil f915 mid m9817 old o123 2 lines imported 5566533.csv importing 3232534.csv ... line 1 f179, n723, m886, o912 fossil f179 mid m886 old o912 ... line 2 f791, n237, m868, o129 fossil f791 mid m868 old o129 ... line 3 f917, n372, m688, o291 fossil f917 mid m688 old o291 3 lines imported 3232534.csv here's 3232534.csv file (manually created junk data):
fossil, new, mid, old f179, n723, m886, o912 f791, n237, m868, o129 f917, n372, m688, o291 i'm not going suggest code particularly robust or that. however, it's starting point if i've interpreted question correctly.
Comments
Post a Comment