How to display the db data at once with different column?
This is my YII
dropdown:
echo CHtml::dropDownList('select_gamecat',"",
CHtml::listData(GamesType::model()->findAll(),
'id', 'descr_en', 'descr'), //<--here
array(
'empty' => Yii::t('labels', 'choosecat'),
'id' => "select_gamecat",
'class' => "inputs",
'style'=>'width:369px'
));
I wish to combine the 'descr_en'
and 'descr'
I tried 'desrc_en.desrc'
and 'desrc_en + desrc'
but both doesn't work.
Any Suggestion ? Thanks
You can try it with an anonymous function:
echo CHtml::dropDownList('select_gamecat',"",
CHtml::listData(GamesType::model()->findAll(),
'id', function($data){
return $data->descr_en.$data->descr;
}),
array('empty' => Yii::t('labels', 'choosecat'),'id' => "select_gamecat", 'class' => "inputs",'style'=>'width:369px'));
Explaination
Yii will check if the third parameter is an anonymous function, if yes, it will run it (with call_user_func) and the first parameter is the current model.
you could recreate your array , as:
$models = GamesType::model()->findAll();
$data = array();
foreach ($models as $model)
$data[$model->id] = $model->descr_en . ' '. $model->descr;
echo CHtml::dropDownList($model, 'id', $data ,array('prompt' => 'Select'));
Just you need to create a getter in model class GamesType
as:
class GameTypes extends CActiveRecord {
# ...
public function getDesrc_enAndDesrc() {
return "$this->desrc_en $this->desrc";
}
# ...
}
then use above getter as property in CHtml::listData
method as:
echo CHtml::dropDownList('select_gamecat',"",
CHtml::listData(GamesType::model()->findAll(),
'id', 'Desrc_enAndDesrc'), # <== HERE
array('empty' => Yii::t('labels', 'choosecat'),'id' => "select_gamecat", 'class' => "inputs",'style'=>'width:369px'));