I am trying to create “view” or a function that will build a HTML table by being given the columns in the table and the table data itself. This will be a page called by the renderPartial method in Yii.
I imagined doing it something like this (will demonstrate using pseudo-code):
void view(array $cols, array $tabledata)
{
//$tabledata will be an array of CActiveRecord objects. $cols is an array of strings from getColumnNames().
<table><thead><tr>
foreach($cols as $col)
{
<th>$col</th>
}
</tr></thead><tbody>
foreach($tabledata as $data)
{
<tr>
foreach($cols as $col)
{
<td>$data->$col</td>
}
</tr>
}
</tbody></table>
}
However the issue I am running into is I cannot get an the columns for a relation. I found http://www.yiiframework.com/doc/api/1.1/CDbTableSchema but that seems to only give you the column names for a specific table. I need a way to get all the columns used when a relation will be used to generate the HTML table. Alternatively if someone has a better way of doing this let me know. Thanks!
Edit: As a note, the reason I am not using CGridView including the one I listed below, is that I don’t want to have to know what the columns are or the data. I want to be able to build the table using just code.
For anyone else who is looking to learn how to do this here is sort of how I came up with a solution:
That was what I was looking for, in case someone else wanted to try.