<?php
	$access_restricted = false;
	include_once($_SERVER['DOCUMENT_ROOT'].'/daten/php/startup.php');
	
	$pagetitle = 'Adressen';
	$breadcrumb = 'Adressen';	
	include_once($_SERVER['DOCUMENT_ROOT'].'/daten/php/pageheader.php');  
?>

<?php
	mysql_connect('localhost', 'dbu1022769', 'fuB0gar7') or die ('Database error: Unable to connect to database.');
	mysql_select_db('db1022769-php') or die ('Database error: Unable to select database.');
	
	if (isset($_POST['Vorname'])) {
		$insert = mysql_query('INSERT INTO adressen (Vorname,Nachname,Anschrift,PLZ,Ort,Telefon,Tel_alt,Tel_Mobil,eMail,eMail_alt,Geburtstag,Gruppe,Bemerkung) VALUES ("'.$_POST['Vorname'].'","'.$_POST['Nachname'].'","'.$_POST['Anschrift'].'","'.$_POST['PLZ'].'","'.$_POST['Ort'].'","'.$_POST['Telefon'].'","'.$_POST['Tel_alt'].'","'.$_POST['Tel_Mobil'].'","'.$_POST['eMail'].'","'.$_POST['eMail_alt'].'","'.$_POST['Geburtstag'].'","'.$_POST['Gruppe'].'","'.$_POST['Bemerkung'].'")');
		if (!$insert) {
			echo '<p>Insert failed: '.mysql_error().'</p>';
		}
	}
	
	if (isset($_GET['delete'])) {
		$delete = mysql_query('DELETE FROM adressen WHERE id='.$_GET['delete']);
		if (!$delete) {
			echo '<p>Could not delete '.$_GET['delete'].': '.mysql_error().'</p>';
		}
	}
	
	$orderby = 'id';
	if (isset($_GET['orderby'])) {
		$orderby = urldecode($_GET['orderby']);
	}
	
	$result = mysql_query('SELECT * FROM adressen ORDER BY "'.$orderby.'" ASC');
	if (!$result) {
		die('Database error: Unable to get table (order by: '.$orderby.')');
	}
	
	function getSortingLink($row, $title) {
		return '<a href="'.$_SERVER['PHP_SELF'].'?orderby='.urlencode($row).'">'.$title.'</a>';
	}
?>

<div style="font-family: Arial, sans-serif; margin-left: 1em;">
	<table border="1" style="font-size: x-small; border-collapse: collapse;">
		<tr>
			<th><?php echo getSortingLink('id', 'ID'); ?></th>
			<th><?php echo getSortingLink('Vorname', 'Vorname'); ?></th>
			<th><?php echo getSortingLink('Nachname', 'Nachname'); ?></th>
			<th><?php echo getSortingLink('Anschrift', 'Anschrift'); ?></th>
			<th><?php echo getSortingLink('PLZ', 'PLZ'); ?></th>
			<th><?php echo getSortingLink('Ort', 'Ort'); ?></th>
			<th><?php echo getSortingLink('Telefon', 'Telefon'); ?></th>
			<th><?php echo getSortingLink('Tel_alt', 'Tel. alt.'); ?></th>
			<th><?php echo getSortingLink('Tel_Mobil', 'Mobil'); ?></th>
			<th><?php echo getSortingLink('eMail', 'E-Mail'); ?></th>
			<th><?php echo getSortingLink('eMail_alt', 'E-Mail alt.'); ?></th>
			<th><?php echo getSortingLink('Geburtstag', 'Geburtstag'); ?></th>
			<th><?php echo getSortingLink('Gruppe', 'Gruppe'); ?></th>
			<th><?php echo getSortingLink('Bemerkung', 'Bemerkung'); ?></th>
			<th></th>
			<th></th>
		</tr>
<?php
	while ($row = mysql_fetch_array($result)) {
		echo "\t\t<tr>";
		echo "\t\t\t<td>\t\t\t\t".$row['id']."\t\t\t</td>";
		echo "\t\t\t<td>\t\t\t\t".$row['Vorname']."\t\t\t</td>";
		echo "\t\t\t<td>\t\t\t\t".$row['Nachname']."\t\t\t</td>";
		echo "\t\t\t<td>\t\t\t\t".$row['Anschrift']."\t\t\t</td>";
		echo "\t\t\t<td>\t\t\t\t".$row['PLZ']."\t\t\t</td>";
		echo "\t\t\t<td>\t\t\t\t".$row['Ort']."\t\t\t</td>";
		echo "\t\t\t<td>\t\t\t\t".$row['Telefon']."\t\t\t</td>";
		echo "\t\t\t<td>\t\t\t\t".$row['Tel_alt']."\t\t\t</td>";
		echo "\t\t\t<td>\t\t\t\t".$row['Tel_Mobil']."\t\t\t</td>";
		echo "\t\t\t<td>\t\t\t\t".$row['eMail']."\t\t\t</td>";
		echo "\t\t\t<td>\t\t\t\t".$row['eMail_alt']."\t\t\t</td>";
		echo "\t\t\t<td>\t\t\t\t".$row['Geburtstag']."\t\t\t</td>";
		echo "\t\t\t<td>\t\t\t\t".$row['Gruppe']."\t\t\t</td>";
		echo "\t\t\t<td>\t\t\t\t".$row['Bemerkung']."\t\t\t</td>";
		echo "\t\t\t<td>\t\t\t\t<a href=\"".$_SERVER['PHP_SELF']."?edit=".$row['id']."\">edit</a>\t\t\t</td>";
		echo "\t\t\t<td>\t\t\t\t<a href=\"".$_SERVER['PHP_SELF']."?delete=".$row['id']."\">del</a>\t\t\t</td>";
		echo "\t\t</tr>";
	}
?>
	</table>
	
	<div style="margin-top: 1em;">
		<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
			<table border="1" style="font-size: x-small; border-collapse: collapse;">
				<tr>
					<td>Vorname</td>
					<td>Nachname</td>
					<td>Anschrift</td>
					<td>PLZ</td>
					<td>Ort</td>
				</tr>
				<tr>
					<td><input type="text" name="Vorname" size="12" style="font-size: x-small;" /></td>
					<td><input type="text" name="Nachname" size="12" style="font-size: x-small;" /></td>
					<td><input type="text" name="Anschrift" size="12" style="font-size: x-small;" /></td>
					<td><input type="text" name="PLZ" size="12" style="font-size: x-small;" /></td>
					<td><input type="text" name="Ort" size="12" style="font-size: x-small;" /></td>
				</tr>
			</table>
			<table border="1" style="margin-top: 1ex; font-size: x-small; border-collapse: collapse;">
				<tr>
					<td>Tel. privat</td>
					<td>Tel. geschäftlich</td>
					<td>Tel. mobil</td>
					<td>E-Mail privat</td>
					<td>E-Mail geschäftlich</td>
				</tr>
				<tr>
					<td><input type="text" name="Telefon" size="12" style="font-size: x-small;" /></td>
					<td><input type="text" name="Tel_alt" size="12" style="font-size: x-small;" /></td>
					<td><input type="text" name="Tel_Mobil" size="12" style="font-size: x-small;" /></td>
					<td><input type="text" name="eMail" size="12" style="font-size: x-small;" /></td>
					<td><input type="text" name="eMail_alt" size="12" style="font-size: x-small;" /></td>
				</tr>
			</table>
			<table border="1" style="margin-top: 1ex; font-size: x-small; border-collapse: collapse;">
				<tr>
					<td>Geburtstag</td>
					<td>Gruppe</td>
					<td>Bemerkung</td>
				</tr>
				<tr>
					<td><input type="text" name="Geburtstag" size="12" style="font-size: x-small;" /></td>
					<td>
						<select name="Gruppe" style="font-size: x-small;">
							<option>Familie</option>
							<option>Beethoven</option>
							<option>Uni</option>
							<option>Bekannte</option>
							<option>Ärzte</option>
							<option>Bettinas Familie</option>
							<option>Bettinas Bekannte</option>
						</select>
					</td>
					<td><input type="text" name="Bemerkung" size="44" style="font-size: x-small;" /></td>
				</tr>
			</table>
			<input type="submit" value="Hinzufügen" style="font-size: x-small" />
		</form>
	</div>
</div>

<?php
	include_once($_SERVER['DOCUMENT_ROOT'].'/daten/php/pagefooter.php');
?>
