Skip to content
Advertisement

PHP how to do math in foreach cycle

I have a dynamical fields that contains values and i need to do math with them. What is the way to do it in foreach, do i have to save each value at the end of each loop?

Example there is 4 fields:

JavaScript

What i have is that I print these dynamical values into table and the table adapts users inputs:

JavaScript

I can get TOTAL SUM (in this case $SUM in table) of all these values, but not 1 by 1.

JavaScript

How can i get total value of each loop?

Thanks in advance.

Advertisement

Answer

Create a function with your formular and a extra foreach. Use the function inside your foreach loop. You can even loop over the fields in the array. (If you get them as array.)

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement