我正在尝试从mysql表中提取文档列表,并将其与相关pdf文件所在的文件夹中的文件数组进行比较。 我需要对它们进行比较,并在列表中突出显示不匹配的记录?
我试图比较和循环记录,但是我不知道该怎么做!
// Empty array to hold invoices id's (serial)
$files = [];
// Select records form invoices table and push invoice id into empty array
$table = 'app_com_customersdocs';
$sql = "SELECT * FROM $table WHERE cust_doc_type = 'FC'";
$data = $conn->query($sql);
while ($result = $data->fetch()) {
array_push($files, $result['cust_doc_id']);
}
// Get all pdf files from folder $dir, then extract the .pdf extension
// this get an array with invoices serial number equivalent to invoices in myslq
$docs = array_diff(scandir($dir), ['.', '..']);
$docs = preg_replace('/\\.[^.\\s]{3,4}$/', '', $docs);
// Compare the two arrays and get the difference between
// this create a new array $documents containing the missing entry in table or missing file in folder
$documents = $docs ? array_diff($docs, $files) : null;
//
$stmt = $conn->query($sql);
while($dc_res = $stmt->fetch()) {
if(in_array($dc_res['cust_doc_id'], $documents)) {
echo '<span style="color: red">'.$dc_res['cust_doc_id'].'</span><br>';
}else {
echo $dc_res['cust_doc_id'].'<br>';
}
}
我希望输出如下列表: