DanDewey Posted March 7, 2023 Share Posted March 7, 2023 Hi There, I'm new to php but using WordPress and gravity forms I'm learning. I'm struggling to get a function working and wondered if anyone can point me in the right direction or at least explain the solution in a more basic way, please. Currently, I have 3 functions named beta_pay, Mileage & CC which trigger 3 separate line entries from a gravity form submission into MySQL, what I am trying to do is combine the function into one or write at least a cleaner function that enables an IF control based on form entry so that a separate line is generated if criteria are met. I've tried adding an IF/Else statement to call them at the end of beta_pay but it just causes a critical error. Any input would be great and for reference, I've left mysql_connect("") blank to keep server settings secure. Thanks in advance Dan The concept is On submission run function beta_pay add_action("gform_after_submission_123", "beta_pay", 10, 2); function beta_pay($entry, $form){ $Name = $entry["1"]; $Service = $entry["3"]; $Value = $entry["4"]; $Qty = $entry["11"]; $MileageRate = $entry["10"]; $MileageQty = $entry["5"]; $CCRate = $entry["12"]; $CCQty = $entry["13"]; $con=mysqli_connect(""); mysqli_query($con,"INSERT INTO beta_pay (Name,Type,Value,Qty) VALUES ('$Name','$Service','$Value','$Qty')"); } Then If $MileageQTY > 0 run function Mileage as well if not then skip add_action("gform_after_submission_123", "Mileage", 10, 2); function Mileage($entry, $form){ $Name = $entry["1"]; $Service = "Fixed-Milage"; $MileageRate = $entry["10"]; $MileageQty = $entry["5"]; $con=mysqli_connect(""); mysqli_query($con,"INSERT INTO beta_pay (Name,Type,Value,Qty) VALUES ('$Name','$Service','$MileageRate','$MileageQty')"); } Then If $CCQTY > 0 run the function called CC as well if not skip add_action("gform_after_submission_123", "CC", 10, 2); function CC($entry, $form){ $Name = $entry["1"]; $Service = "Congestion Charge"; $CCRate = $entry["12"]; $CCQty = $entry["13"]; $con=mysqli_connect(""); mysqli_query($con,"INSERT INTO beta_pay (Name,Type,Value,Qty) VALUES ('$Name','$Service','$CCRate','$CCQty')"); } Quote Link to comment Share on other sites More sharing options...
Solution kicken Posted March 7, 2023 Solution Share Posted March 7, 2023 Make your functions just do nothing if the condition is not met. function Mileage($entry, $form){ $MileageQty = $entry["5"]; if ($MileageQty <= 0){ return; } $Name = $entry["1"]; $Service = "Fixed-Milage"; $MileageRate = $entry["10"]; $con=mysqli_connect(""); mysqli_query($con,"INSERT INTO beta_pay (Name,Type,Value,Qty) VALUES ('$Name','$Service','$MileageRate','$MileageQty')"); } Since you only want the function to run if $MileageQty is > 0, you check if it's 0 or less and if so, return. Return will exit the function, so nothing that comes after it will be executed. Quote Link to comment Share on other sites More sharing options...
DanDewey Posted March 7, 2023 Author Share Posted March 7, 2023 Absolute legend, I was trying to run each as a seperate function but got nowhere the Return piece makes so much sense thank you Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.