<?php
include_once('details1.php');

  // output headers so that the file is downloaded rather than displayed

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=Airtime_Contacts.csv');

// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

// output the column headings
fputcsv($output, array('Name', 'Contact', 'Group',));

// fetch the data

$rows = mysql_query("SELECT airtime_contacts.fname, airtime_contacts.contact, airtime_groups.name FROM airtime_contacts INNER JOIN airtime_groups ON airtime_contacts.group_id = airtime_groups.group_id WHERE airtime_contacts.customer_id = '$session_id' ORDER BY airtime_contacts.contact_id ASC");

// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);

?>