Fixing SQL and loop to dynamically place data into CGridView

'm currently using SQL to create a new dataprovider to fill my cgridview out.

The issue I’m having is that I’m having to target ID’s directly of the dynamic questions that users create, and add them one by one to the form view.

For example, the SQL I’m using is


MAX(CASE WHEN uv.user_type_variables_id = 1 THEN uv.value ELSE NULL END) Question1,

MAX(CASE WHEN uv.user_type_variables_id = 2 THEN uv.value ELSE NULL END) Question2,

MAX(CASE WHEN uv.user_type_variables_id = 3 THEN uv.value ELSE NULL END) Question3,

MAX(CASE WHEN uv.user_type_variables_id = 4 THEN uv.value ELSE NULL END) Question4,

MAX(CASE WHEN uv.user_type_variables_id = 5 THEN uv.value ELSE NULL END) Question5,

MAX(CASE WHEN uv.user_type_variables_id = 6 THEN uv.value ELSE NULL END) Question6

and I’ve had to put each one in the view like


 array(

            'header' => 'Question1',

            'name' => 'uv.user_type_variables_id',

            'type' => 'raw',

            'value' => '$data[\'Question1\']',

        ),

        array(

            'header' => 'Question2',

            'name' => 'uv.user_type_variables_id',

            'type' => 'raw',

            'value' => '$data[\'Question2\']',

        ),

Naturally that doesn’t work when I don’t know the ID and can’t programmatically changethe view everytime the user adds a field!

How do I go about targetting the SQL like above, then dynamically looping around and placing into the gridview without having to manually put in the fields?

I am only presuming that it is possible to target it this way, I can’t think of any other way to target each individual ID and name it so I can place it in the grid without declaring the ID