0

Why is this code not passing a value of "value" into my portfolio.php ? I have no idea I reckon it should work the same as the other two values.

<?php

// configuration
require("../includes/config.php"); 

// render portfolio
// $user = 11;
$rows =  CS50::query("SELECT * FROM portfolios WHERE user_id = ? ",$_SESSION ["id"]);
$cash1 = CS50::query("SELECT cash FROM users WHERE id = ?", $_SESSION["id"]);


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

   $value = ["cash" => $cash1];




render("portfolio.php", ["positions" => $positions, "title" => "Portfolio", $value] );

?>

0

Because it is an associative array, you should use the arrow notation like you used with "positions" and "title"

(the way you did was passing an associative array inside the associative array)

And another thing: cs50::query returns an array of rows, even if the returned value was equivalent to only one row (that is: it would be an array containing only one row which you'd have to index with [0] )

1
  • 1
    Thank you man. I have changed it in two different ways. 1) I have added an element to $positions[] containing a value of $cash1[0]["cash"] and rendered as without $value at all and this worked:) 2) I have initialized another variable called $value and assigned to it a value of $cash1[0]["cash"] and than added a new element to render called simply "value" = $cash and this worked as well:) – Kamil Kug Oct 9 '16 at 18:43

You must log in to answer this question.

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