DragonOsman

"sell", pset7

Dec 20th, 2016
206
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. sell.php:
  2.  
  3. <?php
  4.  
  5.     require("../includes/config.php");
  6.    
  7.     $current_stock = CS50::query("SELECT symbol FROM portfolios WHERE user_id = ?", $_SESSION["id"]);
  8.    
  9.     render("selling_form.php", ["stocks" => $current_stock, "title" => "Sell"]);
  10.  
  11. ?>
  12.  
  13. selling_form.php:
  14.  
  15. <form action="sell_stock.php" method="post">
  16.     <div class="form-group">
  17.         <select class="form-control" name="symbol">
  18.             <option disabled selected value="">Symbol</option>
  19.             <?php foreach ($stocks as $stock): ?>
  20.                 <option value="<?= $stock["symbol"] ?>"><?= $stock["symbol"] ?></option>
  21.             <?php endforeach ?>
  22.         </select>
  23.         <br/>
  24.         <br/>
  25.         <input type="submit" class="btn btn-default" value="Sell"/>
  26.     </div>
  27. </form>
  28.  
  29. sell_stock.php
  30.  
  31. <?php
  32.  
  33.     require("../includes/config.php");
  34.    
  35.     $shares = CS50::query("SELECT shares, symbol FROM portfolios WHERE user_id = ?", $_SESSION["id"]);
  36.    
  37.     $delete_from_portfolios = CS50::query("DELETE FROM portfolios WHERE user_id = ? AND symbol = ?", $_SESSION["id"], $shares[0]["symbol"]);
  38.    
  39.     $stock = lookup($_POST["symbol"]);
  40.     if ($stock === false)
  41.     {
  42.         apologize("invalid symbol");
  43.     }
  44.    
  45.     $total_cash = $stock["price"] * $shares[0]["shares"];
  46.    
  47.     $positions = [];
  48.    
  49.     $update = CS50::query("UPDATE users SET cash = cash + ?", $total_cash);
  50.        
  51.     foreach ($shares as $share)
  52.     {
  53.         $positions[] = [
  54.             "name" => $stock["name"],
  55.             "price" => $stock["price"],
  56.             "shares" => $share["shares"],
  57.             "symbol" => $stock["symbol"],
  58.             "total" => $stock["price"] * $share["shares"]
  59.         ];
  60.     }
  61.    
  62.     render("portfolio.php", ["positions" => $positions, "cash" => $total_cash, "title" => "Portfolio"]);
  63.  
  64. ?>
RAW Paste Data