rayna qarla
rayna qarla

Reputation: 93

how to display time from db table into drop downlist yii2?

this is my form field for dropdown list Time

<?= $form->field($model, 'Time')->widget(Select2::classname(), [
            'data' => ArrayHelper::map(Schedule::find()->where(['username' => 'cust_id', 'day'=>'Day_cust','availability'=>'available'])->all(),'time','time'),
            'language' => 'en',
            'options' => ['placeholder' => 'Select available time ...'],
            'pluginOptions' => [
            'allowClear' => true
            ],
        ]);
        ?> 

Day_cust and cust_id is taken from the previous text input in the same form which is field by the user.

I have tried the above code but it failed to display the time available.

this is my model for schedule

public function attributeLabels()
{
    return [
        'schedule_id' => 'Schedule ID',
        'username' => 'Username',
        'day' => 'Day',
        'time' => 'Time',
        'availability' => 'Availability',
    ];
}

what I want is I want to display on the drop-down list for time the time input in the database with the availability available based on cust_id and day insert by the user in the field.

how should I do it?

if anyone wonder why i dont use dependent dropdownlist is because the cust_id is using text input same goes with the day. i didnt use the ddl for both of them.

Upvotes: 0

Views: 39

Answers (1)

akshaypjoshi
akshaypjoshi

Reputation: 1295

In your ArrayHelper::map(), make sure you have used correct database field values.

Your database field contains time not Time, So ArrayHelper should be like,

ArrayHelper::map(Schedule::find()->where(['username' => 'cust_id', 'day'=>'Day_cust','availability'=>'available'])->all(),'time','time')

Upvotes: 0

Related Questions