summaryrefslogtreecommitdiff
path: root/Year_3/TSDWL/PHP/json_fake_db.php
blob: 30f0c5b8be6041dbc0cd6417a167418594fc1b09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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();
}
?>