HEX
Server: LiteSpeed
System: Linux s1321.sgp1.mysecurecloudhost.com 4.18.0-513.11.1.lve.el8.x86_64 #1 SMP Thu Jan 18 16:21:02 UTC 2024 x86_64
User: usas (2047)
PHP: 8.2.32
Disabled: NONE
Upload Files
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;
?>