Click here for EduSec Demo EduSec Screenshots

Display custom data in Gridview using CArrayDataProvider

On 2013-06-10 - By Ravi Bhalodiya

Student table contains following fields:
id,name,enno,address

StudentController.php action

public function actionMyadmin()
{
        $list = Student::model()->findAll();

        $dataArray = array();
        foreach ( $list as $listdata=>$data )
        {
            $columns = array();
            $columns['id'] = $listdata+1;
            $columns['name'] = $data['name'];
            $columns['enno'] = $data['enno'];
            $columns['address'] = $data['address'];
            $dataArray[] = $columns;
        }

        $dataProvider = new CArrayDataProvider($dataArray);
        $this->render('myadmin',array(
            'dataProvider'=>$dataProvider,
        ));
}

myadmin.php view file

<?php
$this->breadcrumbs=array(
    'Students'=>array('index'),
    'Manage',
);?>

<h1>Manage Students</h1>
<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'student-grid',
    'dataProvider'=>$dataProvider,
    'columns'=>array(
        'id',
        'name',
        'enno',
        'address',   
    ),
)); ?>

Here is the output: