summaryrefslogtreecommitdiff
path: root/Year_3/TSDWL/PHP
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2023-01-07 18:51:03 +0100
committerSanto Cariotti <santo@dcariotti.me>2023-01-07 18:51:03 +0100
commit9240342b366db9999f11659a1f1c396ba418ad78 (patch)
treef34e9259cbb6f8d4bcdea234e4b2c927950411cc /Year_3/TSDWL/PHP
parentba36beaec6d37d26b075d96e58aad73151d6d39e (diff)
Adds
Diffstat (limited to 'Year_3/TSDWL/PHP')
-rw-r--r--Year_3/TSDWL/PHP/index.php14
-rw-r--r--Year_3/TSDWL/PHP/json_fake_db.php70
2 files changed, 84 insertions, 0 deletions
diff --git a/Year_3/TSDWL/PHP/index.php b/Year_3/TSDWL/PHP/index.php
new file mode 100644
index 0000000..a36066f
--- /dev/null
+++ b/Year_3/TSDWL/PHP/index.php
@@ -0,0 +1,14 @@
+<?php
+require 'json_fake_db.php';
+?>
+<html>
+ <head>
+ </head>
+ <body>
+ <h1>Movies JSON db</h1>
+ <?php
+ $movies = ReadAllFilms();
+ echo $moviws;
+ ?>
+ </body>
+</html>
diff --git a/Year_3/TSDWL/PHP/json_fake_db.php b/Year_3/TSDWL/PHP/json_fake_db.php
new file mode 100644
index 0000000..30f0c5b
--- /dev/null
+++ b/Year_3/TSDWL/PHP/json_fake_db.php
@@ -0,0 +1,70 @@
+<?php
+$GLOBALS['FileName'] = "JSONFAKEDB.txt";
+$GLOBALS['ArrayFakeDB'] = array();
+
+function LoadFromJson()
+{
+ if($file = @fopen($GLOBALS['FileName'], "r")) {
+ $GLOBALS['ArrayFakeDB'] = json_decode(fread($file, filesize($GLOBALS['FileName'])), true);
+
+ fclose($file);
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+}
+
+function SaveToJson()
+{
+ if($file = fopen($GLOBALS['FileName'], "w")) {
+ $s = json_encode($GLOBALS['ArrayFakeDB']);
+ if(fwrite($file, $s)) {
+ fclose($file);
+ return true;
+ }
+ else
+ {
+ //print "<b>error: can't write on $FileName</b><br>";
+ fclose($file);
+ return false;
+ }
+ }
+ else
+ {
+ //print "<b>error: can't open $FileName</b><br>";
+ return false;
+ }
+}
+
+function CreateFilm($title, $details)
+{
+ LoadFromJson();
+ $array=$GLOBALS['ArrayFakeDB'];
+ $array[$title]=$details;
+ $GLOBALS['ArrayFakeDB']=$array;
+ SaveToJson();
+}
+
+function ReadAllFilms()
+{
+ LoadFromJson();
+ return $GLOBALS['ArrayFakeDB'];
+}
+
+function UpdateFilm($old_title, $title, $details)
+{
+ DeleteFilm($old_title);
+ CreateFilm($title, $details);
+}
+
+function DeleteFilm($title)
+{
+ LoadFromJson();
+ $array=$GLOBALS['ArrayFakeDB'];
+ unset($array[$title]);
+ $GLOBALS['ArrayFakeDB']=$array;
+ SaveToJson();
+}
+?>