<?php
$conn = mysqli_connect('localhost', 'sarmotorismo_formenter', 'D6l!DHytJFZ2', 'sarmotorismo_formenter');

$queryslct = "SELECT * FROM events";
$resultquery = mysqli_query($conn, $queryslct);

$queryslctdriv = "SELECT * FROM drivers";
$resultqueryslctdriv = mysqli_query($conn, $queryslctdriv);

$queryslccont = "SELECT * FROM contacts";
$resultqueryslccont = mysqli_query($conn, $queryslccont);

$queryslvehicle = "SELECT * FROM vehicle";
$resultqueryslvehicle = mysqli_query($conn, $queryslvehicle);

$html = '<table>
		<tr>
			<td>user_id</td>
			<td>FIA_t1</td>
			<td>FIA_class</td>
			<td>FIA_t2</td>
			<td>FIA_t3</td>
			<td>FIA class</td>
			<td>FIA_t4</td>
			<td>National T1</td>
			<td>classe_t1_nac</td>
			<td>National T1</td>
			<td>National T2</td>
			<td>National T3</td>
			<td>class_t4_nac</td>
			<td>National T4</td>
			<td>National T5</td>
			<td>Comp Name</td>
			<td>Comp Licence No.</td>
			<td>Team Manager/Responsável Equipa</td>
			<td>Mobile</td>
			<td>Email</td>
			<td>First Driver Surname</td>
		<td>First name</td>
		<td>Nationality (as license)/Nacionalidade</td>
		<td>Full Address/Morada completa</td>
		<td>Passport or I.D. No./Passaporte ou cartão de cidadão</td>
		<td>Mobile/Telemóvel</td>
		<td>Email</td>
		<td>Competition License No./Nº Licença</td>
		<td>Issuing ASN/Entidade emissora</td>
		<td>Driving License No./Carta de condução</td>

		<td>Country of issue/País emissor</td>
		<td>Second driver surname</td>
		<td>sec_driv_fname</td>
		<td>sec_driv_nationality_co</td>
		<td>sec_driv_adress</td>
		<td>sec_driv_passport_co</td>
		<td>sec_driv_email_co</td>
		<td>sec_driv_competitionLicense_co</td>
		<td>sec_driv_issuingASN_co</td>
		<td>sec_driv_drivingLicense_co</td>
		<td>sec_driv_countryOfIssue_co</td>


		<td>emergency_surname</td>
		<td>emergency_fname</td>
		<td>emergency_mobile</td>
		<td>emergency_email</td>



		<td>comp_vehicle_make</td>
		<td>comp_vehicle_model</td>
		<td>vehicle_registrationNo</td>
		<td>vehicle_Cc</td>
		<td>vehicle_chassisNo</td>
		<td>vehicle_technicalPassportNo</td>
		<td>year Of Manufacture</td>
		<td>vehicle_group</td>
		<td>vehicle_homologationNo</td>
		<td>vehicle_countryOfRegistration</td>

		</tr>';

		//fetch event
	while($eventdata = mysqli_fetch_assoc($resultquery)){
		$user_id = $eventdata['id'];
		$FIA_t1 = $eventdata['FIA_t1'];
		$FIA_class = $eventdata['FIA_class'];
		$FIA_t2 = $eventdata['FIA_t2'];
		$FIA_t3 = $eventdata['FIA_t3'];
		$sec_class = $eventdata['sec_class'];
		$FIA_t4 = $eventdata['FIA_t4'];
		$grupo_t1_nac = $eventdata['grupo_t1_nac'];
		$classe_t1_nac = $eventdata['classe_t1_nac'];

		$grupo_t2_nac = $eventdata['grupo_t2_nac'];
		$grupo_t3_nac = $eventdata['grupo_t3_nac'];
		$grupo_t4_nac = $eventdata['grupo_t4_nac'];

		$class_t4_nac = $eventdata['class_t4_nac'];
		$grupo_t8_nac = $eventdata['grupo_t8_nac'];
		$grupo_t9_nac = $eventdata['grupo_t9_nac'];
		$comp_name = $eventdata['comp_name'];
		$comp_licen = $eventdata['comp_licen'];
		$team_manag = $eventdata['team_manag'];

		$comp_mobile = $eventdata['comp_mobile'];
		$comp_mail = $eventdata['comp_mail'];

		//fetch drivers 
		while($driverdata = mysqli_fetch_assoc($resultqueryslctdriv)){
			$driver_surname = $driverdata['driver_surname'];
			$driver_fname = $driverdata['driver_fname'];
			$driv_nationality = $driverdata['driv_nationality'];
			$driv_adress = $driverdata['driv_adress'];
			$driv_passport = $driverdata['driv_passport'];
			$driv_mobile = $driverdata['driv_mobile'];
			$driv_mail = $driverdata['driv_mail'];
			$driv_compit_lice = $driverdata['driv_compit_lice'];
			$driv_issu_asn = $driverdata['driv_issu_asn'];

			$driving_lice = $driverdata['driving_lice'];
			$count_of_issue = $driverdata['count_of_issue'];
			$sec_driv_surname = $driverdata['sec_driv_surname'];
			$sec_driv_fname = $driverdata['sec_driv_fname'];
			$sec_driv_nationality_co = $driverdata['sec_driv_nationality_co'];
			$sec_driv_adress = $driverdata['sec_driv_adress'];
			$sec_driv_passport_co = $driverdata['sec_driv_passport_co'];
			$sec_driv_email_co = $driverdata['sec_driv_email_co'];
			$sec_driv_competitionLicense_co = $driverdata['sec_driv_competitionLicense_co'];
			$sec_driv_issuingASN_co = $driverdata['sec_driv_issuingASN_co'];
			$sec_driv_drivingLicense_co = $driverdata['sec_driv_drivingLicense_co'];
			$sec_driv_countryOfIssue_co = $driverdata['sec_driv_countryOfIssue_co'];

		}

		//fetch contact
		while($contactdata = mysqli_fetch_assoc($resultqueryslccont)){
			$emergency_surname = $contactdata['emergency_surname'];
			$emergency_fname = $contactdata['emergency_fname'];
			$emergency_mobile = $contactdata['emergency_mobile'];
			$emergency_mail = $contactdata['emergency_mail'];
		}

		//fetch vicheles

		while($vichdata = mysqli_fetch_assoc($resultqueryslvehicle)){
			$comp_vehicle_make = $vichdata['comp_vehicle_make'];
			$comp_vehicle_model = $vichdata['comp_vehicle_model'];
			$comp_vehicle_registrationNo = $vichdata['comp_vehicle_registrationNo'];
			$comp_vehicle_cc = $vichdata['comp_vehicle_cc'];
			$comp_vehicle_chassisNo = $vichdata['comp_vehicle_chassisNo'];
			$comp_vehicle_technicalPassportNo = $vichdata['comp_vehicle_technicalPassportNo'];
			$comp_vehicle_yearOfManufacture = $vichdata['comp_vehicle_yearOfManufacture'];
			$comp_vehicle_group = $vichdata['comp_vehicle_group'];
			$comp_vehicle_homologationNo = $vichdata['comp_vehicle_homologationNo'];
			$comp_vehicle_countryOfRegistration = $vichdata['comp_vehicle_countryOfRegistration'];
		}
		// ...
	$html .= '<table>
	<tr>
		<td>'.$user_id.'</td>
		<td>'.$FIA_t1.'</td>
		<td>'.$FIA_class.'</td>
		<td>'.$FIA_t2.'</td>
		<td>'.$FIA_t3.'</td>
		<td>'.$sec_class.'</td>
		<td>'.$FIA_t4.'</td>
		<td>'.$grupo_t1_nac.'</td>
		<td>'.$classe_t1_nac.'</td>
		<td>'.$grupo_t2_nac.'</td>
		<td>'.$grupo_t3_nac.'</td>
		<td>'.$grupo_t4_nac.'</td>
		<td>'.$class_t4_nac.'</td>
		<td>'.$grupo_t8_nac.'</td>
		<td>'.$grupo_t9_nac.'</td>
		<td>'.$comp_name.'</td>
		<td>'.$comp_licen.'</td>
		<td>'.$team_manag.'</td>
		<td>'.$comp_mobile.'</td>
		<td>'.$comp_mail.'</td>
		<td>'.$driver_surname.'</td>
		<td>'.$driver_fname.'</td>
		<td>'.$driv_nationality.'</td>
		<td>'.$driv_adress.'</td>
		<td>'.$driv_passport.'</td>
		<td>'.$driv_mobile.'</td>
		<td>'.$driv_mail.'</td>
		<td>'.$driv_compit_lice.'</td>
		<td>'.$driv_issu_asn.'</td>
		<td>'.$driving_lice.'</td>

		<td>'.$count_of_issue.'</td>
		<td>'.$sec_driv_surname.'</td>
		<td>'.$sec_driv_fname.'</td>
		<td>'.$sec_driv_nationality_co.'</td>
		<td>'.$sec_driv_adress.'</td>
		<td>'.$sec_driv_passport_co.'</td>
		<td>'.$sec_driv_email_co.'</td>
		<td>'.$sec_driv_competitionLicense_co.'</td>
		<td>'.$sec_driv_issuingASN_co.'</td>
		<td>'.$sec_driv_drivingLicense_co.'</td>
		<td>'.$sec_driv_countryOfIssue_co.'</td>
		<td>'.$emergency_surname.'</td>
		<td>'.$emergency_fname.'</td>
		<td>'.$emergency_mobile.'</td>
		<td>'.$emergency_mail.'</td>


		<td>'.$comp_vehicle_make.'</td>
		<td>'.$comp_vehicle_model.'</td>
		<td>'.$comp_vehicle_registrationNo.'</td>
		<td>'.$comp_vehicle_cc.'</td>
		<td>'.$comp_vehicle_chassisNo.'</td>
		<td>'.$comp_vehicle_technicalPassportNo.'</td>
		<td>'.$comp_vehicle_yearOfManufacture.'</td>
		<td>'.$comp_vehicle_group.'</td>
		<td>'.$comp_vehicle_homologationNo.'</td>
		<td>'.$comp_vehicle_countryOfRegistration.'</td>
	</tr>';
// ...

	}

	$html .= "</table>";


	header('Content-type:application/xls');
	header('Content-Disposition:attachment;filename=EventData.xls');
echo $html;

?>