Save / Update Checkboxlist() Values In One Column

I have a two models, facilities and hotels. I want when creating a hotel i can display list of checkboxes where i can then select the facilities for that hotel, save in hotels table column facilities in this format 1,3,5,7,8,10,…

I also want when i am editing or updating to be able to view the previously selected facilities checked.

I appreciate you all in advance. I have gone through most of the forum posting related to this topic but none does it for me.

Regards

Dear Friend

Before saving it we can convert the array to string to store it in the database.

Before displaying it, we can convert the attribute value again into array.

This way when validation fails or during update the checkbox list or listBox with multiple selection is nicely updated.

One Example…




public function actionCreate()

        {

                $model=new Habit;


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

                {

                        $model->attributes=$_POST['Habit'];

                        if($model->habits!=='')

                                $model->habits=implode(',',$model->habits);//converting to string...

                        if($model->save())

                                $this->redirect(array('view','id'=>$model->id));

                }

               $model->habits=explode(',',$model->habits);//converting to array...

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

                        'model'=>$model,

                ));

        }


public function actionUpdate($id)

        {

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


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

                {

                        $model->attributes=$_POST['Habit'];

                        if($model->habits!=='')

                                $model->habits=implode(',',$model->habits);

                        if($model->save())

                                $this->redirect(array('view','id'=>$model->id));

                }

        $model->habits=explode(',',$model->habits);

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

                        'model'=>$model,

                ));

        }



Regards.

Hi thanks for your post.

I have done exactly as you have indicated above but it throws an error when i click to save. ( implode() [<a href=‘function.implode’>function.implode</a>]: Invalid arguments passed)

Here is the _form code that generates the check boxes.




 <tr>

    <td><?php echo $form->labelEx($model,'facilities'); ?></td>

    <td><?php $facilities = HotelFacilities::model()->findAll();

	          $list = CHtml::listData($facilities, 'id','name');

			  echo $form->checkBoxList($model, 'facilities', $list );  ?>

		<?php echo $form->error($model,'$facilities'); ?></td>

  </tr>

Dear Friend

I have 2 models:

1.Hotel: id,name,facilities.

2.Facility:id,name.

_form.php




<div class="form">


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

	'id'=>'hotel-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,'name'); ?>

		<?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>64)); ?>

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

	</div>


	<div class="row">

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

		<?php echo $form->checkBoxList($model,'facilities',CHtml::listData(Facility::model()->findAll(),'id','name')); ?>

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

	</div>


	<div class="row buttons">

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

	</div>


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



HotelController.php




public function actionCreate()

{

	$model=new Hotel;


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

	{

		$model->attributes=$_POST['Hotel'];


		if(isset($model->facilities) && $model->facilities!==array())

			$model->facilities=implode(',',$model->facilities);


		if($model->save())

			$this->redirect(array('view','id'=>$model->id));

	}


	if(isset($model->facilities) && $model->facilities!=='')

		$model->facilities=explode(',',$model->facilities);


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

		'model'=>$model,

		));

}


	

public function actionUpdate($id)

{

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


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

	{  	 

		$model->attributes=$_POST['Hotel'];

		if(isset($model->facilities) && $model->facilities!==array())

			$model->facilities=implode(',',$model->facilities);

		if($model->save())

			$this->redirect(array('view','id'=>$model->id));

	}


        if(isset($model->facilities) && $model->facilities!=='')

		$model->facilities=explode(',',$model->facilities);


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

			'model'=>$model,

		));

}



Still shows this error

implode() [<a href=‘function.implode’>function.implode</a>]: Invalid arguments passed

After days of pain i finally figured out how to save comma separated checkboxlist values into the database.

Say you have two models hotels and facilities. In the hotels controller just do the following




public function actionCreate()

{

        $model=new Hotel;


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

        {

          $model->attributes=$_POST['Hotel'];


         if(isset($_POST['Hotel']['facilities']) && $_POST['Hotel']['facilities']!==array())

                        $model->facilities=implode(',',$_POST['Hotel']['facilities']);


                if($model->save())

                        $this->redirect(array('view','id'=>$model->id));

        }


    

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

                'model'=>$model,

                ));

}



It works like charm. I am even feeling jealous of me self. ;D Happy coding

Thanks for your sharing, you really help me kick out the same pain

Hi,

please see this link it’s may be helpful.

http://code.dimilow.com/retrieving-selected-checkbox-items-in-yii/

Bro, how you sort the table for facilities, different facilities field with boolean input?

Is there a way to save as name in facilities instead of numbers?