-2

I am new to Yii , i have created an application in that form i have created an field as multi select option as shown below ......Can any one help me how to save the data into my mysql table for that what changes i have to do and where i have to change .. I want to save the each department which are selected have to be saved as rows

 <?php 
     $data = array('101' => 'CSE', '102' => 'IT', '103' => 'EEE', '104' => 'ECE');
$selected   = array(
  '102' => array('selected' => 'selected'),
  '103' => array('selected' => 'selected'),
);
$htmlOptions = array('size' => '5', 'prompt'=>'Use CTRL to Select Multiple Staff', 'multiple' => 'true', 'options' => $selected);
    echo $form->listBox($model, 'department',$data, $htmlOptions, array('rows' => 6, 'cols' => 50, 'class' => 'text_area')); ?>
</div>
Werner Kvalem Vesterås
  • 10,226
  • 5
  • 43
  • 50
DILLI
  • 1
  • 1
  • 7
  • I think you have to assign name of dropdown like 'department[]'. you got department value in array then you can save value using comma(,) separate whatever you want to save. Hope this work!!! – harsh4u Mar 07 '13 at 13:01
  • i want to save individually each value as an row – DILLI Mar 07 '13 at 13:14
  • can you give more information? Which model are you using for the form? Which model do you want to save to? Is it the same one? If it is, doesn't that mean you'll have several rows with the same information other than the department? – Pomme.Verte Mar 07 '13 at 14:36

2 Answers2

1

you can easily do this by defining an action in the controller

public function actionCreate(){
$model = new modelName;
if(isset($_POST['attribute']))
    {
      $model->attributes = $_POST['attributes'];
        if($model->validate()) 
           { 
             $model->save();
           }else{ "Exception Statement"}
    }
Sudhanshu Saxena
  • 1,199
  • 12
  • 32
0

I want to save the individual option as an row in the table i am getting values as an array Where i have to change the code to save the values in the multiple selction ....The output of the below code was department is "array"

public function actionCreate()
{
    $model = new Emp;
    $model2=new Emprole;
    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['Emprole']))
    {
        $model2->attributes=$_POST['Emprole'];
        $model2->save();
        $contact_id = Yii::app()->db->getLastInsertId();            

    }

    if($contact_id!='')
    {

    if(isset($_POST['Emp']))
    {
        $model->beforeSave();
        $model->attributes=$_POST['Emp'];
        if($model->save())
            $this->redirect(array('view','id'=>$model->empcode));
    }
DILLI
  • 1
  • 1
  • 7