I am very new to PHP and trying to get an IF statement to work to change the membership level based on the possibility of multiple answers provided in the registration form. However, it's not working. If anyone can help me understand what I am doing wrong/missing that would be a great help. Here is my code. Thank you.
function my_pmpro_checkout_level($level)
{
if ( ! empty( $_REQUEST['registrationlevel']) && $level->id == '1' ) {
if (( $_REQUEST['registrationlevel'] == 'eligibleforlimited' && $_REQUEST['experience'] != 'morethansixyears') || ($_REQUEST['registrationlevel'] == 'eligibleforlimited' && $_REQUEST['timeassesment'] == 'no')) {
$level->id = '3';
} elseif ( $_REQUEST['registrationlevel'] == 'eligibleforprovisional' && $_REQUEST['experience'] != 'noexperience') {
$level->id = '3'}
}
return $level;
}
add_filter("pmpro_checkout_level", "my_pmpro_checkout_level");
Currently I have the following issue. I need to figure out how I can check if something is an array.
if(isset($_GET['koophuur']) && $_GET['koophuur'] == 'koop'){
$this->objects->search->koop = true;
$this->objects->search->min_koopprijs = (isset($_GET['min_prijs']) && !empty($_GET['min_prijs'])?$_GET['min_prijs']:null);
$this->objects->search->max_koopprijs = (isset($_GET['max_prijs']) && !empty($_GET['max_prijs'])?$_GET['max_prijs']:null);
}elseif(isset($_GET['koophuur']) && $_GET['koophuur'] == 'huur'){
$this->objects->search->huur = true;
$this->objects->search->min_huurprijs = (isset($_GET['min_prijs']) && !empty($_GET['min_prijs'])?$_GET['min_prijs']:null);
$this->objects->search->max_huurprijs = (isset($_GET['max_prijs']) && !empty($_GET['max_prijs'])?$_GET['max_prijs']:null);
}
if(isset($_GET['koophuurgarage']) && $_GET['koophuurgarage'] == 'koopgarage'){
$this->objects->search->koop = true;
$this->objects->search->min_koopprijs = (isset($_GET['min_prijs']) && !empty($_GET['min_prijs'])?$_GET['min_prijs']:null);
$this->objects->search->max_koopprijs = (isset($_GET['max_prijs']) && !empty($_GET['max_prijs'])?$_GET['max_prijs']:null);
}elseif(isset($_GET['koophuurgarage']) && $_GET['koophuurgarage'] == 'huurgarage'){
$this->objects->search->huur = true;
$this->objects->search->min_huurprijs = (isset($_GET['min_prijs']) && !empty($_GET['min_prijs'])?$_GET['min_prijs']:null);
$this->objects->search->max_huurprijs = (isset($_GET['max_prijs']) && !empty($_GET['max_prijs'])?$_GET['max_prijs']:null);
}
I am not getting these results in my search query right now.
You can use is_array function. it return true if array else false.
is_array($variable);
Also if you need to check if the array is empty or not then
empty($array) : returns true if empty.
If need to check if any key is set or not then you can use
isset($array['key']) or array_key_exists('key', $array)
is_array , array_key_exists
I have following code in my php script:
if ( $x != 'some_val_1' && $y['index']->title != "some_val_2" && $y['index']->title != "some_val_3" ) {
// Do something.
}
In the code shown above, index may/may not be set and thus when it is not set this code throws a php notice.
I changed it to:
if (isset($y['index']->title)) {
if ( $x != 'some_val_1' && $y['index']->title != "some_val_2" && $y['index']->title != "some_val_3" ) {
// Do something.
}
}
However since the "Do something" part is set to work when isset($y['index']->title is not equal to some_val_2 and some_val_3, the above does not work because the isset() condition I added skips the code.
So now the script does not throw notice but at the cost of completely changing the condition to something not desirable.
How can I change this code to not throw the PHP notice?
You can handle it in different ways, I initialize the variables if they are not defined:
if (! isset($y['index']->title)) {
$y['index']->title = '';
}
if ( $x != 'some_val_1' && $y['index']->title != "some_val_2" && $y['index']->title != "some_val_3" ) {
// Do something.
}
I prefer this way because I don't need to change my code and add if conditions (with isset for example) in all my program. I just add the conditions at the top of my code.
Since you are dealing with an array of objects, you can use the function array_key_exists().
Your code will look like this:
if ( array_key_exists('index',$y) && $x != 'some_val_1' && $y['index']->title != 'some_val_2' && $y['index']->title != 'some_val_3' ) {
// Do something.
}
You can read more about it here: http://php.net/manual/en/function.array-key-exists.php
You can try using isset & in_array in this scenario.
if ( $x != 'some_val_1'
&& isset($y['index']->title)
&& !in_array($y['index']->title, array("some_val_2", "some_val_3") ) ) {
// Do something.
}
The benefit of using in_array is that you validate case-sensitivity by passing TRUE / FALSE in the third parameter.
Look at my script:
if((isset($_POST['unma']) && isset($_POST['livi'])) && (isset($_POST['cont']) &&
isset($_POST['phone']))){
if(
(
(
(
(isset($_POST['fnma']) && isset($_POST['lnma']))
&&
(isset($_POST['occ']) && isset($_POST['hom']))
)
&&
(
(isset($_POST['town']) && isset($_POST['dist']))
&&
(isset($_POST['dispmb']) && isset($_POST['fbc']))
)
)
&&
(
(
(isset($_POST['gp']) && isset($_POST['twt']))
&&
(isset($_POST['ins']) && isset($_POST['flc']))
)
&&
(
(isset($_POST['ile']) && isset($_POST['cam']))
&&
(isset($_POST['cam_co']) && isset($_POST['prof_phr']))
)
)
)
&&
isset($_POST['awards'])
){
// Codes here
}
How can I short this isset functions. I found an solution with foreach but they are not checking the post isset, rather they are checking if the posted values are not empty
You can use
if (isset($_POST['a'], $_POST['b'], $_POST['c'], ...))
and so on. If any of the variables is not set then you will get false.
$expectedKeys = ['fnma', 'lnma', ...];
if (!array_diff_key(array_flip($expectedKeys), $_POST)) {
// all keys are set
}
I think something like this would work:
// add all names of the values that needs to be set in the `$_POST` array
$values = array('fnma', 'lnma');
function checkSet($array) {
foreach($array as $value) {
if(!isset($_POST[$value])) return false;
}
return true;
}
if(checkSet($values)) {
// your code
}
put required fields in a array and check for it using loop,it is easy and easy to add required fields.
$req_values = array('fnma','lnma','occ','hom','town','dist','dispmb','fbc','gp','twt','ins','flc','ile','cam','cam_co','prof_phr');
foreach($req_values as $key) {
if (empty($_POST[$key])) {
echo $key .'is required';
exit;
}
}
Knowing that isset can take multiple values, you could do this:
if (isset($_POST['unma'], $_POST['livi'], $_POST['cont'], $_POST['phone'], $_POST['fnma'], $_POST['lnma'], $_POST['occ'], $_POST['hom'], $_POST['town'], $_POST['dist'], $_POST['dispmb'], $_POST['fbc'], $_POST['gp'], $_POST['tat'], $_POST['ins'], $_POST['flc'], $_POST['ile'], $_POST['cam'], $_POST['cam_co'], $_POST['prof_phr'], $_POST['awards'])) {
// Codes here
}
But that seems nightmarish. I would make the $_POST keys an array & then do the following:
// Set an array of post values.
$post_values = array('unma','livi','cont','phone','fnma','lnma','occ','hom','town','dist','dispmb','fbc','gp','tat','ins','flc','ile','cam','cam_co','prof_phr','awards');
// Set '$post_valid' to an array.
$post_valid = array();
// Roll through the post values.
foreach ($post_values AS $post_key => $post_value) {
// If the value is set, set '$post_valid' to TRUE or else set it to FALSE.
// $post_valid = isset($_POST[$post_key]) ? TRUE : FALSE; // Not really needed. Simple `isset` should work.
$post_valid[$post_key] = !empty($_POST[$post_key]);
}
// Now if '$post_valid' values are all true, do something.
$post_valid_values = array_values($post_valid);
if (!empty($post_valid_values) && (count($post_valid_values) == count($post_values))){
// Codes here
}
Context: On our website, we calculate whether an item/order meets the criteria for free shipping using an if statement evaluating if a value - 'ff' - is true. Depending on conditions met it sets the shipping appropriately. We now require the need to evaluate if an item is free shipping with the order of _. To top it off, for other purposes such as external feeds, another condition must apply (weight must = 0 or have no value), for fw to be free freight. Here is the code, that I can't figure out why it is NOT working:
if($r['freeFreight'] == 'ff' || ($r['freeFreight'] == 'fw' && ($r['weight'] == 0 || $r['weight'] == '') ) ) {
$r['shippingStandard'] = 0;
}
Are the conditions overly segregated in the if statement with the sets of ()? Or, are they just incorrectly placed. For example, should it be:
if(($r['freeFreight'] == 'ff') || ($r['freeFreight'] == 'fw' && $r['weight'] == 0 || $r['weight'] == '') ) {
$r['shippingStandard'] = 0;
}
The second of the two seems more logical to me because: if ($r['freeFreight'] == 'ff') - then regardless of the following conditions the statement should return true and set the variable to 0. Is my logic correct? Sorry for this newb question...
Thanks for any help you can offer.
So I think perhaps, based on the answers so far (thanks everybody that has chimed in to help an amateur) - I think I am going to do a trial with:
if( ($r['freeFreight'] == 'ff') || ( $r['freeFreight'] == 'fw' ) && empty( $r['weight'] ) ) {
$r['shippingStandard'] = 0;
}
Planning to run trial with this, if it is fundamentally wrong, please advise.
Try this out?
if( $r['freeFreight'] == 'ff' || ( $r[ 'freeFreight' ] == 'fw' && empty( $r['weight'] ) ) ) {
$r['shippingStandard'] = 0;
}
using empty might be a little cleaner too
if you really want to break it out more, you could do this
if( $r[ 'freeFreight' ] == 'ff' ) {
$r[ 'shippingStandard' ] = 0;
} elseif( $r[ 'freeFreight' ] == 'fw' && empty( $r[ 'weight' ] ) ) {
$r[ 'shippingStandard' ] = 0;
} else {
// Everything else
}
If you want to shorten the variables, and will be using them later:
$freight = $r[ 'freeFreight' ];
$weight = isset( $r[ 'weight' ] ) ? $r[ 'weight' ] : null;
$shipping = $r[ 'shippingStandard' ]; // Or whatever you want the default value to be...
if( $freight == 'ff' || ( $freight == 'fw' && empty( $weight ) ) ) {
$shipping = 0;
}
// ... Later down the file
$r = array(
'freeFreight' => $freight,
'weight' => $weight,
'shippingStandard' => $shipping
);
I really cant tell how the rest of the file looks, like if this is in a function to get shipping, you could simply just return $shipping. Hope this helps. You should be able to move the concepts around to get what you want
your first option should should be identical to the following:
if ($r['freeFreight'] == 'ff') {
$r['shippingStandard'] = 0;
}
elseif ($r['freeFreight'] == 'fw') {
// the dual check for an empty value here is kind of redundant and unneccessary.
// I'd probably just use "if ($r['weight'])" and be done with it
if ($r['weight'] == 0) {
$r['shippingStandard'] = 0;
}
elseif ($r['weight'] == '') {
$r['shippingStandard'] = 0;
}
else {
// $r['shippingStandard'] stays whatever it was before
}
}
else {
// $r['shippingStandard'] stays whatever it was before
}
if that logic looks right to you, then you should be right on and I'd print_r($r) to make sure it's holding what you expect it to be holding. Your parenthesis in the first example is exactly how I would do it.