Showing Array Value In Cgridview

i have a payment table. i wanna show payment type with array in cgridview

my array is:

$pay = array(0=>‘Cash’,1=>‘Credit Card’);

it works when i give array number manually 0 or 1:





array('name'=>'payment', 'filter'=>$pay, 'value'=>$pay[0]),




but i t didnt work when i try to take value from $data->pay (from database value)





array('name'=>'payment', 'filter'=>$pay, 'value'=>$pay[$data->pay]),




all i want is it must show payment "Cash" when $data->pay = 0 and "Credit Card" when $data->pay = 1. but it doesnt recognize $data->pay with array value.

$data->pay work when i write directly (without array) so there isnt any problem in database:





array('name'=>'payment', 'filter'=>$pay, 'value'=>$data->pay),




it returns 1 or 0 correcltly. but i want it returns array value with string. any idea? thanks

I am guessing the database has the value 0 or 1, right? So, it is static that O refers to ‘Cash’ and 1 refers to ‘Credit Card’. If that is so, then you could simply do:




'type' => 'raw',

'value' => function($data) { return $data -> pay = 0 ? 'Cash' : 'Credit Card'; }



thanks but it doesnt work. i dont know why but $data->pay doesnt work on payment column. for example it works when i add it to users colum like this:


array('name'=>'user','value'=>'$data->pay . " " . $data->user',),

it returns "0 Mike Nelson" , "1 Linda Nelson" etc.

but in payment column it returns empity value. for example your code returns else value Credit Cart for all rows cos of $data->pay has no value


'value' => function($data) { return $data -> pay = 0 ? 'Cash' : 'Credit Card'; }

but strange thing is $data->pay works when i add directly:


'value' => $data -> pay

it returns all 0 and 1 values correctly. but when i use it in any code like array or something else it doesnt work…

Try




'value' => function($data) { return $data -> pay == 0 ? 'Cash' : 'Credit Card'; }



( with two "="). That should do the magic.

Another tip: you should consider not using "0" for real-life value/option. Could bring you some difficulties in the future. Have been there :wink:

Can simplify to this:

‘value’ => ‘$data->pay == 0 ? “Cash” : “Credit Card”’,

great! it works, but i wanna use that way


'value' => '$payment[$data->pay]', 

i have an array like this:


$payment = array(0=>'Cash',1=>'Credit Card',2=>'Check',3=>'Other Cards');

cos i wanna add other options like check and other cards etc.

If the options are dynamic or you wanted to add more in the future, the best way to go with is having those options in database. Create seperate table to store these value and have relation to main table. That way, you could simple use


'value' => '$data -> pay -> value'

Something like that… I hope you understood the above convention.

ok finally i made it. declerated a function to get payment type and it works now.




array('name'=>'payment','filter'=>$payment, 'value' => function($data) { getpayment($data->pay); }, ),






function getpayment($pay){

	$pt = array(0=>'Cash',1=>'Credit Card',2=>'Check',3=>'Other Cards');

	echo $pt[$pay];	

}



thanks for your helps guys…