add files of code before my time in the class
This commit is contained in:
66
Zweites Jahr/alt/file_handling/file_write.php
Executable file
66
Zweites Jahr/alt/file_handling/file_write.php
Executable file
@@ -0,0 +1,66 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Document</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<form>
|
||||
<div>
|
||||
<label>First Name:
|
||||
<input name = "fn">
|
||||
</label>
|
||||
</div>
|
||||
<div>
|
||||
<label for = "ln">Last Name:</label>
|
||||
<input id = "ln" name = "ln">
|
||||
</div>
|
||||
|
||||
<button>Ab damit</button>
|
||||
|
||||
</form>
|
||||
|
||||
<?php
|
||||
//file: file_write.php
|
||||
$file = "testfile_1.txt";
|
||||
$fp = fopen($file, 'w'); // open file for writing
|
||||
if(!$fp) { // is fp = false?
|
||||
die("Cannot open file!"); // "die" breaks the script
|
||||
}
|
||||
|
||||
// insert one row
|
||||
/*
|
||||
$row = "This is a row\n";
|
||||
if(!fputs($fp, $row)) echo "Error on writing";
|
||||
else echo "writing succesful";
|
||||
|
||||
*/
|
||||
|
||||
// insert from an assoziative array
|
||||
/*
|
||||
$myArray = [
|
||||
"firstname" => "Michael",
|
||||
"lastname" => "Staudt",
|
||||
"email" => "staudt@wvss-mannheim.de"
|
||||
];
|
||||
|
||||
foreach($myArray as $attr => $value) {
|
||||
$row = "$attr: $value\n";
|
||||
fputs($fp, $row);
|
||||
}
|
||||
*/
|
||||
|
||||
// insert from form
|
||||
if(!empty($_REQUEST)) { //submit button clicked
|
||||
$row = "First Name: $_REQUEST[fn] | Last Name: $_REQUEST[ln]\n";
|
||||
fputs($fp, $row);
|
||||
}
|
||||
|
||||
|
||||
fclose($fp);
|
||||
?>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
1
Zweites Jahr/alt/file_handling/testfile_1.txt
Executable file
1
Zweites Jahr/alt/file_handling/testfile_1.txt
Executable file
@@ -0,0 +1 @@
|
||||
First Name: | Last Name:
|
||||
1
Zweites Jahr/alt/file_handling/testsfile_1.txt
Executable file
1
Zweites Jahr/alt/file_handling/testsfile_1.txt
Executable file
@@ -0,0 +1 @@
|
||||
This is a rowThis is a rowThis is a rowThis is a rowThis is a row
|
||||
50
Zweites Jahr/alt/file_handling/validate_date.php
Executable file
50
Zweites Jahr/alt/file_handling/validate_date.php
Executable file
@@ -0,0 +1,50 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Document</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<?php
|
||||
// validate_date.php
|
||||
|
||||
//ini_set("display_errors", "on");
|
||||
|
||||
// M A I N
|
||||
$date = "29.02.2400";
|
||||
if(isValidDate($date)) echo "<p>Datum $date ist <strong>gültig</strong></p>";
|
||||
else echo "<p>Datum $date ist <strong>ungültig</strong></p>";
|
||||
|
||||
|
||||
// Functions
|
||||
function isLeapYear(int $year): bool {
|
||||
return ($year % 4 == 0 && $year % 100 != 0 || $year % 400 == 0);
|
||||
}
|
||||
|
||||
function isValidDate(string $date): bool {
|
||||
$date_parts = ["Tag","Monat", "Jahr"];
|
||||
$date_array = explode(".", $date);
|
||||
|
||||
$days_of_month = [31,28,31,30,31,30,31,31,30,31,30,31];
|
||||
if(isLeapYear($date_array[2])) {
|
||||
$days_of_month[1] = 29;
|
||||
}
|
||||
|
||||
// Monat auf Gültigkeit prüfen
|
||||
if($date_array[1] < 1 || $date_array[1] > 12) return false;
|
||||
|
||||
// Tag auf Gültigkeit prüfen
|
||||
if($date_array[0] < 1 || $date_array[0] > $days_of_month[$date_array[1]-1]) return false;
|
||||
|
||||
// Jahr auf Gültigkeit prüfen
|
||||
if($date_array[2] < 0 || $date_array[2] > 3000) return false;
|
||||
|
||||
// Falls Datum gültig
|
||||
return true;
|
||||
}
|
||||
|
||||
?>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user