Getting Tab Id In New Record

Hi guys, in my application i’m going to use a form in CJuiTabs, where i want to have the id for each tab posted to the new record as well. How can i do this?

My current code for it is:

View:


<?php 

$tab_list=Platform::getPlatforms();

$tabarray=array();

 

// Create Dynamic Tabs

foreach($tab_list as $key=>$value){

	$tabarray["$value"]=array(

		'id'=>$key,

		'content'=>$this->renderPartial('../ranking/_form',

			array('model'=>$ranking),TRUE)

	);

}?>


<?php

$this->widget('zii.widgets.jui.CJuiTabs',array(

    'tabs'=>$tabarray,

    'options'=>array(

        'collapsible'=>true,

    ),

    'id'=>'categorytabs',

)); ?>

Platform model:


class Platform extends CActiveRecord

{

	const PC = 1;

	const Mac = 2;

	const XBOX = 3;

	const XBOX360 = 4;

	const PS2 = 5;

	const PS3 = 6;

	const PSP = 7;

	const PSVITA = 8;

	const Wii = 9;

	const WiiU = 10;

	const NintendoDS = 11;

	const NintendoDS3 = 12;

...


        public function getPlatforms()

	{

		$id = Yii::app()->request->getQuery('id');

		$platform = Platform::model()->findByPk($id);

		$platforms = array();

		if ($platform -> pc == 1)

		{

			$platforms[self::PC] = "PC";

		}

		if ($platform -> xbox == 1)

		{

			$platforms[self::XBOX] = 'XBOX';

		}

		if ($platform -> xbox360 == 1)

		{

			$platforms[self::XBOX360] = "XBOX 360";

		}

		if ($platform -> ps2 == 1)

		{

			$platforms[self::PS2] = "PS2";

		}

		if ($platform -> ps3 == 1)

		{

			$platforms[self::PS3] = 'PS3';

		}

		if ($platform -> psp == 1)

		{

			$platforms[self::PSP] = "PSP";

		}

		if ($platform -> psVita == 1)

		{

			$platforms[self::PSVITA] = 'PS VITA';

		}

		if ($platform -> wii == 1)

		{

			$platforms[self::Wii] = "Wii";

		}

		if ($platform -> wiiU == 1)

		{

			$platforms[self::WiiU] = "Wii U";

		}

		if ($platform -> nintendoDS == 1)

		{

			$platforms[self::NintendoDS] = 'Nintendo DS';

		}

		if ($platform -> nintendoDS3 == 1)

		{

			$platforms[self::NintendoDS3] = 'Nintendo DS3';

		}		

		return $platforms;

	}

}



_form:


<?php

?>


<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'ranking-form',

	'enableAjaxValidation'=>false,

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary($model); ?>


	<div class="row">

		<?php echo $form->labelEx($model,'overall'); ?>

		<?php $this->widget('CStarRating',array(

			'model'=>$model,

			'attribute' => 'overall',

		)); ?>

		<?php echo $form->error($model,'overall'); ?>

	</div>

	<br/>




	<div class="row">

		<?php echo $form->labelEx($model,'graphics'); ?>

		<?php $this->widget('CStarRating',array(

			'model'=>$model,

			'attribute' => 'graphics',

		)); ?>

		<?php echo $form->error($model,'graphics'); ?>

	</div>

	<br/>

	

	<div class="row">

		<?php echo $form->labelEx($model,'sound'); ?>

		<?php $this->widget('CStarRating',array(

			'model'=>$model,

			'attribute' => 'sound',

		)); ?>

		<?php echo $form->error($model,'sound'); ?>

	</div>

	<br/>


	<div class="row">

		<?php echo $form->labelEx($model,'gameplay'); ?>

		<?php $this->widget('CStarRating',array(

			'model'=>$model,

			'attribute' => 'gameplay',

		)); ?>

		<?php echo $form->error($model,'gameplay'); ?>

	</div>

	<br/>

	

	<div class="row">

		<?php echo $form->labelEx($model,'lastingApp'); ?>

		<?php $this->widget('CStarRating',array(

			'model'=>$model,

			'attribute' => 'lastingApp',

		)); ?>

		<?php echo $form->error($model,'gameplay'); ?>

	</div>

	<br/>


	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

	</div>

	

<?php $this->endWidget(); ?>


</div>

An idea could be to do something with the $key from the tabArray, like using it in before validate in the Ranking model. But i do not know how to do that

Dear Friend

I could not test the code in my localhost.

Would you please check the following?

1.Pass the value to view file.




foreach($tab_list as $key=>$value){

        $tabarray["$value"]=array(

                'id'=>$key,

                'content'=>$this->renderPartial('../ranking/_form',

                        array('model'=>$ranking,'id'=>$key),TRUE)

        );

}



2.Post the value during form submission.




<!--Other Form Elements-->

<div class="row buttons">

                <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save',array(

                      'submit'=>"",// or declare the url like array("ranking/create")

                       "params"=>array("id"=>$id)

                       )); ?>

        </div>




Regards.

Thank you for answering, but it didn’t work.

Any other suggestions?

Slightly different to way of achieving seenivasan’s reply …

Pass the $key into the view and include that as a hidden field?





foreach($tab_list as $key=>$value){

        $tabarray["$value"]=array(

                'id'=>$key,

                'content'=>$this->renderPartial('../ranking/_form',

                        array('model'=>$ranking, 'key'=>$key),TRUE)

        );

}




view file:





div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

        'id'=>'ranking-form',

        'enableAjaxValidation'=>false,

)); ?>


        <p class="note">Fields with <span class="required">*</span> are required.</p>


        <?php echo CHtml::hiddenField('tabKey', $key); ?>


        <?php echo $form->errorSummary($model); ?>


..../




It kind of worked. I tweaked it so the code now looks like:


<?php echo $form->hiddenField($model, 'tab_id', array('value' => $key)); ?>

What should i do if i want to give the user the ability to make one new record for every platform that belongs to the game, and at the same time be able to edit existing records? It should work like, if i go to a tab that i haven’t submitted a record to yet, then it should be a new record, but if i go to a tab that i already submitted a record to then it should show me the values i submitted, and allow me to update them.

I guess it should be done by comparing the user_id, the game_id and the platform_id to existing records to see if there already exists one that matches all 3, and probably doing so by using AJAX. I do however not know how to do that.

Can anybody help me with that?