setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Fetch customer data
$stmt = $db->prepare("SELECT * FROM customers ORDER BY regDate DESC");
$stmt->execute();
$content = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Fetch EMI data
$stmt = $db->prepare("SELECT * FROM emi ORDER BY invoiceId ASC");
$stmt->execute();
$emiContent = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Fetch invoice data
$stmt2 = $db->prepare("SELECT * FROM invoice");
$stmt2->execute();
$invoiceContent = $stmt2->fetchAll(PDO::FETCH_ASSOC);
// Loop through each customer
$customerSerial = 1;
foreach ($content as $customer) {
// Find all invoices for the current customer
$matchingInvoices = array_filter($invoiceContent, function ($invoice) use ($customer) {
return $invoice['customerId'] === $customer['customerId'];
});
// If there are matching invoices, loop through them
if (!empty($matchingInvoices)) {
foreach ($matchingInvoices as $invoice) {
// Find corresponding EMI records for this invoice
$matchingEMIs = array_filter($emiContent, function ($emi) use ($invoice) {
return $emi['invoiceId'] === $invoice['invoiceId'];
});
// If EMI exists, use its data; otherwise, display empty fields
// $emi = reset($matchingEMIs) ?: [];
$matchingEmis = array_filter($emiContent, function ($emi) use ($invoice) {
return $emi['invoiceId'] === $invoice['invoiceId'];
});
if (!empty($matchingEmis)) {
$emi = reset($matchingEmis); // Get the first matching EMI record safely
} else {
$emi = [];
}
?>