array of data

I have two tables:

1.course

  1. lectures

  2. course_lectures

I want the values in lectures to be automatically generated based on the input of course, when save button is click in course.

For exam, if course_format is topic and course_format_no is 4, then section_name will be

topic1,

topic2,

topic3,

topic4

then, if course_format is topic and course_format_no is 2, then section_name will be

topic1,

topic2

Also, if course_format is weekly and course_format_no is 3, the start_date will be selected. If start_date is 24th November. then section_name will be something like this:

18 November - 24 November,

25 November - 1 December,

2 December - 8 December.

Please I dont know how to go about it.

course model




<?php


namespace app\models;


use Yii;


/**

 * This is the model class for table "course".

 *

 * @property string $course_id

 * @property string $course_category_id

 * @property string $course_code

 * @property string $course_name

 * @property string $course_num

 * @property string $course_summary

 * @property string $course_start_date

 * @property string $course_format

 * @property integer $course_format_no

 * @property integer $show_grade

 */

class Course extends \yii\db\ActiveRecord

{

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return 'course';

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['course_category_id', 'course_format_no', 'show_grade'], 'integer'],

            [['course_code', 'course_name', 'course_start_date', 'course_format'], 'required'],

            [['course_summary'], 'string'],

            [['course_start_date'], 'safe'],

            [['course_code'], 'string', 'max' => 100],

            [['course_name'], 'string', 'max' => 255],

            [['course_num', 'course_format'], 'string', 'max' => 20],

            [['course_code'], 'unique'],

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'course_id' => 'Course ID',

            'course_category_id' => 'Course Category ID',

            'course_code' => 'Course Code',

            'course_name' => 'Course Name',

            'course_num' => 'Course Num',

            'course_summary' => 'Course Summary',

            'course_start_date' => 'Course Start Date',

            'course_format' => 'Course Format',

            'course_format_no' => 'Course Format No',

            'show_grade' => 'Show Grade',

        ];

    }

}




It will be displayed in listview

I’m a bit confused by your question. Is section_name a column in your lectures table? Also, if course_format_no is, let’s say 3, do you want want section_name to equal one big string like “topic1, topic2, topic3”? Or did you want 3 entries into your database that hold different section names “topic1”, “topic2”, and “topic3”?

If my assumptions are right you could edit actionCreate in your CourseController to do soemthing like this:




public function actionCreate()

{

    $model = new Course();


    if ($model->load(Yii::$app->request->post()) && $model->save()) {

        //The course was created successfully, so we can use its data to make lectures


        //loop through and make the lecture with topic#

        for( $i = 1; $i <= $model->course_format_no; $i++ ) {

            $lecture = new Lectures();

            $lecture->section_name = $model->course_format . $model->course_format_no;


            // fill in other lecture data here


            $lecture->save();

            // if you needed to make a course_lecture relation, then change $lecture->save(); 

            // into an if statement and make the relationship somewhere down here.

        }


        // Finally we redirect to view by defualt

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

    } else {

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

            'model' => $model,

        ]);

    }

}



You’d have to edit the inside of the for loop to to check if course_format == “weekly” if you wanted to do the date thing you were talking about.

Thanks a lot. You got it.

section_name is a column in your lectures table. Also, if course_format_no is, let’s say 3, I want section_name to be 3 entries into the database that hold different section names “topic1”, “topic2”, and “topic3”. Then if course_format_no of the same record is changed to 2, then topic3 is deleted. Also if changed to 5, we have “topic1”, “topic2”, “topic3”, “topic4”, and “topic5”. Please can you give me a clue on how to go about course_format == “weekly”.

Am grateful