[SOLVED]List box with multiple selection

I want to do multiple selection from a list box and store it in db

my present form code is




    


<div class="row">

		<?php echo $form->labelEx($model,'skillid'); ?>

		<?php echo $form->ListBox($model,'skillid',array('id'=>'Select Skill')); ?>

		<?php echo $form->error($model,'skillid'); ?>

	</div>

for this code i am able to select only one field.

Can someone pls help

<?php echo $form->ListBox($model,‘skillid’,array(‘id’=>‘Select Skill’, ‘multiple’ => ‘multiple’)); ?>

No that wont work. Because i am passing that array just to prompt a message before the data is been loaded

so the output will be like "Select a Skill multiple" inside the list box

Actually the data to this dropdown box is loaded using another action inside the controller

here is my controller code


public function actionDynamicSkill()

    {    

        $industryid1 = $_POST['PsmsEmpInfo']['industryid'];

		$data=PsmsSkillsMaster::model()->findAll('t.industryid=:industryid',array(':industryid'=> $industryid1));

		


        $data=CHtml::listData($data,'skillid','skilldesc');

        foreach($data as $id=>$value)  {

			

           echo CHtml::tag('option', array('value'=>$id),CHtml::encode($value),true);

        }

		

and my form code where i will be getting it as a array


<div class="row">

		<?php echo $form->labelEx($model,'skillid'); ?>

		<?php echo $form->ListBox($model,'skillid',array('id'=>'Select a Skill')); ?>

		<?php echo $form->error($model,'skillid'); ?>

	</div>

so where exactly can i add the array(‘multiple=>multiple’) to make the dropdown box multiple select.

take a look on the code of this extension I made

I do something like that in

http://www.yiiframework.com/extension/translate/

Hope this helps, any doubt you have just ask

Sorry but i was not able to understand the drop-down usage here…

i think here u give the values for drop-down


'translate'=>array(//if you name your component something else change TranslateModule

            'class'=>'translate.components.MPTranslate',

            //any avaliable options here

            'acceptedLanguages'=>array(

                  'en'=>'English',

                  'pt'=>'Portugues',

                  'es'=>'Español'

 

and you receive like this


$translate=Yii::app()->translate;

//in your layout add

echo $translate->dropdown();



but i am not able to understand how will i use it for my purpose… looks very complicated

it will be much helpful if you could check my code above and give a solution for using


array(multiple=>multiple)

i don know very exactly should i use this.

Check the documentation - http://www.yiiframew…eListBox-detail

you need to pass the 4th parameter…

So Ace almost got it…




<?php 

echo $form->ListBox($model,'skillid',array('id'=>'Select Skill'), array('multiple' => 'multiple'));

?>



Thanx a lot.It’s working now

Now i have a problem with saving it in db. the field is not getting update in db…

it’s giving me a errors

1.mb_strlen() expects parameter 1 to be string, array given

2.mb_strlen(array("9", "10"), "UTF-8")

Can someone pls tell me how to solve this??

The error is clear… you have a field of type "string" but are trying to save an array (the result of the multiple select)…

So you need to decide how to save those data…

one solution would be to serialize the array (http://php.net/manual/en/function.serialize.php) or convert it manually to a comma separated values like "9,10"

and of course on read you need to reverse this…

If you have multiple select box you must to save an array.

Try saving it serialized, you can add to the model something like:


public function beforeSave()

{

    $this->skillid= serialize($this->skillid);

    retrurn parent::beforeSave();

}


public function afterFind()

{

    $this->skillid= unserialize($this->skillid);

    retrurn parent::afterFind();

}

You call mb_strlen() with first parameter as array which the error message tells you but it expects a string.

You have to loop over the array and then call mb_strlen foreach array element. Notice the & in front of $value in the foreach loop.

PHP: foreach (first code example)




$arr = array("9","10");

foreach($arr as &$value)

{

 $value = mb_strlen($value, "UTF-8");

}



How can it be manually converted like that??

I want it like that only in db.

i.e separated by comma

Sorry but I have to use this code inside model or controller??

Sorry i din understand where exactly i should do this step.

You should add this code in the model:




public function beforeSave()

{

    $this->skillid= serialize($this->skillid);

    retrurn parent::beforeSave();

}


public function afterFind()

{

    $this->skillid= unserialize($this->skillid);

    retrurn parent::afterFind();

}



correct "retrurn ", it should be "return"

:o :D i Corrected it…

But still not working… :-[

giving error

1.mb_strlen() expects parameter 1 to be string, array given

2.mb_strlen(array("9", "10"), "UTF-8")

In your controller replace the call to


mb_strlen(array("9","10"),"UTF-8");

with my foreach loop i suggested.

[size="2"]Again, you could not pass an array to mb_strlen as first parameter.[/size]

For testing purpose you could also simply comment the call to mb_strlen out to test if you have error’s somewhere else.