Collecting Tabular Input

Hi Experts,

I’m trying to store tabular input values in my model. I’m using only one model. I need to store in my model. but only last record is getting changed not all the records

can any one pls tell me why its happening only to last record. I’m also getting same page twice in my batchupdate.php file

my code is like this:

controller:


public function actionBatchUpdate()

	{

		$item = new Model;

			

		$items=Model::model()->findAll();

		

		

		

		


		if(isset($_POST['Model']))

		{

			$valid=true;

			foreach($items as $i=>$item)

			{

				if(isset($_POST['Model'][$i]))

				$item->attributes=$_POST['Model'][$i];

				$valid=$item->validate() && $valid;

			}

			//print_r($item);die;

			if($valid){

			if($item->save())$this->render('batchUpdate',array(

				'items'=>$items));

			}

				

		}


		// displays the view to collect tabular input

		$this->render('batchUpdate',array(

				'items'=>$items));

		

	}

my view file batchupdate.php is:




	


<div class="form">

<?php echo CHtml::beginForm(); ?>

<table>


<?php foreach($items as $i=>$item): ?>

<tr style=" border-bottom: solid 1px black;">

<p><td><?php echo CHtml::activeTextField($item,"[$i]name",$htmlOptions=array('disabled'=>'disabled')); ?></td></p>

</tr>




<tr style=" border-bottom: solid 2px black;">

<p><td><?php echo CHtml::activeTextArea($item,"[$i]text",$htmlOptions=array ('rows'=>8,'cols'=>60));?></td></p></tr> 


 <tr style=" border-bottom: solid 1px black;">

<p><td><?php echo CHtml::submitButton('Save');?></td></p></tr>

<tr>

<td><hr size="10" width="100%" /></td>

</tr>


<?php endforeach; ?>

</table>

 


<?php echo CHtml::endForm(); ?>

</div><!-- form -->

Thanks in advance

got it guys…I’m rendering twice here…

New code…saving the record in the loop itself now…new code working. Its like this:


public function actionBatchUpdate()

        {

                $item = new Model;

                        

                $items=Model::model()->findAll();

                

                

                

                


                if(isset($_POST['Model']))

                {

                      

			$valid=true;

			foreach($items as $i=>$item)

			{

				if(isset($_POST['Model'][$i]))

					

				$item->attributes=$_POST['Model'][$i];

				$valid=$item->validate() && $valid;

				if($valid)$item->save();

			}

                        

                                

                }


                // displays the view to collect tabular input

                $this->render('batchUpdate',array(

                                'items'=>$items));

                

        }

I want to keep track of more than one in 1 table. But the incoming data is only the first data alone with the following code. Could help me.

View:


<?php 

			$siswa=Siswa::model()->findAll();

			foreach ($siswa as $i=>$ii): 

		?>

		<?php echo $form->textFieldRow($model,"[$i]kode_mapel",array('class'=>'span2','maxlength'=>6)); ?>

		<?php echo $form->textFieldRow($model,"[$i]kode_guru", array('class'=>'span2', 'maxlength'=>2)); ?>

	

		<?php echo $form->hiddenField($model,"[$i]lokal",array('value'=>$ii['lokal'])); ?>

		<?php echo $form->hiddenField($model,"[$i]kelas",array('value'=>$ii['kelas'])); ?>

		<?php echo $form->hiddenField($model,"[$i]nisn",array('value'=>$ii['nisn'])); ?>

			<tr>

				<td><?php echo $ii['nisn']; ?></td>

				<td><?php echo $ii['nama_lengkap']; ?></td>

				<td><?php echo $form->textFieldRow($model,"[$i]na", array('class'=>'input-small', 'id'=>'na')); ?></td>

				<td><?php echo $form->textFieldRow($model,"[$i]un", array('class'=>'input-small', 'id'=>'un')); ?></td>

				<td><?php echo $form->textFieldRow($model,"[$i]uas", array('class'=>'input-small', 'id'=>'uas')); ?></td>

			</tr>

		<?php endforeach; ?>

Controller:


public function actionCreate()

	{

		$model=new Nilai;

		if(isset($_POST['Nilai']))

		{

			$valid=true;

			foreach ($_POST['Nilai'] as $i=>$ii){

				if(isset($_POST['Nilai'][$i])){

					$model=new Nilai;

					$model->attributes=$_POST['Nilai'][$i];

					$valid=$model->validate();

					$valid2=$model->validate() && $valid;

					if($valid2){

						$model->save();

						var_dump($model->errors);

					}

					$this->redirect(array('nilai/admin'));

				}

			}

			

		}

$this->render('create',array(

			'model'=>$model,

		));

	}