diff options
author | Santo Cariotti <santo@dcariotti.me> | 2023-01-07 18:51:03 +0100 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2023-01-07 18:51:03 +0100 |
commit | 9240342b366db9999f11659a1f1c396ba418ad78 (patch) | |
tree | f34e9259cbb6f8d4bcdea234e4b2c927950411cc /Year_3/TSDWL/PHP/json_fake_db.php | |
parent | ba36beaec6d37d26b075d96e58aad73151d6d39e (diff) |
Adds
Diffstat (limited to 'Year_3/TSDWL/PHP/json_fake_db.php')
-rw-r--r-- | Year_3/TSDWL/PHP/json_fake_db.php | 70 |
1 files changed, 70 insertions, 0 deletions
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(); +} +?> |