Created
October 9, 2011 09:36
-
-
Save gbirke/1273501 to your computer and use it in GitHub Desktop.
PHP-Kurs Codebeispiele
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$hauptstaedte = array("Schweiz" => "Bern", | |
"Frankreich" => "Paris", | |
"Deutschland" => "Berlin", | |
"Spanien" => "Madrid"); | |
// Das geht nicht!! | |
//echo "Die Stadt heißt $hauptstaedte["Schweiz"]"; | |
// Das geht. | |
echo "Die Stadt heißt " . $hauptstaedte["Schweiz"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>einfaches Formular</title> | |
</head> | |
<body> | |
<h3>Formular mit Checkboxen, Radiobuttons</h3> | |
<form action="formular_auswertung-2.php" method="POST"> | |
<p>Interessen: | |
<input type="checkbox" name="interesse[]" value="Kultur">Kultur | |
<input type="checkbox" name="interesse[]" value="Musik">Musik | |
<input type="checkbox" name="interesse[]" value="Natur">Natur | |
<input type="checkbox" name="Sport" value="Sport">Sport </p> | |
<p>Zahlungsart: | |
<input type="radio" name="zahlung" value="bar">bar | |
<input type="radio" name="zahlung" value="Scheck">Scheck | |
<input type="radio" name="zahlung" value="Überweisung">Überweisung | |
</p> | |
<p><input type="submit" name="absenden" value="Abschicken"> | |
<input type="reset" value="Zurücksetzen"></p> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Formularauswertung</title> | |
</head> | |
<body> | |
<?php | |
echo "<pre>"; | |
print_r($_POST); | |
echo "</pre>"; | |
if (!empty($_POST["interesse"])) | |
{ | |
// Angekreuzte Felder als Liste ausgeben | |
echo "Folgende Interessen wurden angegeben:<br>"; | |
echo implode(", ", $_POST["interesse"]) ."<br>"; | |
} | |
// Wie werte ich eine Checkbox aus (ohne Array)? | |
if (empty($_POST["Sport"])) { | |
echo "Unsportlich, wa?"; | |
} | |
else { | |
echo "Tüchtig!"; | |
} | |
?> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Globale Variablen in Funktionen | |
// In dieser Funktion ist $summe global | |
function rechne1() { | |
global $summe; | |
echo "Summe in rechne1 ist $summe<br>"; | |
$summe = 5; | |
echo "Summe nach rechne1 ist $summe<br>"; | |
} | |
// In dieser Funktion ist $summe lokal | |
// heißt also nur "zufällig" so wie die globale Variable | |
// Änderungen an der lokalen Variabel haben keine Auswirkung | |
// auf die globale Variable | |
function rechne2() { | |
echo "Summe in rechne2 ist $summe<br>"; | |
$summe = 6; | |
echo "Summe nach rechne2 ist $summe<br>"; | |
} | |
// $summe ist global, weil außerhalb von funktionen, | |
// direkt im PHP-code | |
$summe = 1; | |
echo "$summe<br>"; | |
rechne1(); | |
echo "$summe<br>"; | |
rechne2(); | |
echo "$summe<br>"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Formularauswertung</title> | |
</head> | |
<body> | |
<?php | |
// Eingaben aus Formular als Mail verschicken: | |
$ausgabe = "Folgende Daten wurden übermittelt:\n"; | |
$ausgabe .= "Vorname: " .$_POST["vorname"] ."\n"; | |
$ausgabe .= "Nachname: " .$_POST["nachname"] ."\n"; | |
$ausgabe .= "Wohnort: " .$_POST["ort"]; | |
mail("mich@meinserver.de", "Anfrage von der Webseite", $ausgabe); | |
// Ausgabe für den Benutzer, der das Formular abgeschickt hat | |
echo "Ihre Anfrage wurde als Mail versandt."; | |
?> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Mehrdimensionale Felder</title> | |
</head> | |
<body> | |
<h3>mehrdimensionale Felder</h3> | |
<?php | |
//mehrdimensionales assoziatives Feld mit zwei Indizes (zweidim.) | |
$land = array( | |
"Spanien" => array("Hauptstadt" => "Madrid", | |
"Sprache" => "Spanisch", | |
"Waehrung" => "Euro", | |
"Flaeche" => "504645 qkm"), | |
"England" => array("Hauptstadt" => "London", | |
"Sprache" => "Englisch", | |
"Waehrung" => "Pfund Sterling", | |
"Flaeche" => "130395 qkm"), | |
"Portugal" => array("Hauptstadt" => "Lissabon", | |
"Sprache" => "Portugiesisch", | |
"Waehrung" => "Euro", | |
"Flaeche" => "92345 qkm") | |
); | |
//(manuelle) Erweiterung des Feldes durch einen neuen Eintrag | |
$land["Ungarn"]["Hauptstadt"] = "Budapest"; | |
$land["Ungarn"]["Sprache"] = "Ungarisch"; | |
$land["Ungarn"]["Waehrung"] = "Forint"; | |
$land["Ungarn"]["Flaeche"] = "93036 qkm"; | |
echo "<table border='1'>"; | |
// Tabellenueberschriften aus der 1. "zeile" erzeugen | |
// Zeile ist beliebig, ich koennte auch Portugal oder | |
// England nehmen | |
echo "<tr><th>Land</th>"; | |
foreach($land["Spanien"] as $wertname => $dummy) { | |
echo "<th>$wertname</th>"; | |
} | |
echo "</tr>\n"; | |
// Tabelle ausgeben | |
foreach($land as $landname => $daten) { | |
// Index des "aeußeren" Arrays as Bezeichnung | |
echo "<tr><th>$landname</th>"; | |
// Werte des inneren Arrays | |
foreach($daten as $wertname => $wert) { | |
// Fläche rechtsbuendig | |
if($wertname == 'Flaeche') { | |
echo "<td style='text-align:right;'>$wert</td>\n"; | |
} | |
// Alle anderen Felder normal | |
else { | |
echo "<td>$wert</td>\n"; | |
} | |
} | |
// Statt dessen lassen sich die Spalten auch einzeln ausgeben - | |
// mehr Schreibaufwand, aber auch mehr Kontrolle über das HTML | |
// echo "<td>".$daten['Hauptstadt']."</td>"; | |
// echo "<td>".$daten['Sprache']."</td>"; | |
// echo "<td>".$daten['Waehrung']."</td>"; | |
// echo "<td>".$daten['Flaeche']."</td>"; | |
echo "</tr>"; | |
} | |
echo "</table>"; | |
?> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function kopf($title) { | |
echo " | |
<html> | |
<head> | |
<title>$title</title> | |
</head> | |
<body> | |
<div>Navigation hier ...</div>"; | |
} | |
// Diese Funktion gibt nicht aus, sondern liefert | |
// die Fusszeile als Zeichenkette zurück | |
function fuss($jahreszahl=2011) { | |
return "<div>Copyright $jahreszahl by mir</div> | |
</body> | |
</html>"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include('miniseite_funktionen.php'); | |
kopf("Seite 1"); | |
?> | |
Das ist seite 1 | |
<a href="seite2.php">Zur seite 2</a> | |
<?php echo fuss(2001); ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include('miniseite_funktionen.php'); | |
kopf("Seite 2"); | |
?> | |
<h2>Das ist seite 2</h2> | |
<a href="seite1.php">Zur seite 1</a> | |
<?php echo fuss(); ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Formular mit Selbstverweis</title> | |
</head> | |
<body bgcolor="<?php | |
if(empty($_POST["hintergrund"])) { | |
echo "#ccffcc"; | |
} | |
else | |
{ | |
echo $_POST["hintergrund"]; | |
} ?>"> | |
<h3>Hintergrundfarbe wählen</h3> | |
Wählen Sie eine Hintergrundfarbe für die Datei aus: | |
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST"> | |
<!-- Textfeld vorbelegen: --> | |
<p>Name:<input type="text" name="name" value="<?php if(!empty($_POST['name'])) { echo $_POST['name'];} ?>"></p> | |
<!-- Auswahl des Radio-Button behalten mit if-abfrage und ausgabe von checked --> | |
<p><input type="radio" name="hintergrund" value="#FFFF00" <?php if(!empty($_POST["hintergrund"]) && $_POST["hintergrund"] == "#FFFF00") { echo " checked='checked'"; } ?>>gelb | |
<input type="radio" name="hintergrund" value="#8FEC95" <?php if(!empty($_POST["hintergrund"]) && $_POST["hintergrund"] == "#8FEC95") { echo " checked='checked'"; } ?>>hellgrün | |
<input type="radio" name="hintergrund" value="#58EBE4" <?php if(!empty($_POST["hintergrund"]) && $_POST["hintergrund"] == "#58EBE4") { echo " checked='checked'"; } ?> >hellblau | |
<input type="radio" name="hintergrund" value="#FFFFFF" <?php if(!empty($_POST["hintergrund"]) && $_POST["hintergrund"] == "#FFFFFF") { echo " checked='checked'"; } ?>>weiß </p> | |
<p><input type="submit" name="absenden" value="Absenden"></p> | |
</form> | |
<?php | |
if(isset($_POST["absenden"]) && isset($_POST["hintergrund"])) | |
{ | |
echo "<i>Ihre Auswahl wird als Hintergrundfarbe angezeigt.</i>"; | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment