List Box Selection On Update

View - Create



<div class="row">

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

		<?php

          $data = array('1' => 'Accounting', '2' => 'Admin-Clerical', '3' => 'Automotive', '4' => 'Banking', '5' => 'Biotech', '6' => 'Business Development');


	$htmlOptions = array('size' => '5', 'prompt'=>'Use CTRL to Select Multiple Staff', 'multiple' => 'multiple', 'options' => $selected); 


          echo $form->ListBox($model,'category', $data, $htmlOptions); 

    ?>

Controller



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

			$model->category =  serialize($model->category);

			if($model->save())

			{


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

			}

		}

Please how this list box value to be selected when update this this form


 print_r(unserialize($model->category));

I can get the db values




				 $selected   = array(

				  '1' => array('selected' => 'selected'),

				  '2' => array('selected' => 'selected'),

				);

				 print_r( $selected);

				 exit;


			  $htmlOptions = array('size' => '5', 'prompt'=>'Use CTRL to Select Multiple Staff', 'multiple' => 'multiple', 'options' => $selected); 

This is static meathod to show selected values, How it is possible in dynamic meathod?

Please help.

Dear Balu

Kindly check the following.

Before saving it 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.




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

thank you for your replay

But I have solved in another way


<div class="row">

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

		<?php

          $data = array('1' => 'Accounting', '2' => 'Admin-Clerical', '3' => 'Automotive', '4' => 'Banking', '5' => 'Biotech', '6' => 'Business Development');

		  

			  foreach(unserialize($model->category) as $p)

				 {

					$pp[$p] = array('selected'=>'selected');

				}

			  $htmlOptions = array('size' => '5', 'prompt'=>'Use CTRL to Select Multiple Staff', 'multiple' => 'multiple', 'options' =>$pp); 


          echo $form->ListBox($model,'category', $data, $htmlOptions); 

    ?>

Cheers!!!