Yii Framework Forum: Expression in value of CGridView not working as expected - Yii Framework Forum

Jump to content

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

Expression in value of CGridView not working as expected Rate Topic: -----

#1 User is offline   peasandcarrots 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 28-March 11

Posted 27 April 2011 - 11:41 AM

I am using a ternary operator as an expression in the value as seen in my view's code below:

<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'trip-grid',
	'dataProvider'=>$dataProvider,
	// 'filter'=>$model,
	'columns'=>array(
    array(  'name'=>'f_user_admin',
						'header'=>'Admin?',
						'value'=>$data->f_user_admin,
        ),
    array(  'name'=>'f_user_admin',
						'header'=>'Admin?',
						'value'=>(($data->f_user_admin == 1) ? "Yes" : "No"),
        ),
		array(
			'class'=>'CButtonColumn',
        'viewButtonUrl'=>'Yii::app()->controller->createUrl("view",$data->primaryKey)',
        'updateButtonUrl'=>'Yii::app()->controller->createUrl("update",$data->primaryKey)',
        'deleteButtonUrl'=>'Yii::app()->controller->createUrl("delete",$data->primaryKey)',			
		),
	),
)); ?>


f_user_admin is a TINYINT(4) field in my mySQL database. The table has three records. f_user_admin has a value of 1 for the first record and a value of 2 for the other two records.

If you look at the attached image, you'll see that the first column displays the correct values from the database. The seconds column should display "Yes" for a value of 1, and "No" for a value of 0. However, as you can see by the image, it always displays "No".

Am I missing something?
0

#2 User is offline   peasandcarrots 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 28-March 11

Posted 27 April 2011 - 11:45 AM

Sorry, the file upload didn't work, here it is.

Attached File(s)


0

#3 User is offline   bglee 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 152
  • Joined: 21-November 10
  • Location:Oak Park, IL

Posted 27 April 2011 - 12:16 PM

The ternary part looks fine. I was going to suggest a datatype difference, but you're using "==", so you should be fine. What bothers me is the fact that both columns have the same name. The second column is really a computed field. Try changing the name and see if the same problem exists.
It takes 2 points to draw a straight line, but at least 3 to draw a conclusion.
0

#4 User is offline   peasandcarrots 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 28-March 11

Posted 27 April 2011 - 12:50 PM

View Postbglee, on 27 April 2011 - 12:16 PM, said:

The ternary part looks fine. I was going to suggest a datatype difference, but you're using "==", so you should be fine. What bothers me is the fact that both columns have the same name. The second column is really a computed field. Try changing the name and see if the same problem exists.


I tried changing the name as you suggested, but the problem persists. Before posting in the forum, I had also tried using gettype($data->f_user_admin) as the value, and that returns nothing, which probably means that it's not recognizing a data type for the variable. This doesn't make sense since, if I just put $data->f_user_admin as the value, it works (as it does in column 1 of the attached file in my second post). I also tried changing the data type in mySQL from tinyint(4) to int(11), just in case, but it didn't work.

Anymore ideas?
0

#5 User is offline   bglee 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 152
  • Joined: 21-November 10
  • Location:Oak Park, IL

Posted 27 April 2011 - 01:17 PM

This may sound like I'm grasping at straws, but what happens if you put single quotes around the expression?

'(($data->f_user_admin == 1) ? "Yes" : "No")',

It takes 2 points to draw a straight line, but at least 3 to draw a conclusion.
0

#6 User is offline   peasandcarrots 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 28-March 11

Posted 27 April 2011 - 01:24 PM

I figured it out. There has to not be quotes around the expression, as such:

'value'=>'(($data->f_user_admin == 1) ? "Yes" : "No")',


When I started playing around with expressions last week, I did have quotes around the expression, and that worked great for CGridView. However, when I tried using the same format for an expression in a value for a CDetailView, it would print the expression, literally, on the screen. You can see my post on that topic here. Someone suggested that I remove the quotes around the expression. I thought it worked at first, but only know am I realizing that it was not printing the correct value on the screen.

Anyway, this fixes the issue for the CGridView, but now I have to figure out how to get it to work for CDetailView. If you can help, let me know here.
1

#7 User is offline   Nisanth thulasi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 10-July 12
  • Location:Kochi

Posted 28 February 2013 - 03:53 AM

 'value'=>'(($data->status_type == Pri) ? "Private" : "Public")', 


its working
Thanks
Nisanth Thulasi
1

#8 User is offline   Alex D. 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 33
  • Joined: 15-October 11
  • Location:Ukraine

Posted 19 June 2013 - 03:39 PM

Oh, thanx it works for me too. Actually you can even get rid of (( and ) signs.
Seems like there some difference between value of CGridView and value of CDetailView
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