I have array in session, but I can't show them. My code: Controller:
$game = $request->query->get('game');
$type = $request->query->get('type');
$odd = $request->query->get('odd');
$kupon = array(
'game' => $game,
'type' => $type,
'odd' => $odd,
);
$this->get('session')->set('kupon', $kupon);
Where 'game' is for example: 'Arsenal - Chelsea', 'type' is number like 1, and odd is float number like '2.2'.
Twig file:
{% if app.session.get('kupon') is not null %}
<table>
{% for kupon in session %}
<tr>
<td>{{ kupon.game }}</td>
<td>{{ kupon.type }}</td>
<td> </td>
</tr>
{% endfor %}
</table>
And everything ok, but when I try log in and show data session, I have this error:
Impossible to access an attribute ("game") on a string variable ("PNdjNUeuZ_d5uJlm1VG7zPZhp2Vb4CY3nDf93vAQ574") in baw\kupon.html.twig at line 13.
This variable is from session after log in, I checked dump information about it:
array(3) {
["_csrf/login"]=> string(43) "PNdjNUeuZ_d5uJlm1VG7zPZhp2Vb4CY3nDf93vAQ574" ["login"]=> string(4) "test"
["kupon"]=> array(3) { ["game"]=> string(31) "Arsenal Londyn - Chelsea Londyn" ["type"]=> string(1) "1" ["odd"]=> string(3) "2.2" } }
And now I haven't any idea to fix this.
{% for kupon in session %}
will not loop over app.session.get('kupon')
what you want to do is:
{% for kupon in app.session.get('kupon') %}
But looking at you dumped data, app.session.get('kupon')
is just a single dataset, so you cant even loop that (with desired results)...
it will be just:
{{ app.session.get('kupon').game }}
extra explanations about your data: you have this in you session:
"kupon" => [
"game" => ...
"type" => ...
...
]
to be able to loop over these, you need to make a collection of your data types:
"kupon" => [
[
"game" => ...
"type" => ...
...
],
[
"game" => ...
"type" => ...
...
],
....
]
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments