Calculate field based on other field value Apply simple math formula to dynamically calcualte a field (added in v 4.3)

If you want to calculate a field value based on the other field's value then you can easily do using the formFieldValue() function. You can apply basic math function e.g addition, subtraction, multiplication or division to generate the field value based on other field's value. For example, if you want to save the total field as multiplicate of qty and rate field and don't want to show this total field to user then you can easily acheive this using this function.

Please note that you need to hide that field from form that is being calculated. Also, math function can be applied to two fields only. It doesn't work with complex functions currently. We will further improve this function in next versions.

In the below example, we are dynamically calculating sale_price field based on the price field. We are adding 24 to price field to generate sale_price field.

  
                               $pdocrud = new PDOCrud();
                                $pdocrud->formRemoveFields(array("sale_price"));
                                /**
                                 * Set a value of field
                                 * @param   string   $fieldName                            field name to renamed
                                 * @param   string   $value                                value of the field
                                 * @param   bool     $fieldHidden                          Wheter that field is hidden or not
                                 * @param   string   $tablename                            Tablename = Required only when using join table field name
                                 * return   object                                         Object of class
                                 */
                                $pdocrud->formFieldValue("sale_price", "{price}+24", true);
                                //apply formula
                                $pdocrud->fieldFormula("sale_price", "expression",array("type" =>"math_expression"));
                                echo $pdocrud->dbTable("book")->render("insertform"); // call render function
                                

Book

Showing 1 to 2 of 2 entries

# Book id Name Author Price Sale price Actions
1 12 Angles & Demons DB 600 624
2 13 tert erter 56464 56488
# Book id Name Author Price Sale price Actions