khanz82
(Anthony)
April 15, 2013, 4:19pm
1
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
seenivasan
(Chellamnivas)
April 15, 2013, 5:07pm
2
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.
khanz82
(Anthony)
April 16, 2013, 5:54am
3
seenivasan:
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>
seenivasan
(Chellamnivas)
April 16, 2013, 7:16am
4
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,
));
}
khanz82
(Anthony)
April 20, 2013, 1:10pm
5
seenivasan:
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
khanz82
(Anthony)
May 7, 2013, 5:15pm
6
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. Happy coding
sasasa
(Sasa Syalalala)
July 4, 2013, 3:00am
7
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. Happy coding
Thanks for your sharing, you really help me kick out the same pain
fatt9216
(fatt9216)
February 27, 2021, 5:12am
9
Bro, how you sort the table for facilities, different facilities field with boolean input?
fatt9216
(fatt9216)
March 1, 2021, 2:33am
10
khanz82:
1,3,5,7,8,10,…
Is there a way to save as name in facilities instead of numbers?