在PHP中更改数组以进行CSV上载

时间:2017-04-05 21:22:15

标签: php sql arrays database csv

我的PHP代码中有一个包含大约200个列元素的数组。它看起来像这样:

$

就像我说的那样,这种情况持续到大约199/200。我今天发现CSV将保持这个顺序,但他们在随机顺序中添加了大约30个字段。一些将在前20个,一些在110和120之间。是否有更好的做法在这里添加新元素?我已经将它们添加到了他们需要的数据库中,但现在我需要将它们放在数组中的正确位置,我不知道是否有一种方法可以不重新编号整个数组。

更新 完整代码,除了编码长或重复元素的地方

last parameter

1 个答案:

答案 0 :(得分:1)

使用包含列名的第一行导入CSV文件。

允许您定义CSV中列名称与要导入数据的表格中的列名称之间的转换。

<?php

//  $c_colslist is an array of name/value pairs. The name matches a name in the CSV, 
//  while the value is the name of the column in the table where the value is stored.
//  If there are changes in the CSV, you only have to edit this array and the table staging.
//  If the change is only a change in the order of the fields, you do not have to edit anything.
$c_colslist = array(
    'orderNumber' => 'orderNumber',
    'place' => 'place',
    'workOrderNum' => 'workOrderNum',
    'Column1' => 'userName'
);


//  $csvCols is an array containing the list of column names found in the CSV file, in the order they are found.
$csvCols = array();

//  

$file = $_FILES["file"]["tmp_name"];
$handle = fopen($file, "r");


//  $rowIDs will contain a list of the unique IDs of all inserted rows.
$rowIDs = array();

$rowno = 0;
while(!feof($handle)){
    //  Get the row of data.
    //  If 1st row, it has the column names
    $rowno++;
    $row = fgetcsv($handle, 0, ",");
    if($rowno == 1) {
        //  Save the column name list
        $csvCols = $row;
        //  Pre-create the insert statement (all but the data)
        $qstr = "";
        $qstr .= "INSERT INTO `staging` (";
        $cols = array();
        //  $col_idx contains a list of indexes for each CSV column name (ie. the order they are found in the file)
        $col_idx = array(0);
        $colno = 0;
        foreach($csvCols as $idx => $c_colname) {
            $cols[$colno] = "`".$c_colslist[$c_colname]."`"
            $col_idx[$c_colname] = $colno;
            $colno++;
        }
        $qstr .= implode(',',$cols). ") ";
    } else {
        //  Build the INSERT statement
        $mqstr = $qstr; //  Start with the query string created when the first row was read.
        $mqstr .= ") VALUES (";
        //  Get the values in the same order as the columns.
        $vals = array();
        foreach($csvCols as $idx => $c_colname) {
            $val[] = "'".$row[$col_idx[$c_colname]]."'";
        }
        $mqstr .= implode(',',$vals);
        $mqstr .= ");";
        $query = mysqli_query($connect, $mqstr);
        $rowIDs[] = mysqli_insert_id($connect);
    }

}

//  Now we can copy the records into the other tables.

/*INSERT INTO CLIENTS TABLE FROM STAGING TABLE*/
$q2 .= "INSERT INTO clients (`orderNumber`,`place`,`workOrderNum`,`lowSideMIUNum`,`highSideMIUNum`,`accountNum`,`custName`,`address`,`locID`,`date`,`utility`,`serialNumber`,`serviceName`,`address2`,`servicePreformed`)";
$q2 .= " SELECT `orderNumber`,`place`,`workOrderNum`,`lowSideMIUNum`,`highSideMIUNum`,`accountNum`,`custName`,`address`,`locID`,`date`,`utility`,`serialNumber`,`serviceName`,`address2`,`servicePreformed`";
$q2 .= " FROM `staging`";
$q2 .= " WHERE `StageID` IN (";
$q2 .= implode(',',$rowIDs);
$q2 .=  ");";
$query = mysqli_query($connect, $q2);

/*INSERT INTO METERS TABLE FROM STAGING TABLE*/
$q3 .= "INSERT INTO meters (workOrderNum`,`lowSideMIUNum`,`highSideMIUNum`,`accountNum`,`custName`,`address`,`locID`,`utility`,`serialNumber`,`serviceName`,`bypassSize`,`meterSize`,`meterType`,`manufacturer`,`registration`,`technician`,`linePressurePSI`,`lat`,`lon`,`lowSideRrBefore`,`highSideRrBefore`,`firesideRrBefore`,`lowSideRrAfter`,`highSideRrAfter`,`firesideRrAfter`,`vgOxygen`,`vgCombustGas`,`vgCarbonMon`,`vgHydroSulf)";
$q3 .= " SELECT workOrderNum`,`lowSideMIUNum`,`highSideMIUNum`,`accountNum`,`custName`,`address`,`locID`,`utility`,`serialNumber`,`serviceName`,`bypassSize`,`meterSize`,`meterType`,`manufacturer`,`registration`,`technician`,`linePressurePSI`,`lat`,`lon`,`lowSideRrBefore`,`highSideRrBefore`,`firesideRrBefore`,`lowSideRrAfter`,`highSideRrAfter`,`firesideRrAfter`,`vgOxygen`,`vgCombustGas`,`vgCarbonMon`,`vgHydroSulf ";
$q3 .= " FROM staging";
$q3 .= " WHERE `StageID` IN (";
$q3 .= implode(',',$rowIDs);
$q3 .=  ");";
$query = mysqli_query($connect, $q3);

/*INSERT INTO TESTS TABLE FROM STAGING TABLE*/
$q4 .= "INSERT INTO `tests` (`workOrderNum`,`lowSideMIUNum`,`highSideMIUNum`,`accountNum`,`custName`,`address`,`locID`,`date`,`utility`,`serialNumber`,`serviceName`,`test1TestRateGPM`,`test1MeterVol`,`test1TesterVol`,`test1Accuracy`,`test1CorrectAcc`,`test2TestRateGPM`,`test2MeterVol`,`test2TesterVol`,`test2Accuracy`,`test2CorrectAcc`,`test3TestRateGPM`,`test3MeterVol`,`test3TesterVol`,`test3Accuracy`,`test3CorrectAcc`,`test4TestRateGPM`,`test4MeterVol`,`test4TesterVol`,`test4Accuracy`,`test4CorrectAcc`,`test5TestRateGPM`,`test5MeterVol`,`test5TesterVol`,`test5Accuracy`,`test5CorrectAcc`,`test6TestRateGPM`,`test6MeterVol`,`test6TesterVol`,`test6Accuracy`,`test6CorrectAcc`,`test7TestRateGPM`,`test7MeterVol`,`test7TesterVol`,`test7Accuracy`,`test7CorrectAcc`,`test8TestRateGPM`,`test8MeterVol`,`test8TesterVol`,`test8Accuracy`,`test8CorrectAcc`,`inletValveSize`,`InletValveType`,`inletValveCond`,`outletValveSize`,`outletValveType`,`outletValveCond`,`bypassValveSize`,`bypassValveType`,`bypassValveCond`,`vaultLength`,`vaultWidth`,`vaultHeight`,`meterLocation`,`testPort`,`testPortInstalled`,`testPortSize`,`picture`,`comments`,`testResults`,`retest`,`test1TestRateGPM2`,`test1MeterVol2`,`test1TesterVol2`,`test1Accuracy2`,`test1CorrectAcc2`,`test2TestRateGPM2`,`test2MeterVol2`,`test2TesterVol2`,`test2Accuracy2`,`test2CorrectAcc2`,`test3TestRateGPM2`,`test3MeterVol2`,`test3TesterVol2`,`test3Accuracy2`,`test3CorrectAcc2`,`test4TestRateGPM2`,`test4MeterVol2`,`test4TesterVol2`,`test4Accuracy2`,`test4CorrectAcc2`,`test5TestRateGPM2`,`test5MeterVol2`,`test5TesterVol2`,`test5Accuracy2`,`test5CorrectAcc2`,`test6TestRateGPM2`,`test6MeterVol2`,`test6TesterVol2`,`test6Accuracy2`,`test6CorrectAcc2`,`test7TestRateGPM2`,`test7MeterVol2`,`test7TesterVol2`,`test7Accuracy2`,`test7CorrectAcc2`,`test8TestRateGPM2`,`test8MeterVol2`,`test8TesterVol2`,`test8Accuracy2`,`test8CorrectAcc2`) ";
$q4 .= "SELECT `workOrderNum`,`lowSideMIUNum`,`highSideMIUNum`,`accountNum`,`custName`,`address`,`locID`,`date`,`utility`,`serialNumber`,`serviceName`,`test1TestRateGPM`,`test1MeterVol`,`test1TesterVol`,`test1Accuracy`,`test1CorrectAcc`,`test2TestRateGPM`,`test2MeterVol`,`test2TesterVol`,`test2Accuracy`,`test2CorrectAcc`,`test3TestRateGPM`,`test3MeterVol`,`test3TesterVol`,`test3Accuracy`,`test3CorrectAcc`,`test4TestRateGPM`,`test4MeterVol`,`test4TesterVol`,`test4Accuracy`,`test4CorrectAcc`,`test5TestRateGPM`,`test5MeterVol`,`test5TesterVol`,`test5Accuracy`,`test5CorrectAcc`,`test6TestRateGPM`,`test6MeterVol`,`test6TesterVol`,`test6Accuracy`,`test6CorrectAcc`,`test7TestRateGPM`,`test7MeterVol`,`test7TesterVol`,`test7Accuracy`,`test7CorrectAcc`,`test8TestRateGPM`,`test8MeterVol`,`test8TesterVol`,`test8Accuracy`,`test8CorrectAcc`,`inletValveSize`,`InletValveType`,`inletValveCond`,`outletValveSize`,`outletValveType`,`outletValveCond`,`bypassValveSize`,`bypassValveType`,`bypassValveCond`,`vaultLength`,`vaultWidth`,`vaultHeight`,`meterLocation`,`testPort`,`testPortInstalled`,`testPortSize`,`picture`,`comments`,`testResults`,`retest`,`test1TestRateGPM2`,`test1MeterVol2`,`test1TesterVol2`,`test1Accuracy2`,`test1CorrectAcc2`,`test2TestRateGPM2`,`test2MeterVol2`,`test2TesterVol2`,`test2Accuracy2`,`test2CorrectAcc2`,`test3TestRateGPM2`,`test3MeterVol2`,`test3TesterVol2`,`test3Accuracy2`,`test3CorrectAcc2`,`test4TestRateGPM2`,`test4MeterVol2`,`test4TesterVol2`,`test4Accuracy2`,`test4CorrectAcc2`,`test5TestRateGPM2`,`test5MeterVol2`,`test5TesterVol2`,`test5Accuracy2`,`test5CorrectAcc2`,`test6TestRateGPM2`,`test6MeterVol2`,`test6TesterVol2`,`test6Accuracy2`,`test6CorrectAcc2`,`test7TestRateGPM2`,`test7MeterVol2`,`test7TesterVol2`,`test7Accuracy2`,`test7CorrectAcc2`,`test8TestRateGPM2`,`test8MeterVol2`,`test8TesterVol2`,`test8Accuracy2`,`test8CorrectAcc2` ";
$q4 .= "FROM `staging` ";
$q4 .= " WHERE `StageID` IN (";
$q4 .= implode(',',$rowIDs);
$q4 .=  ");";
$query = mysqli_query($connect, $q4);

/*INSERT INTO COSTS TABLE FROM STAGING TABLE*/
$q5 .= "INSERT INTO `costs` (`workOrderNum`,`onsiteSurveyTestCost`,`onsiteSurveyTestRepairCost`,`offsiteSurveyTestCost`,`offsiteSurveyTestRepairCost`,`onsiteTestOnlyCost`,`onsiteTestRepairOnlyCost`,`onsiteRepairOnly`,`testPort2`,`repairCompleteMeterReplacement`,`repairCompleteMeterReplacementLaborCost`,`umeCost`,`umeLaborCost`,`rotatingLowSideDiskChamber`,`rotatingLowSideDiskChamberLaborCost`,`turbineChamberCost`,`turbineChamberLaborCost`,`automaticValveCost`,`automaticValveLaborCost`,`strainerCost`,`strainerLaborCost`,`lowRegisterCost`,`lowRegisterLaborCost`,`highRegisterCost`,`highRegisterLaborCost`,`miuCost`,`miuLaborCost`,`totalCost`) ";
$q5 .= "SELECT `workOrderNum`,`onsiteSurveyTestCost`,`onsiteSurveyTestRepairCost`,`offsiteSurveyTestCost`,`offsiteSurveyTestRepairCost`,`onsiteTestOnlyCost`,`onsiteTestRepairOnlyCost`,`onsiteRepairOnly`,`testPort2`,`repairCompleteMeterReplacement`,`repairCompleteMeterReplacementLaborCost`,`umeCost`,`umeLaborCost`,`rotatingLowSideDiskChamber`,`rotatingLowSideDiskChamberLaborCost`,`turbineChamberCost`,`turbineChamberLaborCost`,`automaticValveCost`,`automaticValveLaborCost`,`strainerCost`,`strainerLaborCost`,`lowRegisterCost`,`lowRegisterLaborCost`,`highRegisterCost`,`highRegisterLaborCost`,`miuCost`,`miuLaborCost`,`totalCost ` ";
$q5 .= "FROM `staging` ";
$q5 .= " WHERE `StageID` IN (";
$q5 .= implode(',',$rowIDs);
$q5 .=  ");";
$query = mysqli_query($connect, $q5);

/*INSERT INTO WORKORDERS TABLE FROM STAGING TABLE*/
$q6 .= "INSERT INTO `workorders` (`workOrderNum`,`lowSideMIUNum`,`highSideMIUNum`,`accountNum`,`custName`,`address`,`locID`,`utility`,`serialNumber`,`serviceName`) ";
$q6 .= "SELECT `workOrderNum`,`lowSideMIUNum`,`highSideMIUNum`,`accountNum`,`custName`,`address`,`locID`,`utility`,`serialNumber`,`serviceName  ";
$q6 .= "FROM `staging` ";
$q6 .= " WHERE `StageID` IN (";
$q6 .= implode(',',$rowIDs);
$q6 .=  ");";
$query = mysqli_query($connect, $q6);

?>