Same Values getting displayed on textInputs in Yii2

Hi,
I am using Yii2 basic application template.
I have a module called groupsavingdetails where user stores groupsaving records dfor each month.

Groupsavingdetails - table

  1. GroupSavingDetailsId - PK Auto Increment
  2. EmpId - FK
    3.GroupId -FK
  3. Year
  4. Month
  5. OpeningBalance
  6. TotalSaving
  7. LoanRecovery
  8. LoanInterest
  9. Fine
  10. BankInterest
  11. Expenses
  12. LoanGiven
  13. BankLoan
  14. TotalValueofLoanGiven
  15. LoanRepaidUptilNow
  16. TotalValueOfLoanOutstanding
  17. CahInHand
  18. CashInBank
  19. ClosingBalance

For eg:
The scenario is if the Group Formation Date is April 2017, then
For April 2017,

OpeningBalance=0
TotalSaving=1000
LoanRecovery=0
Loan Interest=0
Fine=0
BankInterest=0

Expenses=50
LoanGiven=0
BankLoan=0

Thus Saving for April month will be 950.

Closing Balance of April=950

So I have a function in controller which will set the values for OpeningBalance, TotalValueofLoanGiven, LoanRepaidUptilNow and TotalValueOfLoanOutstanding fields on form.

public function actionGroupsavinginfo($id,$year,$month)
    {
        $group = Yii::$app->db->createCommand('SELECT count(*) 
 FROM `groupsavingdetails`, groupdetails
 where groupdetails.GroupId=groupsavingdetails.GroupId
 and groupsavingdetails.Year<='.$year.'
 and groupsavingdetails.Month<='.$month.'
 and groupsavingdetails.GroupId='.$id
	 )
						->queryScalar(); 
 

 $groupsavingdetails = Yii::$app->db->createCommand('SELECT * 
 FROM `groupsavingdetails`
where
  groupsavingdetails.Year<='.$year.'
 and groupsavingdetails.Month<='.$month.'
 and groupsavingdetails.GroupId='.$id
	 )
						->queryAll(); 

        if ($group == 0) {

               $groupsavingdetails['OpeningBalance']=0;
			   echo $groupsavingdetails['OpeningBalance'];
			   $groupsavingdetails['TotalValueofLoanGiven']=0;
			   //echo $groupsavingdetails['TotalValueofLoanGiven'];
			   $groupsavingdetails['LoanRepaidUptilNow']=0;
					$groupsavingdetails['TotalValueOfLoanOutstanding']=0;

					
								
			
            }
         else {
            foreach($groupsavingdetails as $groupsavingdetails1)
			 {
			
			    echo $groupsaving_details['OpeningBalance'] = $groupsavingdetails1['ClosingBalance'];
				
				echo $groupsaving_details['TotalValueofLoanGiven']= $groupsavingdetails1['TotalValueofLoanGiven'];
				
				echo $groupsaving_details['LoanRepaidUptilNow']=$groupsavingdetails1['LoanRepaidUptilNow'];

				
				echo $groupsaving_details['TotalValueOfLoanOutstanding']= $groupsavingdetails1['TotalValueOfLoanOutstanding'];
		
			}
       
        }
    }

On the form I have a field for Month which is a dropdown and on change event of dropdown I have called and set the values as follows:

<?= $form->field($model, 'Month',['enableAjaxValidation' => true])->dropDownList(['01'=>'January', '02' => 'February','03'=>'March','04'=>'April','05'=>'May','06'=>'June','07'=>'July','08'=>'August','09'=>'September','10'=>'October','11'=>'November','12'=>'December'], ['prompt'=>'Select Month','onChange'=>'
            

	$.post("index.php?r=groupsavingdetails/groupsavinginfo&id='.'"+$("select#groupsavingdetails-groupid").val()+"&year='.'"+$("input#groupsavingdetails-year").val()+"&month='.'"+$("select#groupsavingdetails-month").val(),function(data){
						
			$("input#groupsavingdetails-openingbalance").val(data);
						$("input#groupsavingdetails-totalvalueofloangiven").val(data);
						$("input#groupsavingdetails-loanrepaiduptilnow").val(data);
						$("input#groupsavingdetails-totalvalueofloanoutstanding").val(data);
	  
	  });


	
		
	']) ?>

Here when there is no record for selected group, year and month then, 0 gets assigned to each fields. But when there is else condition is executed then Opening Balance is set to the closing balance of previous month at the same time the same value gets set to the other fields also

How should I resolve this?

I have attached a file showing the process of group saving for months of year.

Image 1
!

Image 2
image2

Image 3