Skip to content
Advertisement

How to adress variable Columns of SQL query with TWIG

I am trying to address a specific column of a SQL query depending on the value of another row from the query.

The result I am getting from the SQL query is something like this:

Number | Value1 | Value2 | Value3 
1      |      3 |      5 |     4
3      |      5 |      2 |     6
2      |      1 |      3 |     2

For each row, I want to get the Value of the Column by the Index of the Number. So for example in the first row, I want to get the Value of ‘Value1’, which is 3. In the second row, I want to get the Value of ‘Value3’, which is 6, and so on.

Then I am iterating through the list with a TWIG for loop and can access the ‘Number’ parameter like this:

{% for key in examplelist|keys %}
   {% set number = examplelist[key].number %}
{% endfor %}

Unfortunately, I cannot address the ‘Value’-columns using that number.

I have tried things like:

{{examplelist[key].Value . number }}

or

{{examplelist[key].Value + number }}

or

{{examplelist[key].Value ~ number }}

but none of these work…

Advertisement

Answer

Okay, I figured it out.

The data gets returned as an object of the type stdClass, not as an array. Therefore you can’t access it like an array and it has to be converted using

$Examplelist = json_decode($Examplelist, true);
return $Examplelist;

in the php part.

Afterwards, it can be adressed by using

{% set value  = examplelist[key]["Value"~number] %}

Thank you for the Help anyways 🙂

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement