比较数据库中的上传数据和原始CSV数据

时间:2017-01-16 16:01:12

标签: php for-loop while-loop

以上是我上传原始文件后尝试使用上传的文件验证原始文件以查看它们是否匹配的代码:

while ($db_fetch_row = sqlsrv_fetch_array($database_query)){        
    $db_eid = $db_fetch_row['eid'];
    $db_team_lead = $db_fetch_row['team_lead'];
    $db_role = $db_fetch_row['role'];
    $db_productivity = $db_fetch_row['productivity'];
    $db_quality = $db_fetch_row['quality'];
    $db_assessment = $db_fetch_row['assessment'];
    $db_staffed_hours = $db_fetch_row['staffed_hours'];
    $db_kpi_productivity = $db_fetch_row['kpi_productivity'];
    $db_kpi_quality = $db_fetch_row['kpi_quality'];
    $db_kpi_assessment = $db_fetch_row['kpi_assessment'];
    $db_kpi_staffed_hours = $db_fetch_row['kpi_staffed_hours'];     

    for($row = 2; $row <= $lastRow; $row++) {
        $eid = $worksheet->getCell('A'.$row)->getValue();
        $team_lead = $worksheet->getCell('C'.$row)->getValue();
        $role = $worksheet->getCell('B'.$row)->getValue();                                  
        $productivity = $worksheet->getCell('D'.$row)->getValue();
        $productivity1 = chop($productivity,"%");

        $quality = $worksheet->getCell('E'.$row)->getValue();
        $quality1 = chop($quality,"%");         

        $assessment = $worksheet->getCell('F'.$row)->getValue();
        $assessment1 = chop($assessment,"%");

        $staffed_hours = $worksheet->getCell('G'.$row)->getValue();
        $staffed_hours1 = chop($staffed_hours,"%");

        $kpi_productivity =  $worksheet->getCell('H'.$row)->getValue();
        $kpi_quality =  $worksheet->getCell('I'.$row)->getValue();
        $kpi_assessment =  $worksheet->getCell('J'.$row)->getValue();
        $kpi_staffed_hours = $worksheet->getCell('K'.$row)->getValue(); 

        if($db_eid == $eid) {
            echo "Raw and Uploaded file Matched";
        } else {
            echo "Did not match";
        }
    }
}

输出始终无法匹配,如下所示:

Output

1 个答案:

答案 0 :(得分:0)

当然它输出了。让我们思考一下:

对于数据库中的每一行,您都要检查CSV中的每一行。据推测,CSV中只有一行与DB中的行具有相同的ID,这意味着如果CSV中有100条记录,它将输出“不匹配”99次(假设有1条记录)匹配的CSV。)

一种方法是将其分成一个函数并尝试查找匹配的行,如下所示:

// Loop over all DB records
while ($db_fetch_row = sqlsrv_fetch_array($database_query)) {
    // call our new function to attempt to find a match
    $match = find_matching_row( $db_fetch_row, $worksheet );
    // If there's no match, output "didn't find a match"
    if ( ! $match ) {
         echo '<br>DID NOT FIND A MATCH.';
    // If there IS a match, $match represents the row number to use....
    } else {
         var_dump( $match );
         // Do your work on the match data...
         $team_lead = $worksheet->getCell('C'.$match)->getValue();
         // ...etc  
    }
}

/**
 * Attempt to find a row from the CSV with the same ID as the DB
 * record passed in.
 *
 * @param array $db_fetch_row
 * @param mixed $worksheet
 */
function find_matching_row( $db_fetch_row, $worksheet ) {
    $db_eid = $db_fetch_row['eid'];
    // Youll need to calculate $lastRow for this to work right..
    $lastRow = .... ;

    // Loop through the CSV records
    for($row = 2; $row <= $lastRow; $row++) {
            // If the IDs match, return the row number
            if($db_eid == $worksheet->getCell('A'.$row)->getValue() ){
                return $row;
            }
    }

    // If no match, return FALSE
    return FALSE;
}
相关问题