<?php
ob_start(); session_start(); 
include_once('../dbcon.php');
include_once('../session.php');
  
	
	$gid = $_GET['id'];
	$gname = $_GET['nm'];

   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('Group_Report.xlsx');// Creating a workbook
	
 	$qryreport = mysql_query("select airtime_sending_records.r_id, airtime_sending_records.airtime_sent, airtime_sending_records.recipient, airtime_sending_records.date_sent, airtime_sending_records.status, airtime_contacts.fname, airtime_contacts.contact, airtime_groups.name from airtime_sending_records inner join airtime_contacts on concat('+254',substr(airtime_sending_records.recipient,-9)) = concat('+254',substr(airtime_contacts.contact,-9)) inner join airtime_groups on airtime_contacts.group_id = airtime_groups.group_id where airtime_sending_records.customer_id = '$session_id' and airtime_contacts.group_id = '$gid' order by date_sent desc ");
	
	$sqlrows=mysql_num_rows($qryreport);
	$j=0;
	$showColoumn = false;
	while ($reportdisp=mysql_fetch_array($qryreport)) {
	$j=$j+1;
				
			$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'];
		    
			 //  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";
	}
	
?>