-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcartapi.php
158 lines (141 loc) · 4.29 KB
/
cartapi.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
<?php
require_once 'presets.php';
header("Content-type: text/plain; charset=utf-8");
function printCartAndAnswer($answer)
{
echo '{ "answer": "'.$answer.'", "cart": [ ';
$cnt=count($_SESSION['cart_content']);
for($i=0; $i<$cnt; $i++)
{
echo '{ "id": "'.$_SESSION['cart_content'][$i][0];
echo '", "count": "'.$_SESSION['cart_content'][$i][1];
echo '", "price": "'.$_SESSION['cart_content'][$i][2].'" }';
if($i != $cnt-1) echo ', ';
}
echo ' ] }';
}
if(!isset($_GET['cmd'])) die('{"error": "no command set", "errcode": "0"}');
$cmd=$_GET['cmd'];
switch($cmd)
{
case 'add': // (pid)
// adding specified item to the cart
$pid=$_GET['pid'];
if(!is_numeric($pid)) die('{ "error": "pid should be a number!", "errcode": "1" }');
session_start();
$cnt=count($_SESSION['cart_content']);
for($i=0; $i<$cnt; $i++)
{
if($_SESSION['cart_content'][$i][0]==$pid)
{
// this item is already in the cart, inc it's count
$_SESSION['cart_content'][$i][1]++;
printCartAndAnswer("increment");
session_write_close();
exit();
}
}
// this is a new item
dbConnect();
$result=mysql_query("SELECT price FROM Product WHERE id_product='$pid'");
$row=mysql_fetch_row($result);
mysql_close();
session_start();
$_SESSION['cart_content'][]=array($pid, 1, $row[0]);
session_write_close();
printCartAndAnswer("success");
break;
case 'rem': // (pid)
// removing one item from the cart
$pid=$_GET['pid'];
if(!is_numeric($pid)) die('{ "error": "pid should be a number!", "errcode": "1" }');
session_start();
$cnt=count($_SESSION['cart_content']);
for($i=0; $i<$cnt; $i++)
{
if($_SESSION['cart_content'][$i][0]==$pid)
{
array_splice($_SESSION['cart_content'], $i, 1);
printCartAndAnswer("success");
session_write_close();
exit();
}
}
printCartAndAnswer("fail");
break;
case 'inc': // (pid)
// incrementing amount of specified item
$pid=$_GET['pid'];
if(!is_numeric($pid)) die('{ "error": "pid should be a number!", "errcode": "1" }');
session_start();
$cnt=count($_SESSION['cart_content']);
for($i=0; $i<$cnt; $i++)
{
if($_SESSION['cart_content'][$i][0]==$pid)
{
$_SESSION['cart_content'][$i][1]++;
printCartAndAnswer("success");
session_write_close();
exit();
}
}
printCartAndAnswer("fail");
break;
case 'dec': // (pid)
// decrementing amount of specified item
$pid=$_GET['pid'];
if(!is_numeric($pid)) die('{ "error": "pid should be a number!", "errcode": "1" }');
session_start();
$cnt=count($_SESSION['cart_content']);
for($i=0; $i<$cnt; $i++)
{
if($_SESSION['cart_content'][$i][0]==$pid)
{
$current_amount=$_SESSION['cart_content'][$i][1];
if($current_amount <= 1) die('{ "error": "cannot decrease amount to value lesser than 1!", "errcode": "2" }');
$_SESSION['cart_content'][$i][1]--;
printCartAndAnswer("success");
session_write_close();
exit();
}
}
printCartAndAnswer("fail");
break;
case 'set': //(pid, value)
// setting specified count of specified item
// (if not exist, add it with specified count)
$pid=$_GET['pid'];
if(!is_numeric($pid)) die('{ "error": "pid should be a number!", "errcode": "1" }');
$value=$_GET['value'];
if(!is_numeric($value)) die('{ "error": "value should be a number!", "errcode": "1" }');
if($value < 1) die('{ "error": "value should be greater than 0!", "errcode": "1" }');
session_start();
$cnt=count($_SESSION['cart_content']);
for($i=0; $i<$cnt; $i++)
{
if($_SESSION['cart_content'][$i][0]==$pid)
{
$_SESSION['cart_content'][$i][1]=$value;
printCartAndAnswer("success");
session_write_close();
exit();
}
}
//printCartAndAnswer("fail");
// OK, that's a new element, let's calculate its price and add it into the cart
dbConnect();
$result=mysql_query("SELECT price FROM Product WHERE id_product='$pid'");
$row=mysql_fetch_row($result);
mysql_close();
$price=$row[0];
$_SESSION['cart_content'][]=array($pid, $value, $price);
printCartAndAnswer("success");
session_write_close();
break;
case 'get': // ()
// listing back cart content
session_start();
printCartAndAnswer("success");
break;
}
?>