<?php
ob_start(); session_start(); 
include_once('../dbcon.php');
include_once('../session.php');

  
	$frm = $_GET['fr'];
	$to = $_GET['to'];

   function HeaderingExcel($filename) {
      header("Content-type: application/vnd.ms-excel");
      header("Content-Disposition: attachment; filename=\"$filename\"" );
      header("Expires: 0");
      header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
      header("Pragma: public");
      }
	  
	  // HTTP headers
    HeaderingExcel('Dated_Summary.xlsx');// Creating a workbook

	
 	$qryreport = mysql_query("select airtime_sending_records.*, airtime_contacts.fname, airtime_contacts.contact, airtime_contacts.group_id, airtime_groups.name from airtime_sending_records inner join airtime_contacts on airtime_sending_records.recipient = airtime_contacts.contact inner join airtime_groups on airtime_contacts.group_id = airtime_groups.group_id where airtime_sending_records.customer_id = '$session_id' and ( airtime_sending_records.date_sent BETWEEN CAST('$frm' AS DATE) and CAST('$to' AS DATE) ) ");
	
	$sqlrows=mysql_num_rows($qryreport);
	$j=0;
	$showColoumn = false;
	while ($reportdisp=mysql_fetch_array($qryreport)) {
	$j=$j+1;
				
			$FirstName = $reportdisp['fname'];
			$LastName = $reportdisp['recipient'];
			$MiddleName = $reportdisp['name'];
			$UserName = $reportdisp['airtime_sent'];
			$Password = $reportdisp['date_sent'];
		    $stts  = $reportdisp['status'];
		    $rid  = $reportdisp['requestid'];
		    
		    //NEW CODE TO GET THE DATA AND COL HEADERS
		    $cols["Name"]=$reportdisp['fname'];
		    $cols["Contact"]=$reportdisp['recipient'];
		    $cols["Group"]=$reportdisp['name'];
		    $cols["Airtime Sent"]=$reportdisp['airtime_sent'];
		    $cols["Date Sent"]=$reportdisp['date_sent'];
		    $cols["Status"]=$reportdisp['status'];
		    $cols["RequestID"]=$reportdisp['requestid'];
		    //  WRITTING OUT TO BE PRINTED IN EXCEL
		    if(!$showColoumn) {		 
    		  echo implode("\t", array_keys($cols)) . "\n";
    		  $showColoumn = true;
    		}
    		echo implode("\t", array_values($cols)) . "\n";

	}

?>