Cgridview Insert Dynamic Chhtml::link Into Column

Hello everybody,

I’m looking to insert a CHtml::link into a column on my CGridview. The catch is I want to build the CHtml::link string and return it from a function as I’d also like to have some rows with a plain string, like so:


$this->widget('zii.widgets.grid.CGridView', array(

    'id'=>'organizations-grid',

    'dataProvider'=>$model->search(),

    'columns'=>array(

        'title',

        array(

            'header'=>'Students',

            'type'=>'raw',

            'value'=>'getNumStudents($data)',

        ),

    ),

));


function getNumStudents($data) {

	$output=Yii::app()->db->createCommand($sql)->query()->readAll();

	

	if($output[0]["count"] == 0) {

		return "No students added";

	} else {

		return 'CHtml::link("View '.$output[0]["count"].' students", array("/organizations/getStudents"), array("students"=>"'.$output.'"))';

	}

}



However, when this runs, it just prints the return value as a string instead of a link; CHtml::link prefix and all. Thanks in advance for the help, I could really use it!

You have to remove single quotes in your return string from


getNumStudents()

function.


function getNumStudents($data) {

        $output=Yii::app()->db->createCommand($sql)->query()->readAll();

        

        if($output[0]["count"] == 0) {

                return "No students added";

        } else {

                return CHtml::link("View " . $output[0]["count"]. " students", array("/organizations/getStudents"), array("students" => $output));

        }

}

Hope this helps you.

Thanks,

Saurabh Dhariwal