<?php
require("../config.php");
$date = date('Y-m-d');
$eventCapital = "";
if ($_GET['eid'] == 26) {
	$eventCapital = '_DC_';	
} else if ($_GET['eid'] == 27) {
	$eventCapital = '_NYC_';	
} else if ($_GET['eid'] == 2) {
	$eventCapital = '_CA_';
}

header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=EntriesDetails_'.$eventCapital.$date.'.csv');
 //$query	= "SELECT * FROM personal_information ORDER BY id DESC";
 //$result = mysql_query($query);
 $row	= array('FirstName',
				'Middle NAme',
				'Last Name',
				'Age',
				'Address',
				'Address 2',
				'City',
				'state',
				'Postal Code',
				'Telephone Number',
				'Selected Category',
				'Email Address',
				'Donation Amount',
				'Is Ticket Generated',
				'Ticket Buyer'
				);


if ( $row ) {
	echocsv( $row );
}
 $query	= "SELECT * FROM user_information WHERE event_selected = '".$_GET['eid']."' ORDER BY id DESC";

 
$resultFields	= mysql_query($query);
while($res = mysql_fetch_assoc($resultFields)){


		$dataArray		= array();
		$dataArray[] 	=  $res['first_name'];
		$dataArray[] 	=  $res['middle_name'];
		$dataArray[]	=  $res['last_name'];
		$dataArray[]	=  $res['age'];
		$dataArray[]	=  $res['address1'];
		$dataArray[]	=  $res['address2'];
		$dataArray[] 	=  $res['city'];
		$dataArray[] 	=  $res['state'];
		$dataArray[] 	=  $res['zip_code'];
		$dataArray[] 	=  $res['telephone'];
		$dataArray[] 	=  $res['category_selected'];
		$dataArray[] 	=  $res['email_address'];
		$dataArray[] 	=  $res['donation_amount'];
		$dataArray[] 	=  $res['is_ticket_generated'];
		$parentId = $res['parent_id'];
		$testquery	= "SELECT email_address FROM user_information WHERE parent_id = '".$parentId."' ";
		$getParentEmail	= mysql_query($testquery);
		$resultSet = mysql_fetch_assoc($getParentEmail);
		$parentEmail = $resultSet['email_address'];
		$dataArray[] = $parentEmail;
		//$dateRegistered = $res['inserted_date'];
		//$dataArray[] = $dateRegistered;
	echocsv($dataArray);	
}




function echocsv( $fields ) {
	$separator = '';
	foreach ( $fields as $field ) {
		if ( preg_match( '/\\r|\\n|,|"/', $field ) ) {
			$field = '"' . str_replace( '"', '""', $field ) . '"';
		}
		echo $separator . $field;
		$separator = ',';
	}
	echo "\r\n";
}

?>
