dynamic rows creation

In Yii Play ground complex dynamic rows

i have foreach invalid error whats the meaning of $persons and $person

and how can i insert into database anybody help me

<div class="complex">

    &lt;span class=&quot;label&quot;&gt;


        &lt;?php echo Yii::t('ui', 'Persons'); ?&gt;


    &lt;/span&gt;


    &lt;div class=&quot;panel&quot;&gt;


        &lt;table class=&quot;templateFrame grid&quot; cellspacing=&quot;0&quot;&gt;


            &lt;thead class=&quot;templateHead&quot;&gt;


                &lt;tr&gt;


                    &lt;td&gt;


                        &lt;?php echo &#036;form-&gt;labelEx(Person::model(),'firstname');?&gt;


                    &lt;/td&gt;


                    &lt;td&gt;


                        &lt;?php echo &#036;form-&gt;labelEx(Person::model(),'lastname');?&gt;


                    &lt;/td&gt;


                    &lt;td colspan=&quot;2&quot;&gt;


                        &lt;?php echo &#036;form-&gt;labelEx(Person::model(),'eyecolor_code');?&gt;


                    &lt;/td&gt;


                &lt;/tr&gt;


            &lt;/thead&gt;


            &lt;tfoot&gt;


                &lt;tr&gt;


                    &lt;td colspan=&quot;4&quot;&gt;


                        &lt;div class=&quot;add&quot;&gt;&lt;?php echo Yii::t('ui','New');?&gt;&lt;/div&gt;


                        &lt;textarea class=&quot;template&quot; rows=&quot;0&quot; cols=&quot;0&quot;&gt;


                            &lt;tr class=&quot;templateContent&quot;&gt;


                                &lt;td&gt;


                                    &lt;?php echo CHtml::textField('Person[{0}][firstname]','',array('style'=&gt;'width:100px')); ?&gt;


                                &lt;/td&gt;


                                &lt;td&gt;


                                    &lt;?php echo CHtml::textField('Person[{0}][lastname]','',array('style'=&gt;'width:100px')); ?&gt;


                                &lt;/td&gt;


                                &lt;td&gt;


                                    &lt;?php echo CHtml::dropDownList('Person[{0}][eyecolor_code]','',Lookup::items('eyecolor'),array('style'=&gt;'width:100px','prompt'=&gt;'')); ?&gt;


                                &lt;/td&gt;


                                &lt;td&gt;


                                    &lt;div class=&quot;remove&quot;&gt;&lt;?php echo Yii::t('ui','Remove');?&gt;&lt;/div&gt;


                                    &lt;input type=&quot;hidden&quot; class=&quot;rowIndex&quot; value=&quot;{0}&quot; /&gt;


                                &lt;/td&gt;


                            &lt;/tr&gt;


                        &lt;/textarea&gt;


                    &lt;/td&gt;


                &lt;/tr&gt;


            &lt;/tfoot&gt;


            &lt;tbody class=&quot;templateTarget&quot;&gt;


            &lt;?php foreach(&#036;persons as &#036;i=&gt;&#036;person): ?&gt;


                &lt;tr class=&quot;templateContent&quot;&gt;


                    &lt;td&gt;


                        &lt;?php echo &#036;form-&gt;textField(&#036;person,&quot;[&#036;i]firstname&quot;,array('style'=&gt;'width:100px')); ?&gt;


                    &lt;/td&gt;


                    &lt;td&gt;


                        &lt;?php echo &#036;form-&gt;textField(&#036;person,&quot;[&#036;i]lastname&quot;,array('style'=&gt;'width:100px')); ?&gt;


                    &lt;/td&gt;


                    &lt;td&gt;


                        &lt;?php echo &#036;form-&gt;dropDownList(&#036;person,&quot;[&#036;i]eyecolor_code&quot;,Lookup::items('eyecolor'),array('style'=&gt;'width:100px','prompt'=&gt;'')); ?&gt;


                    &lt;/td&gt;


                    &lt;td&gt;


                        &lt;div class=&quot;remove&quot;&gt;&lt;?php echo Yii::t('ui','Remove');?&gt;


                        &lt;input type=&quot;hidden&quot; class=&quot;rowIndex&quot; value=&quot;&lt;?php echo &#036;i;?&gt;&quot; /&gt;


                    &lt;/td&gt;


                &lt;/tr&gt;


            &lt;?php endforeach; ?&gt;


            &lt;/tbody&gt;


        &lt;/table&gt;


    &lt;/div&gt;&#60;&#33;--panel--&#62;


&lt;/div&gt;&#60;&#33;--complex--&#62;

Did you find the answer? I have the same problem.

No. did you find the answer

To use a dynamic rows you have to use magic methods, and afterValidate handle that propertys

Hi Guys any one can help me to implement dynamic rows

for dynamic row insert Add this below code in controller under actionCreate public function

Code:

if(isset($_POST[‘Project’]))

{

$items = $_POST[‘Project’];

    foreach(&#036;items as &#036;i=&gt;&#036;item){


&#036;modelProject = new Project;


&#036;modelProject-&gt;attributes = &#036;_POST['Project'][&#036;i];


&#036;modelProject-&gt;save();


}

}

initially actionCreate public function look like this:

public function actionCreate()

{

&#036;model=new Users;





// Uncomment the following line if AJAX validation is needed


// &#036;this-&gt;performAjaxValidation(&#036;model);





if(isset(&#036;_POST['Users']))


{


	&#036;model-&gt;attributes=&#036;_POST['Users'];


	if(&#036;model-&gt;save())


		&#036;this-&gt;redirect(array('view','id'=&gt;&#036;model-&gt;id));


}





&#036;this-&gt;render('create',array(


	'model'=&gt;&#036;model,


));

}

after adding dynamic row insert code:

public function actionCreate()

{

&#036;model=new Users;





// Uncomment the following line if AJAX validation is needed


// &#036;this-&gt;performAjaxValidation(&#036;model);





if(isset(&#036;_POST['Users']))


{


	&#036;model-&gt;attributes=&#036;_POST['Users'];


	if(&#036;model-&gt;save()){


                    if(isset(&#036;_POST['Project']))


                    {


                    &#036;items = &#036;_POST['Project'];


                            foreach(&#036;items as &#036;i=&gt;&#036;item){


                        &#036;modelProject = new Project;


                        &#036;modelProject-&gt;attributes = &#036;_POST['Project'][&#036;i];


                        &#036;modelProject-&gt;save();


                        }


                    }


	&#036;this-&gt;redirect(array('view','id'=&gt;&#036;model-&gt;id));


            }


}





&#036;this-&gt;render('create',array(


	'model'=&gt;&#036;model,


));

}

Please your help.

I have problem with:

<?php foreach($persons as $i=>$person): ?>

Invalid argument supplied for foreach()

C:\AppServ\www\ejemplo\protected\views\requisicionprocesoproexterno\_form.php(95)

do you have any suggestion?

Thanks in advanced for your answer.

Hi,

try this first initialize $persons=array(); and remove $person then add your model name there for example - $Department

I have a problem, and q I can not handle the ajax, I need to do a combo dependent is working but not me … anyone know?

what do you mean when you call project ??

for eg $items = $_POST[‘Project’];