dropdownlist selected value

How do I write the code to save into my database based on the value in my dropdownlist when the save button is clicked.The dropdownlist value can either be equal to ‘weekly format’ or ‘Topic format’

The attribute name for the dropdownlist is ‘course_format’

for example,

If dropdownlist = ‘weekly format’

Then…

else

I want to do this in the controller.

[b]

Model

[/b]




class Course extends \yii\db\ActiveRecord

{

    const TYPE_WEEKLY= 'Weekly format';

    const TYPE_TOPIC='Topic format';    


    public static function tableName()

    {

        return 'course';

    }

    public static function getCourseFormat()

    {

    return[

    Yii::t('app', self::TYPE_TOPIC) => Yii::t('app', 'Topic format'),

    Yii::t('app', self::TYPE_WEEKLY) => Yii::t('app', 'Weekly format'),

    ];

    }


}




[b]

View

[/b]




        <div class="col-xs-12 col-sm-4 col-lg-4">

          <?= $form->field($model, 'course_format')->dropDownList($model->getCourseFormat(),['prompt'=>'Select Course Format']) ?>

        </div>




      <div class="col-xs-6">

        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-block btn-success' : 'btn btn-primary']) ?>

      </div> 




[b]

Controller

[/b]




public function actionCreate()

{      

    $request = Yii::$app->request;

    $model = new Course();

   

    // if post data was submitted, try to load it into the new model

    if ($request->isPost && $model->load($request->post())) {

        // check if topic format 

        if($model->course_format == Course::TYPE_TOPIC){

            // do your topic related stuff 

        }

        // check if weekly format 

        if($model->course_format == Course::TYPE_WEEKLY){

            // do your weekly related stuff 

        }

        // don't forget to save your model 

        if($model->save()){

            // redirect user to view 

            return $this->render('view', ['model' => $model->id]);

        }

    }

    

    // will be executed by default 

    // when no post data is submitted or model->save() failed

    return $this->render('create', [

        'model' => $model,

    ]);

}




[b]

Note

[/b]

If course_format is weekly and course_format_no is 3, the start_date (Datepicker) will be selected. If start_date is 18th November, 2016. then lecture_name will be something like this:

18 November - 24 November,

25 November - 1 December,

2 December - 8 December.

And lecture_code will be something like this:

1,

2,

3

Note that lecture_name is increased by one week.

lecture_name and lecture_code are columns in lectures table.

It should be saved in different fields and not as a big string

Where I have issue is where course_format is weekly.

Note that is a date picker.

7628

course format.PNG

7629

course start date.PNG