How to Export CSV File

require_once ‘includes/dbconnect.php’;

// Fetch Record from Database

$output = “”;
$sql=mysql_query(“SELECT a.post_contact_person,a.post_email,a.post_title,a.Recurring,a.event_start,a.event_end,a.event_start_time,a.event_end_time,a.post_vanue_name,
b.post_content,
c.display_name
FROM    event_detail a
INNER JOIN wp_posts b
ON a.post_id = b.ID
INNER JOIN wp_users c
ON b.post_author = c.ID
WHERE   a.post_status = ‘trash’
“);

$columns_total = mysql_num_fields($sql);

// Get The Field Name

for ($i = 0; $i < $columns_total; $i++) {
$heading = mysql_field_name($sql, $i);
$output .= ‘”‘.$heading.'”,’;
}
$output .=”\n”;

// Get Records from the table
while ($row = mysql_fetch_array($sql)) {
for ($i = 0; $i < $columns_total; $i++) {
$output .='”‘.$row[“$i”].'”,’;
}
$output .=”\n”;
}

// Download the file

$filename = “Events.csv”;
header(‘Content-type: application/csv’);
header(‘Content-Disposition: attachment; filename=’.$filename);

echo $output;
exit;

Advertisements