Jump to content

Calling a function on an IF/ELSE I think


Go to solution Solved by kicken,

Recommended Posts

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')");
}

 

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/315985-calling-a-function-on-an-ifelse-i-think/
Share on other sites

  • Solution

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.