-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_storage.php
173 lines (138 loc) · 4.09 KB
/
function_storage.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
<?PHP
include_once "database.php";
// function to read data from database
function readDataFile($filename) {
if (! file_exists($filename)) {
$fileData = array();
} else {
$fileData = json_decode(file_get_contents($filename), true);
}
return $fileData;
}
// function to write data to database
function writeDataFile($filename,$fileData) {
file_put_contents($filename,json_encode($fileData));
}
// if(!isset($_SESSION['cart'])) {
// $_SESSION['cart'] = array();
// echo('YES');
// }
// function to create category list from main list
function createSubCategory($a){
global $datasource;
global $database;
$subcat = array();
foreach($database as $item){
if($item['product_category'] == $a){
array_push($subcat, $item);
writeDataFile($datasource,$subcat);
}
}
}
// function to create description page of each item
function createSubDescription($a){
global $datasource;
global $database;
$subcat = array();
foreach($database as $item){
if($item['product_name'] == $a){
array_push($subcat, $item);
writeDataFile($datasource,$subcat);
}
}
}
// creates the main product list and associated parameters
function createCatlog($b){
echo('<table>');
echo('<tr>
<th>Product ID</th>
<th>Product Name</th>
<th>Product Price</th>
<th>Product Quantity</th>
</tr>');
foreach ($b as $item) {
echo "<tr>";
echo("<td>".$item['product_id']."</td>");
printf ("<td>".'<a href="http://localhost/Assignment1/description.php?select_name=%s"> %s</a>'."</td>",
$item['product_name'],$item['product_name'] );
echo("<td>".$item['product_price']."</td>");
if(!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array();
echo('YES');
}
foreach($_SESSION['cart'] as $cart_item) {
if ($cart_item == $item['product_id']) {
$item['product_quantity'] = $item['product_quantity'] -1;
}
}
quantityCheck($item['product_quantity']);
echo("</tr>");
}
}
// create shopping cart and add / remove items from it
function makeCart (){
echo('<table id=cart >');
echo('<tr id=trcart >
<th>Product ID</th>
<th>Product Name</th>
<th>Product Price</th>
</tr>');
$totalPrice = 0;
global $database;
foreach ($_SESSION['cart'] as $cart_item) {
foreach($database as $items){
if ($items['product_id'] == $cart_item){
echo "<tr id=trcart >";
echo("<td class= td>".$items['product_id']."</td>");
echo("<td class = td>".$items['product_name']."</td>");
echo("<td class = td>".$items['product_price']."</td>");
printf("<td class =><input type='button' value='Remove from Cart' onclick='remove_from_cart(\"%s\")'></tr>",$cart_item);
echo("</td>");
$totalPrice = $totalPrice + $items['product_price'];
}
}
}
printf("<tr><td colspan='2'>Total Price</td><td>%.2f</td><td><button>Check out</button></td></tr>",$totalPrice);
echo('</table>');
}
// gives quantity product check function to check if its out of stock
function quantityCheck($check){
if ( $check <= 0 ){
echo ("<td>".'<span style="color:#FF0000;">Out of Stock</span>'."</td>" );
}
else{
echo("<td>".$check."</td>");
}
}
//create product description page
function createDescription($b){
foreach ($b as $item) {
echo("<tr>");
echo("<td>".$item['product_id']."</td>");
printf ("<td>".$item['product_name']."</td>");
echo("<td>".$item['product_price']."</td>");
echo("<td>".$item['product_description']."</td>");
echo("</tr>");
echo("<tr>");
if ($item['product_quantity'] <1 ){
printf("<td><input type='button' style='color:#FF0000;' disabled value='Out of Stock' onclick='add_to_cart(\"%s\")'></td>",$item['product_id']);
}
else{
printf("<td><input type='button' value='Add to Cart' onclick='add_to_cart(\"%s\")'></td>",$item['product_id']);
}
echo("</tr>");
}
}
?>
<script>
function add_to_cart(item_id) {
window.location="?add_item="+item_id;
}
function remove_from_cart(item_id) {
window.location="?remove_item="+item_id;
alert("Are you sure");
}
function cat_select(category_select){
window.location = "?select_category="+category_select;
}
</script>