2 checkboxlist and 1 model

Hi!

I need to create some rows in a model based on user’s selection in 2 checkboxlists. I have a model “atributos” who stores a list of products attributes like size or color.

Example:

id------type------value

1-------color------red

2-------color------blue

3-------color------green

4-------size------S

4-------size------M

4-------size------XL

In view I have a separated checkbox for each type, users select colors and sizes avaliable for this product.


	<div class="row">		

		<?php echo $form->checkBoxList($atributos, 'id_atributo', CHtml::listData(Atributos::model()-> findAllByAttributes(array('tipo'=>'color')), 'id_atributo','valor'))?>

		<?php echo $form->error($atributos,'id_atributo'); ?>

	</div>


	<h3>Tallas</h3>


	<div class="row">		

		<?php echo $form->checkBoxList($atributos, 'id_atributo', CHtml::listData(Atributos::model()-> findAllByAttributes(array('tipo'=>'talla')), 'id_atributo','valor'))?>

		<?php echo $form->error($atributos,'id_atributo'); ?>

	</div>

My problem is: In controller I only recibe values of the last checkboxlist.


print_r($_POST['Atributos']);

I think is because two checklists has the same name, but I don’t know how solve this…

Sorry for my english and thanks!

The problem is that you are assigning the value to the same attribute. So the first one is clobbered by the second one.

Try




<?php echo CHtml::checkBoxList('array1', CHtml::listData(Atributos::model()-> findAllByAttributes(array('tipo'=>'talla')), 'id_atributo','valor'))?>


<?php echo CHtml::checkBoxList('array2', CHtml::listData(Atributos::model()-> findAllByAttributes(array('tipo'=>'talla')), 'id_atributo','valor'))?>


                

After submit, you will have to process the results in the controller.

Example

In my example a value is stored in a field called visible, an event can be displayed on numerous sites but I want an easy way to store all of these sites in one field.

Each site has a value that represents one bit in a binary number so sites have values like this.

Site 1 = 1

Site 2 = 2

Site 3 = 4

Site 4 = 8

etc,

Then to check I use (example site 3) If(visible & 4) if true we can display it.

The contents of visible could be two sites (example site 1 and site 3) so it equals 1 + 4.

Therfore 5 & 1 = true but 5 & 2 = false.




   <?php

	 $sites = Websites::model()->findAll();

	 $sitesNow = $this->getSitesDisplayed($model->visible);

         

     // format models resulting using listData

     $siteslist = CHtml::listData($sites,

                'display','entry_url');

     ?>

    <div class="generic_box">

        <h3>Select which sites this event will be displayed on</h3>

        <h5>This event was submitted on <?php echo Websites::model()->findByPk($model->submission_site)->entry_url ?></h5>

		<?php echo CHtml::checkBoxList('sites',

                                                $sitesNow,

		                                $siteslist,

                                                array(

                                                'checkAll'=>'Make it visible on all sites',

                                                'checkAllLast'=>true,

                                                'class'=>'oneline',

                                                'labelOptions'=>array('class'=>'oneline'),

                                                )

		                                );

		 ?>

    </div>



In controller


public function actionApprove($id)

	{

		$model=$this->loadModel($id);


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


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

		{

                    $visible = 0; // start fresh


//  This is the post array  Array ( [sites] => Array ( [0] => 1 [1] => 2 [2] => 4 ) [sites_all] => 1 [yt0] => Save )


                    foreach($_POST['sites'] as $site)

                    {

                        $visible += $site;

                    }

			$model->visible = $visible;

			if($model->save())

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

		}


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

			'model'=>$model,

		));

	}

doodle