-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstocks.php
More file actions
199 lines (150 loc) · 6.95 KB
/
Copy pathstocks.php
File metadata and controls
199 lines (150 loc) · 6.95 KB
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
<?php
// Initialize the session
session_start();
// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
header("location: login.php");
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"/>
<head>
<meta charset="UTF-8">
<title>Welcome</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
body{ font: 14px sans-serif; text-align: center; }
table, th, td { border: 1px solid black; }
th, td { padding: 4px; font-size: 1.5em; }
</style>
</head>
<div class="w3-top">
<div class="w3-bar w3-card w3-large" style="background-color:#bdfa7b;">
<a href="./home.php" class="w3-bar-item w3-left-align w3-button w3-padding-large w3-hover-gray" style="font-size:23px;">Home</a>
<a href="./stocks.php" class="w3-bar-item w3-left-align w3-button w3-hide-small w3-padding-large w3-hover-gray" style="font-size:23px;">Stocks</a>
<!--
<a href="#" class="w3-bar-item w3-left-align w3-button w3-hide-small w3-padding-large w3-hover-gray" style="font-size:23px;">Buy</a>
<a href="#" class="w3-bar-item w3-left-align w3-button w3-hide-small w3-padding-large w3-hover-gray" style="font-size:23px;">Sell</a>
-->
<label class="w3-bar-item w3-left-align w3-padding-large" style="font-size:23px;">Date: <?php echo date("Y-m-d",$_SESSION["demoDate"] ); ?></label>
<a onclick="show_hide();" class="w3-bar-item w3-right w3-button w3-hide-small w3-padding-large w3-hover-red"><i class="fa fa-user" style="font-size:32px;padding:medium;"><br><?php echo htmlspecialchars($_SESSION["username"]); ?></i></a>
<img src="ApeMoney.jpg" width="100" height="100" style="margin-left: 10.7%"/>
</div>
<body>
<p><h2>List of Stocks:</h2></p>
<?php // this line starts PHP Code
require_once "config.php";
//run a query to get all stock ticker
$sqlstatement = $mysqli->prepare("SELECT DISTINCT ticker FROM stocks;"); //prepare the statement
$sqlstatement->execute(); //execute the query
$stocks = $sqlstatement->get_result(); //return the results we'll use them in the web form
$sqlstatement = $mysqli->prepare("SELECT cash FROM account where id = " . $_SESSION["id"] . ";"); //prepare the statement
$sqlstatement->execute(); //execute the query
$sqlstatement->store_result();
// Bind result variables
$sqlstatement->bind_result($money);
$sqlstatement->fetch();
$sqlstatement->close();
$sql = "SELECT ticker, company_name, price, pDate, one_day, three_day FROM stocks NATURAL JOIN price_movement WHERE pDate = '".Date("Y-m-d",$_SESSION["demoDate"])."'";
//swap out price_history for price_movment
//add one_day and three_day to select clause
//change price_date to pDate in where
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
// Setup the table and headers
echo "<Center><table><tr><th>Ticker</th><th>Company Name</th><th>Price</th><th>Date</th><th>-1 Day</th><th>-3 Day</th></tr>";
// output data of each row into a table row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["ticker"]."</td><td>".$row["company_name"]."</td><td>".$row["price"]."</td><td>".$row["pDate"]."</td>";
$one_day_change = number_format($row["price"] - $row["one_day"],2,'.','');
if($one_day_change > 0){
echo "<td style=\"color: green;\"> $".$one_day_change."</td>";
}else if($one_day_change == 0){
echo "<td> ".$one_day_change."</td>";
}else{
echo "<td style=\"color: red;\"> $".$one_day_change." </td>";
}
$three_day_change = number_format($row["price"] - $row["three_day"],2,'.','');
if($three_day_change > 0){
echo "<td style=\"color: green;\"> $".$three_day_change."</td>";
}else if($three_day_change == 0){
echo "<td> ".$three_day_change."</td>";
}else{
echo "<td style=\"color: red;\"> $".$three_day_change." </td>";
}
//add one_day and three_date to table, maybe do some coloring or something like on home page to show if it is up or down
}
echo "</table></center>"; // close the table
echo "There are ". $result->num_rows . " results.";
// Don't render the table if no results found
} else {
echo "0 results";
}
if($_SERVER["REQUEST_METHOD"] == "POST") {
$stock = trim($_POST["stock"]);
$numshares = trim($_POST["num_shares"]);
$sql = $mysqli->prepare("SELECT price from price_history where price_date = ? and ticker = ?;"); //prepare the statement
$sql->bind_param("ss",$param_date, $param_stock);
$param_stock = $stock;
$param_date = date("Y-m-d",$_SESSION["demoDate"]);
$sql->execute(); //execute the query
$sql->store_result();
// Bind result variables
$sql->bind_result($stockPrice);
$sql->fetch();
$cost = ($stockPrice * $numshares);
// Prepare an insert statement
$sql = "INSERT INTO lots (id, ticker, num_shares, purchase_price, purchase_date) VALUES (?, ?, ?, ?, ?);";
if($stmt = $mysqli->prepare($sql)){
// Bind variables to the prepared statement as parameters
$stmt->bind_param("isdds", $param_id, $param_stock, $param_numshares, $param_stockPrice,$param_date);
// Set parameters
$param_id = $_SESSION["id"];
$param_stock = $stock;
$param_numshares = $numshares;
$param_stockPrice = $stockPrice;
$param_cost = $cost;
$param_date = date("Y-m-d",$_SESSION["demoDate"]);
if ($cost <= $money) {
if($stmt->execute()){
$sql = "UPDATE account SET cash = cash - ? WHERE id = ?;";
$statement = $mysqli->prepare($sql);
$statement->bind_param("di", $param_cost, $param_id);
$param_id = $_SESSION["id"];
$param_cost = $cost;
$statement->execute();
} else{
echo "Oops! Something went wrong. Please try again later.";
}
// Close statement
$stmt->close();
} else {
echo "Can't buy more than you can afford.";
}
}
}
$mysqli->close();
?> <!-- this is the end of our php code -->
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<p>Select Stock:<p>
<select name="stock">
<?php //iterate through the results of the department query to build the web form
while($stock = $stocks->fetch_assoc()) {
?>
<option value="<?php echo $stock["ticker"]; ?>"><?php echo $stock["ticker"]; ?>
</option>
<?php } //end while loop ?>
</select>
<p>Enter Amount Of Stocks: <input type=text size=10 name="num_shares">
<p> <input type=submit value="submit">
</form>
<?php
?>
</body>
</html>