Ravi005
(Bhalodiyaravi)
April 25, 2013, 6:59am
1
I have table with following fields :
id
roll_no
name
branch
Here is my data :
1 7005 Ravi CE
2 7006 Parth CE
3 7007 Yashpal IT
4 7008 Sukunj IT
I want to display data in dropdown with following format
Ravi-7005(CE)
Parth-7006(CE)
Yashpal-7007(IT)
Sukunj-7008(IT) using CHtml::listData
Here is my view file code
<?php
echo CHtml::dropDownList('stud_id', '',CHtml::listData(Student::model()->findAll(), 'id', 'concate')
); ?>
Here is my model file’s concate method
public function getConcate()
{
return $this->name."-".$this->roll_no."(".$this->branch.")";
// or whatever format you want to display
}
By create this type of method you can concate any type of string that you need.
Here is the output.
fburhan89
(Sefburhan)
April 25, 2013, 7:07am
2
I had the same problem, which solve by create a function (return the concatenated list) USING sql and pass to array Which fine and works for like in function:
$connection=Yii::app()->db;
$sql=‘Your query’;
$command=$connection->createCommand($sql);
$rows=$command->queryAll();
$aAry = array();
foreach($rows as $row){
$aAry [$row['id']] = $row['name1'].' | '.$row['name2'].' | '.$row['name3'];
}
.
.
.
in view…
and list echo $form->dropDownListRow($model, ‘myfield’,$$aAry,array(‘class’=>‘span7’));
this is works for
Ravi005
(Bhalodiyaravi)
April 25, 2013, 7:15am
3
fburhan89:
I had the same problem, which solve by create a function (return the concatenated list) USING sql and pass to array Which fine and works for like in function:
$connection=Yii::app()->db;
$sql=‘Your query’;
$command=$connection->createCommand($sql);
$rows=$command->queryAll();
$aAry = array();
foreach($rows as $row){
$aAry [$row['id']] = $row['name1'].' | '.$row['name2'].' | '.$row['name3'];
}
.
.
.
in view…
and list echo $form->dropDownListRow($model, ‘myfield’,$$aAry,array(‘class’=>‘span7’));
this is works for
Thankx for code…
But you need to require loop for making your required string/array. In my case this is not required.
jacmoe
(Jacob Moen)
April 25, 2013, 11:30am
4
[color="#006400 "] /* moved from Feature Requests */ [/color]