Calorie::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 30
c 0
b 0
f 0
rs 9.7
cc 2
nc 2
nop 0
1
<?php
2
//error_reporting(0);
3
class Calorie {
4
5
    private $conn;
6
    private $db_table = "calories";
7
8
    public $id;
9
    public $userid;
10
    public $title;
11
    public $calories;
12
    public $amount;
13
    public $date;
14
15
    public function __construct($db) {
16
        $this->conn = $db;
17
    }
18
19
    public function readByDay() {
20
21
        $query = "
22
        SELECT ID as id, Title as title, Calories as calories, Amount as amount
23
        FROM ". $this->db_table . "
24
        WHERE UserID = :userid
25
        AND Date = :date
26
        ";
27
28
        $stmt = $this->conn->prepare($query);
29
        $stmt->bindParam(':userid', $this->userid);
30
        $stmt->bindParam(':date', $this->date);
31
        $stmt->execute();
32
33
        return $stmt;
34
35
    }
36
37
    public function readDays($order = 'DESC') {
38
39
        $query = "
40
        SELECT Date as date FROM ". $this->db_table . "
41
        WHERE UserID = :userid
42
        GROUP BY Date
43
        ORDER BY Date ".$order;
44
45
        $stmt = $this->conn->prepare($query);
46
        $stmt->bindParam(':userid', $this->userid);
47
        $stmt->execute();
48
49
        return $stmt;
50
51
    }
52
53
    public function create() {
54
55
        $query = "
56
        INSERT INTO " . $this->db_table . " SET
57
        UserID = :userid,
58
        Title = :title,
59
        Calories = :calories,
60
        Amount = :amount,
61
        Date = :date
62
        ";
63
64
        $this->userid = htmlspecialchars(strip_tags($this->userid));
65
        $this->title = htmlspecialchars(strip_tags($this->title));
66
        $this->calories = htmlspecialchars(strip_tags($this->calories));
67
        $this->amount = htmlspecialchars(strip_tags($this->amount));
68
        $this->date = htmlspecialchars(strip_tags($this->date));
69
70
        $stmt = $this->conn->prepare($query);
71
        $stmt->bindParam(":userid", $this->userid);
72
        $stmt->bindParam(":title", $this->title);
73
        $stmt->bindParam(":calories", $this->calories);
74
        $stmt->bindParam(":amount", $this->amount);
75
        $stmt->bindParam(":date", $this->date);
76
77
        if ($stmt->execute()) {
78
            $this->id = $this->conn->lastInsertId();
79
            return true;
80
        }
81
82
        return false;
83
84
    }
85
86
    public function delete() {
87
88
        $query = "
89
        DELETE FROM " . $this->db_table . "
90
        WHERE ID = :id AND UserID = :userid
91
        ";
92
93
        $this->id = htmlspecialchars(strip_tags($this->id));
94
        $this->userid = htmlspecialchars(strip_tags($this->userid));
95
96
        $stmt = $this->conn->prepare($query);
97
        $stmt->bindParam(":id", $this->id);
98
        $stmt->bindParam(":userid", $this->userid);
99
100
        if ($stmt->execute()) {
101
102
            return true;
103
104
        }
105
106
        return false;
107
108
    }
109
110
}
111