1

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");

picture of the warning

Thank you

2
  • Welcome to the CS50 Stack Exchange community! Please review Markdown Editing ir order to make it easier for people to read your code. – abelinux Jan 5 '15 at 23:06
  • Thankyou, I will keep that in mind . – Avi Jan 6 '15 at 19:49
1

You're inconsistent, lookup returns $stock associative array, so use $stock["symbol"], not $row when assigning to $shares associative array.

2
  • I did not realize that lookup() was returning names as well (my bad). I was using row["name"] because the first query searches and returns the name of the stock, assigning it to $row. BUT even though I have changed row["symbol"] to stock["symbol"] when assigning to shares associative array,, I am still getting exact same warning. – Avi Jan 5 '15 at 19:42
  • I'm sorry, I'm afraid I'm getting confused. You are using $row in 2 different contexts, in the 2nd code excerpt where you have for $shares as $row, I assume $shares is returning the results of a mysql query of your ? If so, this might help: if $shares!=false, go ahead and declare the array as follows (try not to use $shares variable name again): $array[]=["symbol"=>$shares["symbol"], etc. ]. Take note, $array is declared with []. – ronga Jan 6 '15 at 7:55
0

[code]

foreach ($shares as $x => $x_value)
{
echo("" . $x_value . ""); }

[/code]

This solved the problem for me for now. But I know I should technically be able to access each index by name but I cannot.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .