Hi
I hope someone could please help me with the following
I have a form which dynamicaly generates any number of dropdown lists ranging from 1-100 depending on the users selection
The dropdowns are not directly part of my active model but the data I get back will be used for creating player records
I have extended my model to include the variable ‘position’ and also added it to safe as follows:
Model
class GameResults extends CActiveRecord
{
// extend to add property into model
public $position; // to be used as the array for player
....
Rules
array('position', 'safe'),
....
In my view I have a hidden dropdown list which has been generated so that I can clone and re-use its options
_form.php - Hidden Select With Options To Be Cloned
<?php echo $form->dropDownList($model, 'wibble', $model->getPlayerlist(), array('class' => 'hidden', 'id' => 'venue_player_list')); ?>
<?php echo $form->hiddenField($model,'played_count',array('id'=>'howmany')); ?>
_form.php - Select A Number
<?php
// This dropdown gets populated from the model by calling getPlayercount() and once selected it passes the value to the function 'myfunction'
echo $form->dropDownList($model, 'player_total_selector', $model->getPlayercount(),
array(
'class'=>'styled-select',
'onchange'=>'myfunction(this.value)',
'id'=>'played_count',
));
?>
_form.php - Script create required number of dropdowns incrementing the array ID
<script type="text/javascript">
// function gets the value of players then loops & clones the hidden options
function myfunction(val)
{
str = '';
$("#howmany").val(val);
for (var i = 1; i <= val; i++) {
str += '<div class="row"><select name="GameResults[position' + i + ']" id="position' + i + '"></select></div>';
}
// Outputs the required number of empty selects to the div with the id #container
$('#container').html(str);
// Clone hidden options and append to the new selects
var $selectContents = $('#venue_player_list option').clone();
$('.listme').append($selectContents);
}
</script>
Everything works as expected
I select a number
The dropdowns appear populated with the cloned content
And if for example I select 3, and make my selections I can see the following ID’s been passed in the browser
Now I am stuck
Was this the correct way to pass an array?
And How do I loop through it in my controller?
For an example how to loop and echo out passed results?
My Controller looks like this so far
public function actionCreate()
{
$transaction=Yii::app()->db->beginTransaction();
$isok = true;
$model=new GameResults;
if (isset($_POST['GameResults'])) {
try {
$model->attributes=$_POST['GameResults'];
$total = $model->played_count; // get the total number of people played for use in the loop counter
for ($i = 1; $i <= $total; $i++){ // initiate the loop
$x = $model->position[$i]; // test output
echo '<pre>'; // TEST
var_dump($x);
echo '</pre>';
}
if ($isok == true) {
$transaction->commit();
$this->redirect(array('view', 'id' => $model->id));
}
else {
$transaction->rollBack();
echo 'something wrong!';
}
}
catch(Exception $e) {
$transaction->rollBack();
}
}
$this->render('create',array(
'model'=>$model,
));
}
I have looked at many examples and read the section on tabular input but I cannot see the simple way to manually craft an array and loop through it in my controller
Any help would be most appreciated
Many thanks
GPM