Viewing file: export_excel.php (2.13 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
// Connect to the database
$conn = new mysqli('localhost','root','','loan');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Get the date range from query string
$from_date = isset($_GET['from_date']) ? $_GET['from_date'] : '';
$to_date = isset($_GET['to_date']) ? $_GET['to_date'] : '';
// Build the SQL query with date filter if applicable
$sql = "SELECT * FROM doctor_loan";
if ($from_date && $to_date) {
$sql .= " WHERE created_at BETWEEN '$from_date' AND '$to_date'";
} elseif ($from_date) {
$sql .= " WHERE created_at >= '$from_date'";
} elseif ($to_date) {
$sql .= " WHERE created_at <= '$to_date'";
}
$result = $conn->query($sql);
// Initialize an empty array to hold the records
$developer_records = [];
while ($row = $result->fetch_assoc()) {
$developer_records[] = $row;
}
$conn->close();
// Custom column names (exclude the "id" column)
$custom_column_names = [
"Full Name",
"Mobile",
"Email",
"Qualification",
"Work Experience",
"Residential Property",
"PAN Number",
"Pincode",
"Date of Birth",
"Medical Registration Year",
"Employment Type",
"Is Register Doctor",
"Loan Amount",
"City",
"Date",
// Add more custom column names as needed
];
// Export data if the form is submitted
$filename = "doctors_loan_" . date('Ymd') . ".xls";
// Set headers to prompt download
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"$filename\"");
$show_column = false;
// Check if there are records to export
if (!empty($developer_records)) {
foreach ($developer_records as $index => $record) {
// Print custom column names as the first row (if not already printed)
if (!$show_column) {
echo implode("\t", $custom_column_names) . "\n";
$show_column = true;
}
// Remove the "id" column from the record (if it exists)
unset($record['id']);
// Print row values
echo implode("\t", array_values($record)) . "\n";
}
}
exit;
?>
|