0

It seems like every hour I have another question I can't seem to figure out with pset7 :( I'm sorry to whoever is kindly answering all of these queries and I appreciate the help a lot! I know at least one other person who has had a similar undefined index problem, too, and am wondering if something might be causing it that I just don't have the knowledge base to figure out.

I am trying to get portfolio.php to work but for some reason my queries don't seem to be working. Right now I have:

$row = query("SELECT symbol, shares FROM Portfolio WHERE id=?", $_SESSION["id"]); 
$cash = query("SELECT cash FROM users WHERE id=11", $_SESSION["id"]); 

I keep being told cash is an undefined index and when I try to print it out as a test it doens't work. furthermore, when I pass the values as $positions to portfolio.php (a template) with this code:

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

And then I do foreach ($position as $positions)

I get told position is an undefined variable. So even though that one seems to exist in index.php it doesn't work in portfolio.php, while cash works in neither. I re-watched the videos many times but can't seem to find a typo in the code above. I have tried putting the variables in render in and out of quotes (just because I had to try something) but it made no difference.

1
  • Could you please execute and share the output of var_dump($positions)?
    – skreborn
    Jun 21 '14 at 12:36
-1

I myself have not yet used the query function, but based on description of this problem set, it returns an array of rows, not a single one. So I would say your best bet is to go with something like this:

$cash = query("SELECT cash FROM users WHERE id=11", $_SESSION["id"]);
$cash = $cash[0]['cash'];

Hope this solves your first problem. Feel free to ask if something isn't clear.


EDIT - there is actually an example for this on the pset7 page:

$rows = query("SELECT LAST_INSERT_ID() AS id");
$id = $rows[0]["id"];

Based on this, the returned value really is just an array of rows which you can take the first (0) element of.


As for the positions one, I need you to show me the output of var_dump($positions) so I can assist you further.

3
  • that tip about $rows containing an array that had to be indexed by using $rows[0] was super helpful. I had thought if there was only one row that query was returning this would not be necessary, but portfolio.php is working now. Thanks a lot! I'm glad it was something so simple.
    – user1298
    Jun 22 '14 at 21:23
  • Just out of curiosity, can you use render to pass an array of arrays into render and then just print out $cash[0]["cash"]?
    – user1298
    Jun 22 '14 at 21:34
  • @user1298 Sorry it took so long to reply. Unfortunately, I am unfamiliar with render.
    – skreborn
    Jun 23 '14 at 13:10

You must log in to answer this question.

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