I encountered a problem while doing pset7. In portfolio.php, I would like to include not only the info of stocks a user own, but also his/her username and cash as well. So I wrote the following code, query both from the "users" table and "portfolios" table. But then I had this "undefined index" problem.
<?php
// configuration
require("../includes/config.php");
if (isset($_SESSION["id"]))
{
//user info from table user
$user = CS50::query("SELECT username, cash FROM users WHERE id = ?", $_SESSION["id"]);
$user["cash"] = number_format($user["cash"], $decimals = 2, $dec_point = "." , $thousands_sep = "," );
..........
//get info of stocks that a user owns using query()
// render portfolio
render("portfolio.php", ["title" => "Portfolio", "user" => $user["username"], "cash" => $user["cash"], "positions" => $positions]);
}
else
{
// else render form
render("login_form.php", ["title" => "Log In"]);
}
?>
This is my portfolio.php code:
<div>
<?= "Username: " . $user . " Cash Available: " . $cash?>
</div>
<div>
<?php foreach ($positions as $position): ?>
<tr>
<td><?= $position["symbol"] ?></td>
<td><?= $position["name"] ?></td>
<td><?= $position["shares"] ?></td>
<td><?= $position["price"] ?></td>
</tr>
<br/>
<?php endforeach ?>
</div>