-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.php
213 lines (173 loc) · 7.69 KB
/
db.php
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
//creating database
//database connection
//create table Cafe(id,name,icon)
//create table Rate(id, food, service, value, comment, date, foreign key to Cafe)
//inserting test data
class DatabaseSetup{
private $db_host = "localhost";
private $db_username = "root";
private $db_password = "";
private $db_name = "FitnessFlex";
// SQL Connection
private $conn ="";
private $query="";
public function createDatabase($db_name){
$this->query="CREATE DATABASE IF NOT EXISTS $db_name";
$this->conn=mysqli_connect($this->db_host,$this->db_username,$this->db_password);
if(mysqli_query($this->conn,$this->query)){
$this->conn=mysqli_connect($this->db_host,$this->db_username,$this->db_password,$db_name);
return true;
}
return false;
}
public function connect($db_name){
$this->conn=mysqli_connect($this->db_host,$this->db_username,$this->db_password,$db_name);
}
function createTable($table_name,$columns){
$this->query = "CREATE TABLE $table_name(";
foreach($columns as $column){
$this->query .= $column.",";
}
$this->query=substr_replace($this->query,"",-1);
$this->query.=");";
if (mysqli_query($this->conn,$this->query)) {
return true;
}
// echo mysqli_error($this->conn);
return false;
}
public function changeBalance($id,$balance){
$this->query = "UPDATE clients SET balance='$balance' WHERE id=$id;";
if (mysqli_query($this->conn,$this->query)) {
return true;
}
else return false;
}
//returns array
public function getDataByDate($startDate,$endDate){//2013-07-06 format 2013-07-07
$this->query = "SELECT clientID,fees.doe,name,contact,cnic,fees.id,monthly,training,discount,received,adjustment,fees.balance,received_by FROM clients INNER JOIN fees WHERE fees.clientID = clients.id and fees.doe BETWEEN'$startDate' AND '$endDate';";
return mysqli_query($this->conn,$this->query);
}
public function insertInto($table_name,$columns,$values){
$this->query = "INSERT INTO $table_name(";
foreach($columns as $column){
$this->query .= $column.",";
}
$this->query=substr_replace($this->query,"",-1);
$this->query.=") VALUES (";
foreach($values as $value){
$this->query .= "'$value'".",";
}
$this->query = substr_replace($this->query,"",-1);
$this->query .= ")";
if (mysqli_query($this->conn,$this->query)) {
$last_id = mysqli_insert_id($this->conn);
return $last_id;
}
echo mysqli_error($this->conn);
echo $this->query;
return 0;
}
public function insertIntoFees($table_name,$columns,$values){
$this->query = "INSERT INTO $table_name(";
foreach($columns as $column){
$this->query .= $column.",";
}
$this->query=substr_replace($this->query,"",-1);
$this->query.=") VALUES (";
foreach($values as $value){
if($value=="CURDATE() + INTERVAL 7 DAY"){
$this->query.="CURDATE() + INTERVAL 7 DAY,";
}
else{
$this->query .= "'$value'".",";}
}
$this->query = substr_replace($this->query,"",-1);
$this->query .= ")";
if (mysqli_query($this->conn,$this->query)) {
$last_id = mysqli_insert_id($this->conn);
return $last_id;
}
echo mysqli_error($this->conn);
echo $this->query;
return 0;
}
public function getConn(){
return mysqli_connect($this->db_host,$this->db_username,$this->db_password,$this->db_name);
}
public function getData($table_name,$attribute,$id){
$this->query= "SELECT $attribute FROM $table_name WHERE id='$id'";
return mysqli_query($this->conn,$this->query);
}
public function getDataByContact($attribute,$contact){
$this->query= "SELECT $attribute FROM clients WHERE contact='$contact'";
return mysqli_query($this->conn,$this->query);
}
public function getBalance($id){
return mysqli_fetch_row($this->getData("clients","balance",$id))[0];
}
public function getFeeData($table_name,$attribute,$id,$paid){
$this->query= "SELECT $attribute FROM $table_name WHERE clientID='$id' AND paid='$paid'";
return mysqli_query($this->conn,$this->query);
}
public function payInvoice($id,$receivedBy){
$this->query= "UPDATE fees SET received=(SELECT (monthly+training-discount-adjustment) FROM fees WHERE id='$id'), received_by='$receivedBy', paid='1' WHERE id='$id'";
return mysqli_query($this->conn,$this->query);
}
public function getByCNIC($table_name,$attribute,$cnic){
$this->query= "SELECT $attribute FROM $table_name WHERE cnic='$cnic'";
return mysqli_query($this->conn,$this->query);
}
public function getDataAll($table_name,$attribute){
$this->query= "SELECT $attribute FROM $table_name";
return mysqli_query($this->conn,$this->query);
}
public function getNet($table_name,$attribute){
$this->query = "SELECT sum($attribute) as net FROM $table_name";
$result = mysqli_query($this->conn,$this->query);
$net = mysqli_fetch_assoc($result);
return (int) $net['net'];
}
public function getNetToday($table,$attribute){
$this->query = "SELECT sum($attribute) as net FROM $table where DATE(doe) = CURDATE();";
$result = mysqli_query($this->conn,$this->query);
$net = mysqli_fetch_assoc($result);
return (int) $net['net'];
}
public function getMaleFemale(){
$this->query = "SELECT COUNT(CASE WHEN gender='Male' THEN id END) male, COUNT(CASE WHEN gender='Female' THEN id END) female FROM clients";
$result = mysqli_query($this->conn,$this->query);
$net = mysqli_fetch_assoc($result);
return [(int) $net['male'],(int) $net['female'] ];
}
public function getN($table){
$this->query = "SELECT count(id) as net FROM $table";
$result = mysqli_query($this->conn,$this->query);
$net = mysqli_fetch_assoc($result);
return (int) $net['net'];
}
public function getNToday($table){
$this->query = "SELECT count(id) as net FROM $table where DATE(doe) = CURDATE();";
$result = mysqli_query($this->conn,$this->query);
$net = mysqli_fetch_assoc($result);
return (int) $net['net'];
}
public function getLastInsert(){
}
}
?>
<!--
CREATE TABLE clients(
id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR() NOT NULL,
fathername VARCHAR() NOT NULL,
gender VARCHAR(6) NOT NULL,
contact VARCHAR(11),
cnic VARCHAR(13) NOT NULL,
dob DATE,
doj DATE,
email VARCHAR(250),
package VARCHAR() NOT NULL,
doe datetime DEFAULT(CURDATE()));
-->