2017-01-16 58 views
1

下面是代碼數據我上傳的原始文件後,然後試圖驗證的原始文件,上傳文件,看它們是否匹配:比較上傳數據庫,並把原始CSV數據

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"; 
     } 
    } 
} 

輸出始終沒「T匹配,你可以看到如下:

Output

+0

那麼它不是_always_「不匹配」,看起來像你的第一行匹配了。 – roberto06

回答

0

當然它輸出。讓我們仔細考慮:

對於數據庫中的每一行,您都檢查CSV中的每一行。據推測,CSV中只有一行與數據庫中的行具有相同的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; 
}