Can anyone help me with a problem in portfolio? I have the webpage completed and presenting all stocks in someones portfolio, but it prints each stock 5 times instead of just one. Below I have the code from index.php. It will be great if anyone can point out which part of my code creates this problem. Thanks
// configuration
require("../includes/config.php");
//First we look for the user id
$rows= CS50::query("SELECT * FROM Portfolios WHERE id=?", $_SESSION["id"]);
// we create an array to store all the information
$positions=array();
//Iterate over the rows of the database
foreach($rows as $row){
//using the lookup function
$stock=lookup($row["symbol"]);
//Check if the function works right and then add the information
if ($stock !== false){
$positions=array(
"name"=>$stock["name"],
"price"=>$stock["price"],
"shares"=>$row["shares"],
"symbol"=>$row["symbol"],
"total"=>$row["shares"] * $stock["price"]
);
}
}
$users= CS50::query("SELECT * FROM users WHERE id=?", $_SESSION["id"]);
$balance=$users[0]["cash"];
// render portfolio
render("portfolio.php", ["positions"=>$positions, "title"=>"Positions", "balance"=>$balance]);