Hibiscus Kontoanzeige

Moin,

da ich ja täglich meinen Reichtum mittels Hibiscus-Server abfrage, brauchte ich auch was was mir meinen Reichtum anzeigt. Dabei ist folgendes PHP-Script entstanden:

<?php

	// AGPL - x8Bit.de - 2013 - V1.0

	$db = mysql_connect('127.0.0.1', 'hibiscus', 'hibiscus') or die(mysql_error());
	mysql_select_db('hibiscus') or die(mysql_error());
	mysql_query("SET NAMES 'utf8'") or die(mysql_error());

	function db_select($query)
	{
		$daten = null;
		$result = mysql_query($query);
		if (!$result)
		{
			die(mysql_error());
			$daten[] = "";
		} else
		{
			while ($row = mysql_fetch_assoc($result)) $daten[] = $row;
		}
		return $daten;
	}
	
	function nameconverter($knr) {
		if (!strcmp($knr, "BLZ.KNR")) return "mein altes Konto";
		
		return "Konto ist neu";
	}

	$konto = db_select("SELECT * FROM hibiscus.konto ORDER BY blz, kontonummer");
	
?>
 
<html>

	<head>
		<title>Kontostände</title>
		<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=utf-8">
	</head>
	
	<script type="text/javascript">
		function anzeigen(das){
			if(document.getElementById(das).style.display=='none') {
				document.getElementById(das).style.display='block';
			} else {
				document.getElementById(das).style.display='none';
			}
		}
	</script>

	<style type="text/css">
		body { font-family: Ubuntu, Arial Narrow, Helvetica; }
		#footer { width: 100%; text-align: center; font-size: 10px; color: #888; }
		
		.kontoauszug { }
		.kontohead { width: 500px; background-color: #fff; border-top: 1px solid #888; }
		.kontohead:hover { background-color: #eee; }
		.kontoname { font-size: 16px; text-shadow: 1px 1px #aaa; }
		.kontonumbers { font-size: 12px; color: #888; }
		
		.saldo { font-size: 16px; width: 100px; text-align: right; text-shadow: 1px 1px #eee; padding-right: 5px; }
		
		.details { font-size: 12px; width: 75px; text-align: right; cursor: help; color: #00f; text-shadow: 1px 1px #aaa; }
		.betrag { font-size: 16px; width: 100px; text-align: right; padding-right: 5px; }
		.datum { font-size: 10px; color: #888; }
		.zweck { font-size: 12px; width: 400px; }
		.empfaenger { font-size: 12px; color: #888; }
		.umsatz { border-top: 1px dotted #ccc; }
		.umsatz:hover { background-color: #eee; }
		.colorred { color: #f00; }
		.colorgreen { color: #0c0; }
	</style>

	<body>
		<?php
			for($i = 0; $i < count($konto); $i++) {
				$query = "SELECT * FROM hibiscus.umsatz WHERE konto_id = ".$konto[$i]["id"]." ORDER BY datum DESC LIMIT 0,10";
				$umsatz = db_select($query);
				
				$color = "colorgreen";
				if ($konto[$i]["saldo"] < 0) $color = "colorred";
		?>
		
		<div class="kontoauszug">
			<table class="kontohead" onClick="anzeigen('<?php echo $konto[$i]["kontonummer"] ?>')">
				<tr>
					<td rowspan="2" class="saldo <?php echo $color; ?>"><?php echo number_format($konto[$i]["saldo"], 2, ',', '.')."€"; ?></td>
					<td colspan="2" class="kontoname"><?php echo nameconverter($konto[$i]["blz"].".".$konto[$i]["kontonummer"]); ?></td>
				</tr>
				<tr class="kontonumbers">
					<td><?php echo "BLZ ".$konto[$i]["blz"]." - KNR ".$konto[$i]["kontonummer"] ?></td>
					<td class="details">(Details)</td>
				</tr>
			</table>

			<div class="kontodetails" id="<?php echo $konto[$i]["kontonummer"] ?>" style="display: none">
				<?php
					for($j = 0; $j < count($umsatz); $j++) {

					$color = "colorgreen";
					if ($umsatz[$j]["betrag"] < 0) $color = "colorred";
				?>
				<table class="umsatz">
					<tr>
						<td class="betrag <?php echo $color; ?>" rowspan="4">
							<?php echo number_format($umsatz[$j]["betrag"], 2, ',', '.')."€" ?>
							<div class="datum">
								<?php echo $umsatz[$j]["datum"] ?>
							</div>
						</td>
						<td class="zweck"><?php echo $umsatz[$j]["zweck"] ?></td>
					</tr>
					<tr>
						<td class="zweck"><?php echo ($umsatz[$j]["zweck2"] == null ? " " : $umsatz[$j]["zweck2"]) ?></td>
					</tr>
					<tr>
						<td class="zweck"><?php echo ($umsatz[$j]["zweck3"] == null ? " " : $umsatz[$j]["zweck3"]) ?></td>
					</tr>
					<tr>
						<td class="empfaenger"><?php echo ($umsatz[$j]["empfaenger_name"] == null ? " " : $umsatz[$j]["empfaenger_name"]) ?></td>
					</tr>
				</table>
				<?php
					}
				?>
			</div>
					
		</div>
		
		<?php
			}
		?>
	</body>

</html>

ist sicherlich Verbesserungswürdig, aber das liegt im Detail am Auge dessen wer da rauf schaut. Mir reicht es zumindest um vom Tablet schnell mal die Kontostände zu checken (geraade was EC-Karte angeht).

hand, mogel

Ganz grauselig ist, dass du noch die alte, funktionale MySQL-Erweiterung nutzt. Du solltest zumindest MySQLi nutzen. Oder PDO.

Ansonsten vielleicht noch View von der Abfrage trennen. So template-mäßig. Also eine PHP-Datei, in der du nur den HTML-Code stehen hast und nur Variablen ausgibst. Diese bindest du dann in deiner Abfragedatei ein und schon hast du eine saubere Trennung von Logik und Anzeige.

prinzipell hast Du recht - gnadenlos. Prepared-Statements sind definitiv von Vorteil - wobei hier eh alles aus der DB kommt. Ein bischen MVC sollte man auch einhalten.

ABER

Das Script ist nur für den Hausgebrauch und würde ich so einem Kunden nie anbieten (zumal ich generell PHP/HTML nicht mache). Wenn ich mit meiner Frau und den Kinder losziehe, kommt immer die Frage “Wieviel Geld hast Du?”. Dann anmelden auf der Webseite von $(BANK) - was gefühlt Stunden dauert - die Kinder stressen weil denen Langweilig wird. Meine Frau ist dann natürlich bei einer anderen Bank bzw. andere Account-Daten - das dauert dann nochmal so lange. Und so tippe ich kurz aufs Tablet und sehe gleich alles.

Hatte ich auch so verstanden. Aber auch für den Hausgebrauch nutze ich MySQLi :wink: Die alte MySQL-Erweiterung ist IIRC sogar deprecated.
Und da du das Script hier ja veröffentlicht hast, dachte ich, dass du auch gerne Feedback hättest.

In diesem Fall hätte ich wahrscheinlich auch keine prepared Statements verwendet (ok, für die zweite Abfrage wahrscheinlich schon wieder, weil man da mit dem Escapen auf keinen Fall Probleme bekommt - auch wenn es bei Kontodaten wohl nichts zu escapen gibt).