This topic is locked

Map excel import column names to different table field names

8/27/2013 11:10:40 AM
PHPRunner Tips and Tricks
F
FunkDaddy author

I needed to import an excel file with more "user friendly" column names than those store in my database.
Ex: I want to import an excel file with a column named "EmployeeID" and need it to map to my database table with a field where the actual column name for that same field is "LinkEmployeeID".
So, in order to map columns with different names/labels during your import you can apply this hacky solution:

  1. Modify your table query view inside PHPR and add the column labels you have in Excel by placing blank alias columns.

    Ex:

    SELECT

    ChildID,

    LinkEmployeeID,

    LinkDepartmentID,

    LinkFunctionID,

    '' AS EmployeeID /blank alias field /

    '' AS DepartmentID /blank alias field /

    '' AS FunctionID /blank alias field /

    FROM

    your_table
  2. In PHPR "Before Insert Record" event of the import you can now assign the import value from excel to your table by declaring $values['LinkEmployeeID'] = $rawvalues['EmployeeID'];
  3. Make sure that your return statement inside that event page is "return false", otherwise PHPR will attempt to insert into your table the alias name you created in the query but that field doesn't actually exist in your underlying table. Hence, you must stop that insert from taking plance.
  4. Now, since you stopped the insert you must now hijack that insertion (or whatever else you want to do with the imported data) by adding your own custom query in that same event page:

    Ex: $sqlNSERT = "INSERT INTO your_table (LinkEmployeeID, LinkDepartmentID, LinkFunctionID) values ("'.$rawvalues['EmployeeID'].'",'".$rawvalues['DepartmentID']."','".$rawvalues['FunctionID']."')";

    CustomQuery($sqlINSERT);
  5. Make sure you properly escape bad characters when doing your query insertion (use addslashes or mysqli_real_escape_string php functions)
    Lastly, you can get creative and use this to make mass updates to your existing lists instead of only using the excel import feature to add records to your tables... by always returning false in the Before Insert Statement you can grab that excel data which is stored in memory during the import to mass update other tables, etc.

S
sradforth 10/16/2013

If it helps anyone I implemented my own importer in the end after finding this http://code.google.com/p/php-excel-reader/ for reading the excel files in PHP.
Just placed a button to 'custom import', added my custom import.php file to the source directory and in PHPRunner added a button to call it.
Don't get me wrong, PHPRunner import is amazing out of the box functionality but like you say getting column names remapped makes you jump through lots of hoops and easily fails if the data isn't structured just right.