1

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.

1
  • so are you using symfony2 or 3? use the correct tags please... Commented Nov 25, 2016 at 12:38

2 Answers 2

2

{% 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" => ...
        ...
    ],
    ....
]
Sign up to request clarification or add additional context in comments.

3 Comments

ok, I changed it, but now everything is x3. Everything data with array I must have separately, for example: <td>name</td><td>surname</td>, now I have something like that: <td>name surname</td> <td>name surname</td>. I change to this: {% for kupon in app.session.get('kupon') %} and this: <td>{{ app.session.get('kupon').game }}</td>. I tried even kupon.game or something, but I had error: "Impossible to access an attribute ("game") on a string variable ("Arsenal Londyn - Chelsea Londyn") in baw\kupon.html.twig at line 13."
Did you tried {{ app.session.get('kupon').game }} ?
as I said, you can not put this in a loop, because the data is not a collection... you just have one "instance" of a "kupon"; Ive updated my answer with a clear example
0

Try below

{% if app.session.get('kupon') is not null %}
  {% set kupon = app.session.get('kupon') %}
  <table>
      <tr>
        <td>{{ kupon.game }}</td>
        <td>{{ kupon.type }}</td>
        <td> </td>
      </tr>
  </table>
{% endif %}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.