-
Notifications
You must be signed in to change notification settings - Fork 0
/
buyStock.php
executable file
·212 lines (185 loc) · 9.89 KB
/
buyStock.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
<?php
//files used by this page
require_once 'utils/functions.php';
require_once 'classes/User.php';
require 'classes/DB.php';
require 'classes/StockTableGateway.php';
require_once 'classes/CustomerTableGateway.php';
start_session();
//insures user is logged in
if (!is_logged_in())
{
header("Location: login_form.php");
}
//sets user to session user
$user = $_SESSION['user'];
/*if(isset($_GET['page']))
{
$pages=array("products", "cart");
if(in_array($_GET['page'], $pages))
{
$_page=$_GET['page'];
}
else
{
$_page="products";
}
}
else
{
$_page="products";
} */
//gets connection
$connection = Connection::getInstance();
//uses connection in StockTableGateway
$gateway = new StockTableGateway($connection);
//uses connection in CustomerTableGateway
$custGateway = new CustomerTableGateway($connection);
//sql to find this user by their email
$customer = $custGateway->getCustByEmail($user->getUsername());
//sets the custome info from db to $custRow
$custRow = $customer->fetch(PDO::FETCH_ASSOC);
//gets all stock from the stock table using the getStock() method
$stock = $gateway->getStock();
//adds items to session cart
require 'functions/cartFunctions.php';
?>
<!DOCTYPE html>
<!--
John Kenny
N00145905
-->
<html>
<head>
<!--Meta Tags-->
<?php require 'utils/meta.php'; ?>
<!--page title-->
<title><?php echo $user->getUsername() . " Buy Stock"; ?></title>
<!--css stylesheets-->
<?php require 'utils/styles.php'; ?>
</head>
<body>
<!--sets bg for top nav-->
<div class="row topnavbg">
<!--imports navbar-->
<?php require 'utils/toolbar.php'; ?>
</div>
<!--main content-->
<div class="container">
<!--this is only displayed when the user has added stock to their cart-->
<div class ="row">
<!--uses full 12 columns of grid-->
<section class="col-lg-12 col-md-12 col-sm-12 col-xs-12 top">
<!--php to check cart-->
<?php
//checks if cart is set
if(isset($_SESSION['cart']))
{
//ENSURES CART IS NOT EMPTY AS STOCK MAY HAVE BEEN REMOVED
if (!empty($_SESSION['cart']))
{
//lets this to the las stock added to the cart
$newitem = end($_SESSION['cart']);
//set to last stock id by the for each loop below
$id;
//calculated by the for each loop
$total = 0;
//loops through each stock changing id to the current stock of the loop
foreach($_SESSION['cart'] as $id => $value)
{
//sets total to qty*price of each stock. adds previuos value every time loop is ran
$total += (($_SESSION['cart'][$id]['quantity']) *($_SESSION['cart'][$id]['price']));
}
//passes connection to StockTableGateway
$gateway3 = new StockTableGateway($connection);
//sql to get the stock with the last id in the cart
$stock4 = $gateway->getStockByNo($id);
//sets its values to row
$row = $stock4->fetch(PDO::FETCH_ASSOC);
//testers
//print_r($newitem);
//print_r($id);
//print_r($row);
//
//this is the displayed preview cart of last item added
//opening div
echo '<div class="preview-cart">';
//left div containsthe logo and added not shown on smaller devices
echo '<div class="tum-logo col-md-4 col-sm-2 hidden-xs"><div class="col-md-3 hidden-sm hidden-xs"> <i class="fa fa-check fa-3x"></i></div>'
. '<img src="image/logos/'.$row['image'].'">'
. '<span class="hidden-sm hidden-xs">Added to Cart</span></div>';
//middle row showing info on the last added stock
echo '<div class="new-cart col-md-6 col-sm-7 col-xs-6"><h2>Cart Total: $'.$total.'</h2>'
. '<p ><strong>Stock Name: </strong>'.$row['stockName'].'<span class="hidden-sm hidden-xs"> <strong>Share Price: </strong>$'.$row['currentPrice'].'</span></p>'
. '<p ><ins class="hidden-sm hidden-xs"><strong>Qty Ordered: </strong>'.$newitem['quantity'].'</ins><span > <strong>Total Price: </strong>$'.($newitem['quantity']*$row['currentPrice']).'</span></div>';
//final row shown the buttons to modify the cart
echo '<div class="col-md-2 col-sm-3 col-xs-6 center-all-content preview-btns"> <a class="buy_btn" href="buyStock.php?page=products&action=add&id='.$id.'"><i class="fa fa-cart-plus"></i></a>'
. '<a class="sell_btn" href="buyStock.php?page=products&action=remove&id='.$id.'"><i class="fa fa-cart-arrow-down"></i></a>'
. '<p><a href="viewcart.php" class="myButton smaller">View Cart</a></p></div>';
echo '</div>';
}
}//close if(isset($_SESSION['cart']))
?>
</section><!--close this cart preview section-->
</div><!--close row-->
<!-- new row displaying all stock that can be added to the cart-->
<div class ="row">
<!--uses full grid-->
<section class="col-lg-12 add-2-cart">
<!--breaks with top line-->
<hr>
<!--section heading-->
<h1>All Stock</h1>
<!--the stock will be displayed in a scrollable owl-carousel-->
<div id="our-stocks" class="owl-carousel">
<!--php to get all stock from db-->
<?php
//gets first row from stock table-->
$stockRow = $stock->fetch(PDO::FETCH_ASSOC);
//loops through all the rows of this table on the database
while ($stockRow)
{
//sets div contain this stock
echo'<div class="item">';
//adds the stock logo to the div
echo'<img src="image/logos/'.$stockRow['image'].'" class="backup_picture img-responsive">';
//inserts the stock name
echo '<h3><b>Stock Name: </b><span>'.$stockRow['stockName'].'</span></h3>';
//inserts the stock code
echo '<p><b>Stock Code: </b>'.$stockRow['stockCode'].'</p>';
//inserts the stock price
echo '<p><b>Current Price: </b> $'.$stockRow['currentPrice'].'</p>';
//inserts the button to add to cart & closes this div
echo '<a class="button button-block" href="buyStock.php?page=products&action=add&id='.$stockRow['stockId'].'">Add to cart</a></td>
</div>';
//ends the statement to stop an infinate loop on this row
//gets next stock row
$stockRow = $stock->fetch(PDO::FETCH_ASSOC);
}//close while
?>
</div><!--clsoes owl-carousel-->
<!--instructions for owl-carousel-->
<!--image on the right of text-->
<div class="col-md-3 col-sm-3 col-xs-3 right-img">
<img class="img-responsive img-circle" src="image/grab.png" alt="grap">
</div>
<!--centered text-->
<div class="col-md-6 col-sm-6 col-xs-6 center-all-content">
<h2>Grab or Scroll through our Stocks</h2>
</div>
<!--image on the left of text-->
<div class="col-md-3 col-sm-3 col-xs-3 left-img">
<img class="img-responsive img-circle" src="image/touch.png" alt="touch">
</div>
<div class="clearfix"></div>
<!-- bottom line and footer margin added-->
<hr class="pushfoot">
</section><!--close all stocks section-->
</div><!--close row-->
</div><!--close container-->
<!--imports footer-->
<?php require 'utils/footer.php'; ?>
<!--imports js-->
<?php require 'utils/scripts.php'; ?>
</body>
</html>