Yii: Fixing Array Values

I’m really newbie in using yii framework … so i need help …

i have this code in my _form.php which is a checkboxlist




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

		<td><?php echo $form->checkBoxList($model,'visit_purpose',

        array(

              'ASQ administration'=>'ASQ administration',

              'IFSP'=>'IFSP'),

        array('labelOptions'=>array('style'=>'display:inline'),'separator'=>" | ")); ?></td>

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



and this one in my model.php … i got this code in one of the threads here in forum




public $purposeIds;


    public function afterFind()

    {

        parent::afterFind();

        

        $this->purposeIds = CJSON::decode($this->visit_purpose);

        $this->purposeIds = CJSON::decode($this->presents);

        $this->purposeIds = CJSON::decode($this->concern_for_cg);

        $this->purposeIds = CJSON::decode($this->pb_physical_condition_concern);

        

        if (!is_array($this->visit_purpose) || !is_array($this->presents) || !is_array($this->concern_for_cg) || !is_array($this->pb_physical_condition_concern))

        {

            $this->purposeIds[] = array('selected'=>'selected');

        }


        $this->purposeIds = array();

        

        if (sizeof($this->purposeIds)){

            foreach ($this->visit_purpose as $visit_purpose){

                $this->purposeIds[]=array();

            }

            }

        if (sizeof($this->purposeIds)){

            foreach ($this->presents as $presents){

                $this->purposeIds[]=array();

            }

        }

        if (sizeof($this->purposeIds)){

            foreach ($this->concern_for_cg as $concern_for_cg){

                $this->purposeIds[]=array();

            }

        }

        if (sizeof($this->purposeIds)){

            foreach ($this->pb_physical_condition_concern as $pb_physical_condition_concern){

                $this->purposeIds[]=array();

            }

        }


    }

       protected function beforeValidate()

       

        {

        if (!is_array($this->visit_purpose)){

            $this->purposeIds = array();

        }

        if (!is_array($this->presents)){

            $this->purposeIds = array();

        }

        if (!is_array($this->concern_for_cg)){

            $this->purposeIds = array();

        }

        if (!is_array($this->pb_physical_condition_concern)){

            $this->purposeIds = array();

        }

        

        $this->visit_purpose = CJSON::encode($this->visit_purpose);

        $this->presents = CJSON::encode($this->presents);

        $this->concern_for_cg = CJSON::encode($this->concern_for_cg);

        $this->pb_physical_condition_concern = CJSON::encode($this->pb_physical_condition_concern);


        return parent::beforeValidate();

    }



it saves in the database like it should but the output is like this … [“ASQ administration”,“IFSP”] … well obviously … i need to remove [""] and let it be a comma separated array … how can i fix this … is there something wrong with the code i’m using … if yes … please do help me the proper way in saving checkboxlist array :D thank you so very much

Dear Friend

Kindly check whether the following is helpful.

Before saving the checkbox value, I converted it to string.

Before displaying it, I converted it again to array.

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

Consider we have a model Habit.

The property habits have checkBoxList .




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.

thanks a bunch it helped me a lot … but “$model->habits=explode(’,’,$model->habits);” << that part is giving me error … so is it okay if i remove it since the one in the if condition solved my problem … or are there other functions that will affect if i remove it?

Dear Friend

What exactly the problem you are facing?

Since explode function converts the string form in the data base again into array.

So during update,the corresponding checkboxes would be checked.

That way we are aware of existing selections during update or incase when validation fails during create.

Regards.

never mind it already worked … i do not know what happened … but it’s working now … thank you so much