[SOLVED]listbox multi select

Hi, I created a listbox but it doesn’t allow me to select multiple from the options provided inside the box by pressing “CTRL” + “clicking the desired options”

at the model i created constants




        const MARKETING_CAT = 1;

        const SOFTWARE_CAT = 2;

        const INTERNET_CAT = 3;

        const ONLINETOOLS_CAT = 4;

        const BACKUP_CAT = 5;

        const SECURITY_CAT = 6;

        const COMMUNICATION_CAT = 7;

        const HARDWARE_CAT = 8;



then a function like this




        public function getCatOptions()

        {

            return array(

                self::MARKETING_CAT => 'Marketing',

                self::SOFTWARE_CAT => 'Software',

                self::INTERNET_CAT => 'Internet',

                self::ONLINETOOLS_CAT => 'Online Tools',

                self::BACKUP_CAT => 'Backup',

                self::SECURITY_CAT => 'Security',

                self::COMMUNICATION_CAT => 'Communication',

                self::HARDWARE_CAT => 'Hardware',

            );

        }



then at the _form view file, I created it like this




        <div class="row">

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

		<?php echo $form->listBox($model,'Category',$model->getCatOptions()); ?>

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

	</div>



what should I do in order for it to allow me to select multiple options ? ???

to select multiple U have to supply the ‘multiple’ htmloption:




        <div class="row">

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

		<?php echo $form->listBox($model,'Category',$model->getCatOptions(),array('multiple'=>'multiple')); ?>

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

	</div>



i have the main problem now,

that multiselect list box is located at the top my form for creating a product

so that the user can choose one or more categories for the certain product to be created,

how will I fetch the value/values of one or multiselected options from the listbox and get it saved to it’s corresponding table ?

my products table has these columns




ProductID, ProductDescription



my category_products table has these columns




catid, ProductID



this table above should save the integer value of the selected option from the lisbox

my create action has this code




	public function actionCreate()

	{

		$model=new Wswebproducts;


		// Uncomment the following line if AJAX validation is needed

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


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

		{

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

			if($model->save())

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

		}


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

			'model'=>$model,

		));

	}



how to get the multi selected values from my form and save it along with the product ?

if I created 1 product and it has catid 1,2,3,4,5 …how will i do that?

Here is how to do it:




//In the _form view

echo CHtml::activeDropDownList($modelCat, 'catid', $categoryListArray(),

		array('size'=>7,'multiple'=>'multiple','style'=>'width: 205px'));


//In the create view

echo $this->renderPartial('_form', array('model'=>$model,'modelCat'=>$modelCat));




public function actionCreate()

{

		$model=new Wswebproducts;

		$modelCat = new CategoryProducts;


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

		{

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

				if($model->save()){

					$productId = $model->ProductID;

					

					if(isset($_POST['CategoryProducts']['catid'])){

						  $arr = $_POST['CategoryProducts']['catid'];

						  if(count($arr)>0){

							  foreach($arr as $catID){

								  $modelCat=new CategoryProducts;

								  $modelCat->ProductID = $productId;

								  $modelCat->catid = $catID;

								  $modelCat->save();

							  }

						  }

					  }

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

				}

		}


		$this->render('create',array('model'=>$model, 'modelCat'=>$modelCat));

}

thanks for this…there’s a slight problem with constraint violation though.

but it got solved when i dropped the constraint and primary key…thanks alot :)

hope this helps others when they encountered same problem ahaha

glad it solved your problem. its like “a little progress everyday adds up to a big result” :)

good work

good work this helped me a lot!