DragonOsman

index.php and portfolio.php for portfolio - pset7

Dec 19th, 2016
272
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. index.php:
  2.  
  3. <?php
  4.  
  5.     // configuration
  6.     require("../includes/config.php");
  7.  
  8.     $positions = [];
  9.     $rows = CS50::query("SELECT shares, symbol FROM portfolios WHERE user_id = ?", $_SESSION["id"]);
  10.     $cash = CS50::query("SELECT cash FROM users WHERE id = ?", $_SESSION["id"]);
  11.     foreach ($rows as $row)
  12.     {
  13.         $stock = lookup($row["symbol"]);
  14.         if ($stock !== false)
  15.         {
  16.             $positions[] = [
  17.                 "name" => $stock["name"],
  18.                 "price" => $stock["price"],
  19.                 "shares" => $row["shares"],
  20.                 "symbol" => $row["symbol"],
  21.                 "total" => $row["shares"] * $stock["price"]
  22.             ];
  23.         }
  24.     }
  25.    
  26.     // render portfolio
  27.     render("portfolio.php", ["positions" => $positions, "cash" => $cash, "title" => "Portfolio"]);
  28.  
  29. ?>
  30.  
  31. portfiolio.php:
  32.  
  33. <div>
  34.     <table class="table table-striped table-hover">
  35.         <?php foreach ($positions as $position): ?>
  36.            
  37.             <thead>
  38.                 <th>Symbol</th>
  39.                 <th>Name</th>
  40.                 <th>Shares</th>
  41.                 <th>Price</th>
  42.                 <th>TOTAL</th>
  43.             </thead>
  44.             <tbody>
  45.                 <tr>
  46.                     <td><?= $position["symbol"] ?></td>
  47.                     <td><?= $position["name"] ?></td>
  48.                     <td><?= $position["shares"] ?></td>
  49.                     <td>$<?= $position["price"] ?></td>
  50.                     <td>$<?= $position["total"] ?></td>
  51.                 </tr>
  52.                 <tr>
  53.                     <td colspan="4">CASH</td>
  54.                     <td>$<?= $cash[0]["cash"] ?></td>
  55.                 </tr>
  56.             </tbody>
  57.            
  58.         <?php endforeach ?>
  59.     </table>
  60. </div>
RAW Paste Data