• Resolved livewithdreams

    (@livewithdreams)


    Is it possible to calculate from two number fields in PODS? I would like to add two fields and then subtract the value of the first feild from second and show it in a new feild. For reference I am going to use beaver themer to show the numbers and results.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Jim True

    (@jimtrue)

    Nope, this isn’t possible on the back-end, as this requires Conditionals (the two values have to have values in order to perform calculations).

    However, if you’re just wanting to show it on the front-end, this is very possible with some PHP

    {@ID,calc_field_less_other_field}

    the function field_less_other_field would need to do the math calculation and pull the fields based on the post ID being passed. That could be as simple as:

    
    function calc_field_less_other_field ($id) {
       $field_one = get_postmeta($id,'field1',true);
       $field_two = get_postmeta($id,'field2',true);
       return $field_two - $field_one;
    }
    

    There’s obviously no error checking in this, and you would have to make sure your fields were represented as numbers and that you’re performing the math correctly, but this would be something you could test at your end easily.

    Plugin Author Jim True

    (@jimtrue)

    Creating a Calculation Field doesn’t always make sense, because if the calculation will never change, your calculation will always be accurate if it’s handled in code based on the value of the other two fields.

    Plugin Author Jim True

    (@jimtrue)

    Well, shoot, I’m not a PHP Developer, so apologize, but I typed get_postmeta instead of the actual get_post_meta. My bad!

    Here’s the corrected code:

    
    function calc_field_less_other_field ($id) {
       $field_one = (int) get_post_meta($id,'field1',true);
       $field_two = (int) get_post_meta($id,'field2',true);
       $result = $field_two - $field_one;
       return $result;
    }
    
    • This reply was modified 6 years ago by Jim True. Reason: Fixing the PHP Code. I'm really not a PHP Developer ;)
    • This reply was modified 6 years ago by Jim True. Reason: Type casting the two variables to Integer. I always knew Hollywood PHP Casting Directors were prejudiced against Integers
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Simple Calculations’ is closed to new replies.