File: /home/usas/swastikenterprises.usas.in/Admin/export_excel.php
<?php
include 'Database/config.php';
$selected_month = isset($_GET['month']) ? $_GET['month'] : '';
$monthPart = !empty($selected_month) ? '_' . $selected_month : '';
// SQL query (same as your filter logic)
$sql = "SELECT customers.*,
products.product_name,
product_name.products_name,
service_schedule.service_type,
service_schedule.service_month,
service_schedule.status,
service_schedule.service_date
FROM customers
LEFT JOIN products ON customers.cust_product = products.id
LEFT JOIN product_name ON customers.cust_model = product_name.id
LEFT JOIN service_schedule ON customers.cust_id = service_schedule.cust_id";
if (!empty($selected_month)) {
$sql .= " WHERE (
(service_schedule.service_type = 'paid' AND service_schedule.service_month = '{$selected_month}')
OR
(service_schedule.service_type IN ('free','amc')
AND MONTHNAME(service_schedule.service_date) = '{$selected_month}')
)";
}
$sql .= " ORDER BY customers.cust_id DESC";
$result = mysqli_query($conn, $sql);
// Set headers so browser downloads the file
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=customer_service_records' . $monthPart . '.csv');
// Open output stream
$output = fopen('php://output', 'w');
// Write column headers
fputcsv($output, ['Serial No', 'Customer Name', 'Phone', 'Address', 'Product', 'Service Type', 'Service Date/Month', 'Status']);
$serial = 1;
while ($row = mysqli_fetch_assoc($result)) {
$serviceInfo = '';
if ($row['service_type'] == 'paid') {
$serviceInfo = $row['service_month'];
} elseif (!empty($row['service_date'])) {
$serviceInfo = date('d-m-Y', strtotime($row['service_date']));
}
fputcsv($output, [
$serial++,
$row['cust_name'],
$row['cust_ph'],
$row['cust_address'],
$row['products_name'],
$row['service_type'],
$serviceInfo,
$row['status']
]);
}
fclose($output);
exit;
?>