Add column as link in CGridView

2020-07-27 07:46发布

问题:

I need to add link to my gridview this link will call action with parameters like user_id and course_id .
so i wrote this code

View :

<?php
$cmodel=new Courses;

$user_id=intval($_GET['user_id']);



$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'courses-grid',
    'dataProvider'=>$cmodel->searchCources($user_id),
    'filter'=>$cmodel,
    'columns'=>array(
        'course_id',
        'name_ar',
        'name_en',
        'code',
        't_time',
        array(
            'name'  => 't_time',
            'value' => "CHtml::link('Add',array('packages/add','user_id'=>$user_id,'course_id'=>$data->course_id))",
            'type'  => 'raw',
        )
    ,

        /*
        'req',
        'aud',
        'targets',
        'programs',
        'criterion',
        'section_id',
        'acc',
        'en',
        'ar',
        'fr',
        'image',
        */


))); 
?>

Model :

    <?php
...

        public function searchCources($user_id)
{
    // Warning: Please modify the following code to remove attributes that
    // should not be searched.

    $criteria=new CDbCriteria;



    $criteria->compare('course_id',$this->course_id);
    $criteria->compare('name_ar',$this->name_ar,true);
    $criteria->compare('name_en',$this->name_en,true);
    $criteria->compare('code',$this->code,true);
    $criteria->compare('t_time',$this->t_time);
    $criteria->compare('intro',$this->intro,true);
    $criteria->compare('req',$this->req,true);
    $criteria->compare('aud',$this->aud,true);
    $criteria->compare('targets',$this->targets,true);
    $criteria->compare('programs',$this->programs,true);
    $criteria->compare('criterion',$this->criterion,true);
    $criteria->compare('acc',$this->acc);
    $criteria->compare('en',$this->en);
    $criteria->compare('ar',$this->ar);
    $criteria->compare('fr',$this->fr);
    $criteria->compare('image',$this->image,true);
      $criteria->addCondition(' course_id not in ( select course_id from ict_cpackage where user_id='.$user_id.') ');

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria
    ));
}
...
?>

But return this error :

Undefined variable: data 

1- Why return this error ? 2- how I can access to column value

Thanks in advance

回答1:

'value' => "CHtml::link('Add',array('packages/add','user_id'=>$user_id,'course_id'=>\$data->course_id))",

You are using "" to quote your string, so you need to escape the $ on $data, so it reads as \$data.



标签: php yii