girmay
girmay

Reputation: 3

how to show database column value during dropdown selecetion on yii2

i am using Yii2, i have two tables and two models: table 1: tenderprice:(id, itemname, quanity)(id is primary key) with model name=Tenderprice, table 2:tenderpricelist:id pk,singleprice, totalprice,tenderpriceid: (tenderpriceid is foreign key refers to Tenderprice) with model name: Tenderpricelist now i want quantity when itemname is selected:

<?
    $itemtenders=Tenderprice::find()->all();
     $itemlist=ArrayHelper::map($itemtenders,'id','itemname');
     echo $form->field($model, 'tenderpriceid')->dropDownList($itemlist,['prompt'=>'Select tender'])->hint('Please choose item one by one')->label('Add Items');
     ?>
     // inserting data into tenderpricelist based on the selection of table tenderprice
      <?= $form->field($model, 'singleprice')->textInput(['maxlength' => true])->hint('Please enter your price') ?>

    <?= $form->field($model, 'totalprice')->textInput(['maxlength' => true]) 
?>

Now i want to insert data into tenderpricelist table based on the item name selected from dropdownlist.The dropdownlist fills correctly but i cannot access the value of "quantity" column when itemname is selected. I just want when i select item name from the dropdownlist, its corresponding quanity will be shown on textbox or label and the totalprice column in my table tenderpricelist will be : totalprice

totalprice=quantity *singleprice

Note: singleprice is in table tenderpricelist, while quantity is in parent table tenderprice.

Upvotes: 0

Views: 146

Answers (1)

Nicholas Gooding Rios
Nicholas Gooding Rios

Reputation: 533

The best option is to handle that in the controller (If your validation requires to have the quantity value) or model (If is not required on validation) after you get the information from the $form back. There, before the validate and before save, you can operate with the value given by:

  • $_GET ($tenderId = Yii::$app->response->get('FormName['tenderpriceid'])) or
  • $_POST ($tenderId = Yii::$app->response->post('FormName['tenderpriceid']))

The "FormName" you have to review it by looking on the debug. Once you get the $tenderId you can make a database query and find the quantity:

$quantity = Tenderprice::find()->one()->where(['id' => $tenderId]);

And then you can pass that value to the save or update option. Let me know if this works, or please post the model and controller.

Upvotes: 0

Related Questions