Problem with htmloptions in dropdownlist Yii1.1

138 Views Asked by At

I use Yii 1.1 and I have a question for the dropdownlist. I try to set an extra attribute besides the classic value in the options tags.

The data are coming with queries from the database.

The problem is that I manage to set the extra attribute but it seems that the extra attribute for the first options tag is not displayed.

enter image description here

Here is my code in the view part:

        $types_list = CHtml::listData($model->getTypesnewCodes(),'id','description');
                    
                    
                 $flg = $model->getTypesnewCodesflg();
                    
                 $htmlOptions = array(
                        'prompt' => ("- Select an option -"),
                        'options' =>  $flg,
                    );
            
           echo $form->labelEx($model, 'TypeId');
           echo $form->dropDownList($model,'TypeId',$types_list, $htmlOptions);

Here is my code in the model:

    public function getTypesnewCodesflg() {
            $array =array();
            $typesflg = Yii::app()->db->createCommand()
            ->select( 'id,flag')
            ->from('Types')
            ->queryAll();
        
           
            array_push($array, $typesflg );
    
            
            return $array[0];
        }
1

There are 1 best solutions below

0
Neo On BEST ANSWER

The problem was in the database. I had a table with five records. The value of the id's keys were, from 1 to 5 and when I changed the id's keys value from 0 to 4 it worked.

So be careful with the declaration of the id's!

I really hope that I helped someone!