Special Calculations

annasweb

Member
I need to be able to give a checkbox item a value of 5 points. I also need the list view to show the total for all check boxes selected (five selected would show 25 points for that member in that element) I then need to add those points to other points from another element that would show in a separate column as total points for that member.

Is this possible? How can I make this work?

Thanks,
Anna
 
Thank you. I will give it another try later today and see if I can make it work after I review the links. I didn't have any idea where to go for documentation. This is a big help.

Anna
 
I tried to calc the element but I can only see the "List View Settings" withing the element and according to the wiki, the calculations will be shown at the bottom of the form. I don't want any calculations at the bottom of the form.

I can't see where to calculate the elements value within the form.

I need to have the calculations in-line with the elements. Is it possible to have an element named "Points" and have the value of that element as a calculation of the info from 2 other element calculations added together? Like 5 badges times 25 points plus 3 badges times 1oo points and have that total show next to the skater in my form? It would only show the total for that skater based on the checkboxes selected in each element.

Thanks,
Anna
 
You don't need "to calc the element" but to "use a calc (or calculation) element". I beleive it is part of the core Fabrik, but if not (that is, it does not show in your list of element types when you create a new field in your form), you need to update from GitHub (link to the procedure in may signature).
 
I'm totally confused now.

You don't need "to calc the element" but to "use a calc (or calculation) element". I don't understand what you mean here. Do you mean I should create a "Type" called calc? When I look in the dropdown list of element types I create I don't see a calc type.

I did link to the links provided and couldn't make anything out with them.

Are you saying I need to add a new field in my form? I thought the elements were the fields. If I add a new field/element to my form how would I get it to calculate the two other elements for a value?

Thanks,
Anna
 
When you want to perform some calculation with the value entered in some elements in your form, you use a "calc" element. It is indeed a new element you add in your form. it can be hidden so the user that fills the form does not see the calculation taking place.
You can read more about this element here: http://fabrikar.com/wiki/index.php/Element_Calculation

If you don't see this element in your list of types, then you need to update your Fabrik install from GitHub: http://fabrikar.com/forums/index.php?wiki/update-from-github/
 
Thanks for the explanation. I'm not so confused now. I will update from GitHub and see if the "type" shows up so I can create the calculations.

I'll let you know how this works.

Thanks again,
Anna
 
I upgraded the files as suggested. I now see the Calc element type. I entered the following in the calculation field and I only see 0 on the list page.

return int)'{Member_Manager___select_badges_raw}' * 5;

The select_badges is an element with checkboxes and I need to multiply the number of selected boxes by 5. I thought the calculation above would be how to do that based on what I read. I'm not a PHP expert by any means but do need help figuring out how to make this calculate.

The actual calculation I need is to take all the "selected" checkbox fields in my select_badges, multiply them by 5. Calculate the checkboxes in my trick_badges element and multiply them by 100 and add the total of these two calculations as one number that will show on the list.

Any suggestions on how to make this work?

Anna
 
Well, if it is a calculation on the number of selected boxes, then you need to write some JS to do that but I am not an expert for that.
 
No need for JS. Just need to get the actual submitted data, which should be array of all the checked boxes, and count the items in the array.

Try this:

PHP:
$checked = $formModel->getElementData('Member_Manager___select_badges', true);
$checked = count($checked);
return $checked * 5;

If that doesn't work, put:

PHP:
var_dump($checked);exit;

... as the second line (so before the count), and copy and paste the resulting debug output here.

-- hugh
 
Can anyone tell me how to make this calculation happen or where I can go for more info? I checked the wiki but can't get it to work. Is it possible that it's not working because I have text in the "value" for each checkbox? Would it work better if I put a "1" in the value instead? It seems to now be counting any selections but shows a 5 for every row.

Thanks,
Anna
 
OK, try this:

PHP:
$chbox = $data['Member_Manager___select_badges_raw'];
$chbox_count = 0;
if (!empty($chbox)) {
    if (!is_array($chbox)) {
        $chbox = json_decode($chbox);
    }
    $chbox_count = count($chbox);
}
return $chbox_count * 5;

Works for me.

the problem is that sometimes that data is an array (when submitting the form), and sometimes it's a JSON string, when rendering the list or form. JSON is what we actually store in the database, to represent the array of selected values, so it's a string like ["1", "3","7"] etc. representing an array, instead of an actual array.

Anyway, let me know how you get on with the above code.

-- hugh
 
Thanks Hugh,

It doesn't work for me. I'm going to remove all the members on my database, add some new ones and see if it works with a clean list of members. I'll let you know in a few. I'd list the url but when I tried I couldn't get it to post.

Thanks,
Anna
 
Hugh,

I deleted all the member records and entered 1 new member. I then checked 3 badges for that member. I looked at the front end and it did calculate 15 points. So, I think this will work. Now I only need to duplicate this for the trick badges and make the 2 add together. Can I do that in this same calculation (ideal) or do I need to do another calc and then a third one to add the two together?

Thanks,
Anna
 
Hugh,

I figured it out. I now have the total badges points and the total trick points showing in the points column. It seems to work just fine now. Below is what I ended up using.

$chbox = $data['records_skaters___select_badges_raw'];
$chbox_count = 0;
if (!empty($chbox)) {
if (!is_array($chbox)) {
$chbox = json_decode($chbox);
}
$chbox_count1 = count($chbox);
}

$chbox = $data['records_skaters___trick_badges_raw'];
$chbox_count = 0;
if (!empty($chbox)) {
if (!is_array($chbox)) {
$chbox = json_decode($chbox);
}
$chbox_count2 = count($chbox);
}
return $chbox_count1 * 25 + $chbox_count2 * 100;

Thanks so much for the help.

Anna
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top