Yii Framework Forum: Passing Variable To Buttonurl Outside Of Data In Cgridview - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Passing Variable To Buttonurl Outside Of Data In Cgridview Rate Topic: -----

#1 User is offline   freshyiiuser 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 07-March 13

Posted 04 November 2013 - 02:24 PM

i my cgrid view, i need to pass three variable to the viewbuttonurl but these variables are outside of the model.

this is my code
$outvar = NULL;
$lettervar = NULL;
$certvar = NULL;
		
$this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'assignment-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array(
array(
                'class' => 'CButtonColumn',
                'template' => '{view}',
                
				
				'viewButtonUrl'=>'Yii::app()->controller->createUrl("view",array("id"=>$data->id,"outvar"=>$outvar, \'lettervar\'=>$lettervar, \'certvar\'=>$certvar))',
				//'updateButtonUrl'=>'Yii::app()->controller->createUrl("update",$data->primaryKey)',
				//'deleteButtonUrl'=>'Yii::app()->controller->createUrl("delete",$data->primaryKey)',

        ),


i got this error:
Undefined variable: outvar

C:\xampp\htdocs\yii\framework\base\CComponent.php(606) : eval()'d code(1)

what is the problem?
0

#2 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,694
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 04 November 2013 - 02:39 PM

The problem is that string expression evaluates in different context.

You can do this for example:

'viewButtonUrl'=>'Yii::app()->controller->createUrl("view",array("id"=>$data->id,"outvar"=>' . $outvar . ', \'lettervar\'=>' . $lettervar . ', \'certvar\'=>$certvar))'

God is real unless declared as integer
0

#3 User is offline   freshyiiuser 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 07-March 13

Posted 04 November 2013 - 03:41 PM

i write this:
				'viewButtonUrl'=>'Yii::app()->controller->createUrl("view",array("id"=>$data->id,"outvar"=>' . $outvar . ', "lettervar"=>' . $lettervar . ', "certvar"=>'. $certvar . '))',


the admin page now appears, but in the button place i got this error:
Parse error: syntax error, unexpected ',' in C:\xampp\htdocs\yii\framework\base\CComponent.php(606) : eval()'d code on line 1

where is the problem?
0

#4 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,694
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 04 November 2013 - 03:56 PM

You should probably wrap vars in quotes
"outvar"=>"' . $outvar . '",
because they can be empty.

And remember, btw, that this is not a good solution because of possible code injection.
I'd rather go with something like
$outvar =...;
$lettervar = ...;
$certvar = ...;
$url = Yii::app()->controller->createUrl("view", array(
    "id" => '_ID_',
    "outvar" => $outvar, 
    "lettervar" => $lettervar, 
    "certvar" => $certvar
));

'viewButtonUrl'=>'str_replace("_ID_", $data->id, "' . $url . '")'


But maybe you should look at the widget's source code (or docs if any), I suppose there's much better solution
God is real unless declared as integer
0

#5 User is offline   freshyiiuser 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 07-March 13

Posted 04 November 2013 - 04:11 PM

View PostORey, on 04 November 2013 - 03:56 PM, said:

You should probably wrap vars in quotes
"outvar"=>"' . $outvar . '",
because they can be empty.

And remember, btw, that this is not a good solution because of possible code injection.
I'd rather go with something like
$outvar =...;
$lettervar = ...;
$certvar = ...;
$url = Yii::app()->controller->createUrl("view", array(
    "id" => '_ID_',
    "outvar" => $outvar, 
    "lettervar" => $lettervar, 
    "certvar" => $certvar
));

'viewButtonUrl'=>'str_replace("_ID_", $data->id, "' . $url . '")'


But maybe you should look at the widget's source code (or docs if any), I suppose there's much better solution

0

#6 User is offline   freshyiiuser 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 07-March 13

Posted 04 November 2013 - 04:11 PM

Solved

thank you very much for your help
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users