summaryrefslogtreecommitdiff
path: root/Year_3/TSDWL/PHP
diff options
context:
space:
mode:
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();
+}
+?>