CalcBuilder Forum

sections, arrays and sums

Usuario Koen_Paredaens 2016-02-27 21:08:43

I need to calculate the total volume and weight of boxes. So a site visiter needs to imput the length, width, height and weight of a number of boxes.

For this I want to create four fields in a repeating section.

How do I code that for each box I get a volume, a total volume for all boxes and a total weight.

I also want to count how many lengths are >120cm
and how many boxes weigh more than 30kg

Moonsoft support 2016-02-29 09:16:47

Hi,
the steps to build that calculator would be:

-Create your input fields (length, width, height and weight) typed as 'number'

-Create a new field 'volume' typed as 'inline result'

-Create a new section and select all of them to be included

-Create your input layout. Form layout->select your section tab and insert your fields or use the helper buttons to do it automatically.

- Code section: you need to iterate all lines to get the result, the code would be like:

$totalvolume=0;
for($i=0;count($length)>$i;$i++){
$volume=$length*$width*$height;
$totalvolume=$totalvolume+$volume;

}


This will fill the 'volume' result for each line, and also calculate a total that you can print at the exit layout using ##totalvolume##


You can add a totalweight following the same pattern and also create your other needed results.


Hope this helps, regards

Usuario Koen_Paredaens 2016-03-02 15:09:49

Hi

No, it doesn't calculate.
I allways get 0.

Something else is strange. In the layout form I put all the fields next to each other and these appear vertically on the site.

Moonsoft support 2016-03-02 16:09:35

Hi,
if you get a result the calculator is working, if the final result is 0 there is something wrong at your code or configuration. Please double check if the section is properly configured, for example printing as a result the number of lines $result=count($length);

and also that your fields are named exactly as your code (length, width, height).

About the layout, the style you set with the html editor inside the module is applied, but as the module is loaded inside your page, some styles from your template can be inherited affecting to the final look, as it happens with any other module.

If you can't get the code working please export your calculator and attach it to a ticket of your helpdesk, we'll take a look and let you know how to adjust.

Regards

Usuario Koen_Paredaens 2016-03-02 17:26:48

I started all over again. All problems solved!

For those interested here's the code for the weight.


$totalweight=0;
for($i=0;count($weight)>$i;$i++){
$weight=$weight;
$totalweight=$totalweight+$weight;


Thank you!

Koen

...
Support/development 10 hours

Get a bigger amount of hours for more complex tasks and get a 10% discount

Buy now!
...
CalcBuilder

Create dynamic calculators

Buy now!