emil_pgc
(Emil Thushara)
June 28, 2011, 1:47pm
1
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
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’)); ?>
emil_pgc
(Emil Thushara)
June 28, 2011, 2:56pm
3
<?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
emil_pgc
(Emil Thushara)
June 28, 2011, 3:43pm
4
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.
gusnips
(Gustavo)
June 28, 2011, 4:30pm
5
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
emil_pgc
(Emil Thushara)
June 29, 2011, 6:22am
6
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.
mdomba
(Maurizio Domba Cerin)
June 29, 2011, 7:54am
7
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'));
?>
emil_pgc
(Emil Thushara)
June 29, 2011, 7:57am
8
mdomba:
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
emil_pgc
(Emil Thushara)
June 30, 2011, 7:01am
9
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??
mdomba
(Maurizio Domba Cerin)
June 30, 2011, 7:10am
10
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…
zaccaria
(Matteo Falsitta)
June 30, 2011, 7:11am
11
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();
}
kokomo
(Mwerlberger85)
June 30, 2011, 7:23am
12
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??
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");
}
emil_pgc
(Emil Thushara)
June 30, 2011, 10:05am
13
mdomba:
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…
How can it be manually converted like that??
I want it like that only in db.
i.e separated by comma
emil_pgc
(Emil Thushara)
June 30, 2011, 10:07am
14
zaccaria:
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();
}
Sorry but I have to use this code inside model or controller??
emil_pgc
(Emil Thushara)
June 30, 2011, 10:18am
15
kokomo:
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");
}
Sorry i din understand where exactly i should do this step.
zaccaria
(Matteo Falsitta)
June 30, 2011, 12:46pm
16
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();
}
gusnips
(Gustavo)
June 30, 2011, 2:40pm
18
correct "retrurn ", it should be "return"
emil_pgc
(Emil Thushara)
June 30, 2011, 3:19pm
19
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")
kokomo
(Mwerlberger85)
July 1, 2011, 7:07am
20
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.