0

I've been at this for a while now... My code in index.php

render ("portfolio.php", ["cash" => $_SESSION["cash"] ]

Then in portfolio.php

<?= $cash ?>

I get a error " Undefined index: cash" which is in portfolio... Help me please

1

Ok, so I will answer my own question.

What query does is make an array of arrays of sorts. So when you do:

<?php print_r($cash) ?>

You will get a output of all the 'things' in your array. The output says

([0] => Array([cash] => 10000.000))

notice how cash is nested in the 0th index of $cash. So to access this, you must first access array 0 of $cash by

$cash[0]

Then you must access the cash in array 0 by adding this on

$cash[0]['cash']

That fixes the problem, assuming your print_r outputs the actual value of cash.

0

You are trying to get the cash value from the session by using $_SESSION['cash']. Only the user id is stored in the session. You have to query the users table using that id to find the user's cash.

2
  • I did: $cash = query("SELECT cash FROM users WHERE id = ?", $_SESSION['id']"); Then did: render ("portfolio.php", ["cash" => $cash ] Then: <?= $cash ?> in portfolio.php bold But Then I get an error: array to string conversion for $cash in portfolio.php
    – Fijj
    Oct 29 '15 at 15:14
  • Oh, it says Array instead of the actual value for cash... I'm getting close but I'm not sure how to fix it
    – Fijj
    Oct 29 '15 at 15:20

You must log in to answer this question.

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