Click here to Skip to main content
14,265,979 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am using Yii2 framework with PHP.
I have a function which takes 3 parameters as id, year and month. It cheecks whether there exists a record in the table. If it does'nt exists, then four fields such as OpeningBalance, TotalValueofLoanGiven, LoanRepaidUptilNow and TotalValueOfLoanOutstanding are set to 0. Otherwise previous months values are set to the fields.

The function returns 4 values but all the 4 values are displayed in every inputs.

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
	 )
						-&gt;queryScalar(); 
 

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

        if ($group == 0) {

               echo $groupsavingdetails['OpeningBalance']=$groupsavingdetails['TotalValueofLoanGiven']=$groupsavingdetails['LoanRepaidUptilNow']=$groupsavingdetails['TotalValueOfLoanOutstanding']=0;		
            }
         else {

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

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

			echo $groupsaving_details['OpeningBalance'];
			echo $groupsaving_details['TotalValueofLoanGiven'];
			echo $groupsaving_details['LoanRepaidUptilNow'];
			echo $groupsaving_details['TotalValueOfLoanOutstanding'];
	
        }
    }



In the form I have called this method 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);
	  
	  });	
	']) ?>


What I have tried:

I am using Yii2 framework with PHP.
I have a function which takes 3 parameters as id, year and month. It cheecks whether there exists a record in the table. If it does'nt exists, then four fields such as OpeningBalance, TotalValueofLoanGiven, LoanRepaidUptilNow and TotalValueOfLoanOutstanding are set to 0. Otherwise previous months values are set to the fields.

<pre>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
	 )
						-&gt;queryScalar(); 
 

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

        if ($group == 0) {

               echo $groupsavingdetails['OpeningBalance']=$groupsavingdetails['TotalValueofLoanGiven']=$groupsavingdetails['LoanRepaidUptilNow']=$groupsavingdetails['TotalValueOfLoanOutstanding']=0;		
            }
         else {

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

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

			echo $groupsaving_details['OpeningBalance'];
			echo $groupsaving_details['TotalValueofLoanGiven'];
			echo $groupsaving_details['LoanRepaidUptilNow'];
			echo $groupsaving_details['TotalValueOfLoanOutstanding'];
	
        }
    }



In the form I have called this method 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);
	  
	  });	
	']) ?>
Posted
Updated 8-May-19 6:03am
v2
Comments
Gerry Schmitz 8-May-19 12:31pm
   
$groupsaving_details[]

The "target" (item) is not changing in the inner loop, while the "source" is? Maybe.
Member 13935271 8-May-19 12:36pm
   
So what to do?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100