While trying to print user's portfolio (i.e. shares, price and symbol) in portfolio.php, I keep getting :
"Warning : Illegal string offset 'symbol' "
"Warning : Illegal string offset 'shares' "
"Warning : Illegal string offset 'price' "
Here's how I am trying to print:
<?php
foreach ($shares as $row)
{
echo("<tr>");
echo("<td>" . $row["symbol"] . "</td>");
echo("<td>" . $row["shares"] . "</td>");
echo("<td>$" . number_format($row["price"], 2) . "</td>");
}
?>
I have done var_dump($row)
for each variable and doing so prints the expected column value from the database.
That is, say the current user has a Google stock with symbol GOOG, then when I do:
var_dump($row["symbol"]
that prints:
'GOOG'
But I don't understand this "Illegal string offset ...."
warning when trying to print all the values in the portfolio template and how to resolve it.
Here is part of index.php
to which the warning is pointing (line 37 is one with call to render()
)
foreach($rows as $row)
{
$stock = lookup($row["symbol"]);
if($stock !== false)
{
// extracting stock info and adding to shares array
// if the information is in history database...
// then since we have the result in rows we take from $row
// else if we had to call lookup() to look for the info
// (such as stock price) then we take from $stock...
// result of calling lookup()
$shares = [
"symbol" => $row["symbol"],
"name" => $stock["name"],
"shares" => $row["shares"],
"price" => $stock["price"]
];
}
}
render("portfolio.php", ["title" => "Portfolio","username"=>$_SESSION["id"],"shares"=>$shares]);
And lastly here is the line the picture is pointing to in functions.php
require("../templates/$template");
Thank you