0

I've manged to get the stock price to come up correctly for one stock, but the other for some reason adopts the same value?

here's the example.

Controller:

        foreach ($rows as $row)
{
    $stock = lookup($row["symbol"]);
}
    $whats_price_precious = number_format($stock["price"], 2, '.', '');
    // render portfolio

render("portfolio.php", [ "whats_price_precious" => $whats_price_precious,"rows" => $rows,"cash" => $cash,"title" => "Portfolio"]);

Template:

    foreach ($rows as $row)

{
    print("<hr /><center><tr>");
    print("<td><p> <b>Type of stock owned:</b></p>{$row["symbol"]}<br /><br /></td>");
    print("<td><p><b>Amount of this stock owned:</b></p>{$row["shares"]}<br /><br /></td>");
    print("<td><p><b>Current market price: </b>" . $whats_price_precious."</td></p>");
    print("</tr></center><hr />");

Some database code etc omitted for obvious reasons.

0

My guess is that you're getting the price of the last stock in the query. $stock is a single structure, not an array. While it works to get the data for a single stock, it doesn't retain multiple lookups. You need to accumulate the data and populate an array with the output of the lookups inside the foreach loop. You can then pass the data through.

foreach ($rows as $row)
{
    $stock = lookup($row["symbol"]);
    if ($stock !== false)
    {
        $positions[] = [
            "name" => $stock["name"],
            "price" => $stock["price"],
            "shares" => $row["shares"],
            "symbol" => $row["symbol"]
        ];
    }
}

This may not be the best implementation, and some data may be redundant, but it works. I'll let you figure out how you want to implement the rest of that. ;-)

If this answers your question, please click on the check to accept. Let's keep up on forum maintenance. ;-)

1
  • Yep, I get it. I found the code in the pset spec and descrition around it a bit wiffy/waffy so just ignored it and went with what I did :/ haha. Thanks Cliff Nov 5 '15 at 23:06

You must log in to answer this question.

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