基于Yii(关系)数据库表动态创建html表?

Pra*_*eek 1 html php styling yii

我试图通过给出表中的列和表数据本身来创建"视图"或将构建HTML表的函数.这将是Yii中renderPartial方法调用的页面.我想像这样做(将演示使用伪代码):

    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>
    }
Run Code Online (Sandbox Code Playgroud)

然而,我遇到的问题是我无法获得关系的列.我找到了http://www.yiiframework.com/doc/api/1.1/CDbTableSchema,但似乎只给出了特定表的列名.我需要一种方法来获取关系将用于生成HTML表时使用的所有列.或者,如果有人有更好的方法,请告诉我.谢谢!

编辑:作为一个注释,我没有使用CGridView的原因,包括我下面列出的那个,是我不想知道列是什么或数据.我希望能够使用代码构建表.

dIn*_*0nG 8

没有必要创建这样的功能.CGridView正是您所需要的.它将数据呈现为html表.下面是示例代码.

<?php $this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider' => new CArrayDataProvider($tabledata),
        'columns' => array(
         //specify the colums you wanted here
        ),
    ));
?>
Run Code Online (Sandbox Code Playgroud)