Dropdown Multiselect Values How To Save

in my application i used two dropdown multiselect option iam using how to save in controller side please help anybody

will get as an array, right?

use serialize() and save.

if need to save as multiple records then set a loop and save.

in my code create controller

if(isset($_POST[‘ExMusclesDetails’]))

	{


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


            $mas= $_POST['targeted_muscles'];


                    foreach ($mas as $data){


                        $model->targeted_muscles=$data;


                        $model->save();


                        


                        


                    }


                    if($model->save())


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


	}

please tell what is wrong and how to add serializ add please tell me

Hello,

If you want to save selected list i mean multiple records you have to use following code in your loop.




$model->setIsNewRecord(true);

$model->id=null;




$mas will be an array, right?

if you want to save it as separate data, then you have to create new model instances




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

{

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

$mas= $_POST['targeted_muscles'];

foreach ($mas as $data){

$model->targeted_muscles=$data;

$model->save();




}



change this to




foreach ($mas as $data){

$model = new ExMusclesDetails;

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

$model->targeted_muscles=$data;

$model->save();




and if you want to save as a single document, then you have to serialize the array $mas

then you can save to db

Iam using dropdown multiselect option ,i try to save in database its show the error

messages for Array to string conversion how to solve please help anybody

iam try to save multiple select option in my controller shows error messages

like this "Array to string conversion" how to solve help anybody

thats what im saying from the first post!! what are you doing?

just serialize the data man…then you can save

serialize(data) is function, you can use unserialize(data) to retun

in my controller part how to use serializer and i show my code please see

create code

public function actionCreate()

{


	$model=new ExMusclesDetails;





	// Uncomment the following line if AJAX validation is needed


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


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

{

$model->attributes=$_POST[‘ExMusclesDetails’];

foreach($_POST[‘ExMusclesDetails’][‘targeted_muscles’] as $value){

$model->targeted_muscles=$value;

$model->additional_muscles;

$model->id;

}

foreach ($_POST[‘ExMusclesDetails’][‘additional_muscles’] as $addition){

$model->additional_muscles=$addition;

}

            $model->save();   


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


	}





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


		'model'=>$model,


	));


} 

how to use serialize please give some steps




foreach($_POST['ExMusclesDetails']['targeted_muscles'] as $value){


$model->targeted_muscles=$value; 

$model->additional_muscles;

$model->id;




}



this is wrong

only last value is assigning to targeted_muscles.

remove this foreach loop


$model->targeted_muscles=serialize($_POST['ExMusclesDetails']['targeted_muscles']); 

and remove second loop and do this too


model->additional_muscles=serialize($_POST['ExMusclesDetails']['additional_muscles']);

this will save

u cant save an array to mysql db!

now in my controller like this ,the problem is at a time only one values save in multiselect

in my code

public function actionCreate()

{


	$model=new ExMusclesDetails;





	// Uncomment the following line if AJAX validation is needed


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


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

{

$model->attributes=$_POST[‘ExMusclesDetails’];

foreach($_POST[‘ExMusclesDetails’][‘targeted_muscles’] as $values){

$model->targeted_muscles =($values);

$model->id;

}

            $model->save();   


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


	}





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


		'model'=>$model,


	));


}

what is my code wrong and how to post retrive in yii framework

Hi murugangm,

You’d better explain your database schema(s) for the model(s) first.

  1. What is the table schema for ExMusclesDetails model?

  2. What relation(s) do you have for ExMusclesDetails model?

  3. Is "targeted_muscles" an column in the table for ExMusclesDetails or is it a HAS_MANY relation?

in my db two tables

1.muscles

2.exmusclesdetails

the exmusclesdetails tables foreign key of muscles tables making i send my models code here

;;;;

exmusclesdetails

 */


public function relations()


{


	// NOTE: you may need to adjust the relation name and the related


	// class name for the relations automatically generated below.


	return array(


		'exname0' => array(self::BELONGS_TO, 'ExNameDetails', 'exname'),


		'targetedMuscles' => array(self::BELONGS_TO, 'Muscles', 'targeted_muscles'),


		'additionalMuscles' => array(self::BELONGS_TO, 'Muscles', 'additional_muscles'),


                    'targetedMuscles' => array(self::HAS_MANY, 'Muscles', 'targeted_muscles'),                    );


}

and my controller code

;;;;;;;;;

public function actionCreate()

{


	$model=new ExMusclesDetails;





	// Uncomment the following line if AJAX validation is needed


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


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

{

$model->attributes=$_POST[‘ExMusclesDetails’];

foreach($_POST[‘ExMusclesDetails’][‘targeted_muscles’] as $values){

$model->targeted_muscles =($values);

$model->id;

}

            $model->save();   


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


	}





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


		'model'=>$model,


	));


}

and what is wrong in my code

my requirement is dropdown multiple select values insert into the db

You can’t declare ‘targetedMuscles’ as BELONGS_TO and as HAS_MANY at the same time.

You have to re-design your database before you proceed in coding.

Probably you have to create a link table and establish a MANY_MANY relation between ExMusclesDetails and Muscles models.

i changed but again i insert a values in dropdown multiselect at a time only one values saved in db what is wrong in my code

in my dropdowncode is

<div class="row">

&lt;b style=&quot; padding-right: 60px;&quot;&gt;Targeted_muscles &lt;span class=&quot;required&quot;&gt;* &lt;/span&gt; &lt;/b&gt;&lt;?php echo &#036;form-&gt;dropdownList(&#036;model,'targeted_muscles[]', CHtml::listData(Muscles::model()-&gt;findAll(array('order' =&gt; 'muscles_name')), 'id', 'muscles_name'), array('empty'=&gt;'Select ' ,'multiple'=&gt;'multiple',)


                      ) 


            


           


            ?&gt; 





	&lt;?php echo &#036;form-&gt;error(&#036;model,'targeted_muscles'); ?&gt;


&lt;/div&gt;

and my controller code is

public function actionCreate()

{


	&#036;model=new ExMusclesDetails;





	// Uncomment the following line if AJAX validation is needed


	// &#036;this-&gt;performAjaxValidation(&#036;model);


	if(isset(&#036;_POST['ExMusclesDetails']))

{

$model->attributes=$_POST[‘ExMusclesDetails’];

foreach($_POST[‘ExMusclesDetails’][‘targeted_muscles’] as $values){

$model->targeted_muscles =($values);

}

            &#036;model-&gt;save();   


                &#036;this-&gt;redirect(array('view','id'=&gt;&#036;model-&gt;id));


	}





	 &#036;this-&gt;render('create',array(


		'model'=&gt;&#036;model,


	));


}

please check what is wrong in my code

What changes did you make for the models and the database?

IMO, the problem is in your model codes (and the underlying database design).

I don’t believe you could change them in a few minutes. :)

in my table

1 muscles and

2 exmusclesdetails

i make the foreign key relation with two tables

in my dropdown multiselect option only saved one values in database at a time how to solve and my model relation is

public function relations()

{


	// NOTE: you may need to adjust the relation name and the related


	// class name for the relations automatically generated below.


	return array(


		'exname0' =&gt; array(self::BELONGS_TO, 'ExNameDetails', 'exname'),


		


		


                


                     'targetedMuscles' =&gt; array(self::HAS_MANY, 'Muscles', 'targeted_muscles'),


		'additionalMuscles' =&gt; array(self::HAS_MANY, 'Muscles', 'additional_muscles'),


                


                );


}

and my controller code is actioncreate

public function actionCreate()

{  


	&#036;model=new ExMusclesDetails;





	// Uncomment the following line if AJAX validation is needed


	// &#036;this-&gt;performAjaxValidation(&#036;model);


	if(isset(&#036;_POST['ExMusclesDetails']))

{

		&#036;model-&gt;attributes=&#036;_POST['ExMusclesDetails'];


             


              &#036;target =&#036;_POST['ExMusclesDetails']['targeted_muscles'];


              for(&#036;i=0;&#036;i&lt;count(&#036;target);&#036;i++)


                           {


&#036;model-&gt;targeted_muscles=&#036;target[&#036;i];


                           }





                           &#036;model-&gt;save();   


			&#036;this-&gt;redirect(array('view','id'=&gt;&#036;model-&gt;id));	


                            }





	 &#036;this-&gt;render('create',array(


		'model'=&gt;&#036;model,


	));





}

what is wrong my code

please help anyone how to insert at a time multiple dropdown values in database

remove this line ,




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



this is the error "array to string"

as i said earlier just serialize the data


$model->targeted_muscles=serialize($_POST['ExMusclesDetails']['targeted_muscles']); 


model->additional_muscles=serialize($_POST['ExMusclesDetails']['additional_muscles']);




i think change this to


$_POST['ExMusclesDetails']['targeted_muscles']=serialize($_POST['ExMusclesDetails']['targeted_muscles']); 


$_POST['ExMusclesDetails']['additional_muscles']=serialize($_POST['ExMusclesDetails']['additional_muscles']);




then use this




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



this will solve