<?php // configuration require "../includes/config.php"; // query database for user's transaction history $transactions = CS50::query("SELECT * FROM history WHERE user_id = ?", $_SESSION["id"]); if (count($transactions) == 0) { apologize("No transactions to display."); } else { // format price to at least two decimal places but no more than four $num_transactions = count($transactions); for ($i = 0; $i < $num_transactions; $i++) { $transactions[$i]["price"] = desired_decimal_format($transactions[$i]["price"]); } render("history.php", ["transactions" => $transactions, "title" => "History"]); }
<?php // configuration require "../includes/config.php"; // if user reached page via GET (as by clicking a link or via redirect) if ($_SERVER["REQUEST_METHOD"] == "GET") { // else render form render("quote_form.php", ["title" => "Get Quote"]); } else { if ($_SERVER["REQUEST_METHOD"] == "POST") { // lookup stock symbol $stock = lookup($_POST["symbol"]); // ensure stock exists if ($stock != false) { // format price to at least two decimal places but no more than four $stock["price"] = desired_decimal_format($stock["price"]); render("quote.php", ["title" => "Quote", "stock" => $stock]); } else { apologize("Stock does not exist."); } } }
<?php // configuration require "../includes/config.php"; // query database for user's information $user = CS50::query("SELECT * FROM users WHERE id = ?", $_SESSION["id"]); $user[0]["cash"] = desired_decimal_format($user[0]["cash"]); // query database for user's holdings $rows = CS50::query("SELECT * FROM portfolios WHERE user_id = ?", $_SESSION["id"]); $positions = []; foreach ($rows as $row) { $stock = lookup($row["symbol"]); if ($stock !== false) { // calculate current value of holdings for each stock $total = $stock["price"] * $row["shares"]; // format price and total to at least two decimal places but no more than four $stock["price"] = desired_decimal_format($stock["price"]); $total = desired_decimal_format($total); $positions[] = ["name" => $stock["name"], "price" => $stock["price"], "shares" => $row["shares"], "symbol" => $row["symbol"], "total" => $total]; } } render("portfolio.php", ["positions" => $positions, "user" => $user, "title" => "Portfolio"]);