I am passing an associative array(positions) via render:
render("portfolio.php", [ "title" => "Portfolio","positions" => $positions]);
and trying to print it in portfolio using below code:
foreach ($positions as $position)
{
print("<tr>");
print("<td>{$position["name"]}</td>");
print("<td>{$position["symbol"]}</td>");
print("<td>{$position["shares"]}</td>");
print("<td>{$position["price"]}</td>");
print("</tr>");
}
but it gives me a whole list of errors: Here is a part of my index function:
$positions = [];
$balance = 10000.0000;
// query info from portfolios
$rows = CS50::query("SELECT * FROM portfolios WHERE user_id = ?", $_SESSION["id"]);
// enter portfolio details in the array 'positions'
foreach ($rows as $row)
{
$stock = lookup($row["symbol"]);
if ($stock !== false)
{
$positions[] = [
"name" => $stock["name"],
"price" => $stock["price"],
"shares" => $row["shares"],
"symbol" => $row["symbol"]
];
$positions[] = ["price" => number_format($positions["price"],2,'.',',')];
}
else
{
apologize("This is no stock in your portfolio");
}
}
$positions[] = ["balance" => $balance];
$positions[] = ["balance" => number_format($positions["balance"],2,'.',',')];
// render portfolio
render("portfolio.php", [ "title" => "Portfolio","positions" => $positions]);
I really can't find the error. What can be done ?? here's what var_dump outputs: