Hi
I wonder if anyone can help with the following question.
In my view I have a dropdown which displays a list of numbers which is populated from my model.
View
<?php
echo $form->dropDownList($model, 'player_total_selector', $model->getPlayercount(),
array(
'class'=>'styled-select',
'prompt'=>'How many people played this week?',
'onchange'=>'myfunction(this.value)',
));
?>
Resulting Options

I would like my user to select a number from the dropdown and then use the selected value to loop inside my view displaying the extra form input fields as required.
Idea?
Would I have some kind of do while loop?
function myfunction(val){
// Some kind of do while loop?
<?php echo CHtml::activeTextField($model,"option[val]"); ?>
// loop ++
}
How do I reload the view to contain the extra input fields matching the users selection?
Any help would be most appreciated
Many thanks
GPM
You don’t need to reload the view, you can do that using JS only.
I mean,
function myfunction(val)
{
for (var i = 0; i < val; i++) {
// create input element
}
}
The tricky part here is input field name and value (in case of validation fail). You can use these functions:
http://www.yiiframework.com/doc/api/1.1/CHtml#activeId-detail
http://www.yiiframework.com/doc/api/1.1/CHtml#activeName-detail
http://www.yiiframework.com/doc/api/1.1/CHtml#activeLabel-detail
Thank you for your help with this question
Based upon your answer I have got a version working which creates the input boxes as expected
// Dropdown - Select number
<?php
echo $form->dropDownList($model, 'player_total_selector', $model->getPlayercount(),
array(
'class'=>'styled-select',
'prompt'=>'How many people played this week?',
'onchange'=>'myfunction(this.value)',
));
?>
// Loop and output the input boxes
<script type="text/javascript">
function myfunction(val)
{
str = '';
for (var i = 0; i < val; i++) {
// create input element works
str += '<input type="text" name="name' + i + '" id="id' + i + '" placeholder="input' + i + '" />';
}
$('#container').html(str);
}
</script>
<div id="container"></div>

Would it be possible to replace the input box with a dropdown list which is populated from my model?
I would like to replace
str += '<input type="text" name="name' + i + '" id="id' + i + '" placeholder="input' + i + '" />';
With my dropdown
<?php
echo $form->dropDownList($model, 'player_selector[i]', $model->getPlayerlist(),
array(
'class'=>'styled-select',
'prompt'=>'Select Player',
));
?>

Is this possible?
Many thanks
GPM
You can either make a hidden select and clone it, or js variable storing select options (json-encoded for example), or something completely different, it’s up to you.
Thank you for all your help
I want to use the hidden select and clone it, I have got it all working but I am unsure how to load up my select from my controller.
So far I have it all working with a dynamic dropdown which when triggered calls the url of my controller and loads up the options.
This all works ok, but is not a good solution
Can I automate this to trigger onload?
Or is there a simpler way to call my controller for the select options?
My View
<?php
echo CHtml::dropDownList('foobar','', array(1=>'option 1',2=>'option 2'),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('dynamicplayers'), //url to call.
'update'=>'#player_id', //selector to update
)));
//empty since it will be filled by the other dropdown
echo CHtml::dropDownList('player_id','', array());
?>
My Controller
public function actionDynamicplayers()
{
$id=(Yii::app()->user->venue_user_id);
// Return all the players associated with the users venue
$sqlPlayers='SELECT * FROM player LEFT JOIN player_venues ON player.id = player_venues.player_id WHERE player_venues.venue_id=:id AND player_venues.active = "yes"';
$data = Yii::app()->db->createCommand($sqlPlayers)->bindValue('id',$id)->queryAll();
$data=CHtml::listData($data,'id','player_forename');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}