Here is the php code. The functions require, query and render are given to us.
<?php
// configuration
require("../includes/config.php");
$rows = CS50::query("SELECT `symbol`, `shares`, `cash` FROM `portfolios`, `users` WHERE ?", $_SESSION["id"]);
$positions = [];
foreach ($rows as $row)
{
$stock = lookup($row["symbol"]);
$total = ($stock["price"] * $row["shares"]);
if ($stock !== false)
{
$positions[] = [
"name" => $stock["name"],
"price" => $stock["price"],
"shares" => $row["shares"],
"symbol" => $row["symbol"],
"total" => $total,
"cash" => $row["cash"]
];
}
}
// render portfolio
render("portfolio.php", ["positions" => $positions, "title" => "Portfolio"]);
Here is my HTML output
<div id="middle">
<table class="table table-striped">
<thead>
<tr>
<th >Symbol</th>
<th >Name</th>
<th >Shares</th>
<th >Price</th>
<th >TOTAL</th>
</tr>
</thead>
<tbody>
<?php foreach ($positions as $position): ?>
<tr>
<td align="left" ><?= $position["symbol"] ?></td>
<td align="left" ><?= $position["name"] ?></td>
<td align="left" ><?= $position["shares"] ?></td>
<td align="left" ><?= number_format($position["price"], 2) ?></td>
<td align="left" ><?= number_format($position["total"], 2) ?></td>
</tr>
<?php endforeach ?>
<tr>
<td colspan="4" align="left">CASH</td>
<td align="left"><?= number_format($position["cash"], 2) ?></td>
</tr>
</tbody>
</table>
My guess is that there is something wrong with my foreach loop. But im not quite sure, there could also be some faults in my SQL database.
My mySQL database consist of 3 rows user_id
, symbol
, shares
. And for my user_id I have 3 diffrent stocks with like 10 shares each.
Anyone know what could be wrong?