Why is my IF statement not working to define membership level? - php

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

Related

Multiple checkbox validation before sql insert

I have a problem similar to the post:
how to check multiple $_POST variable for existence using isset()?
My issue is, I have a rather large list of checkboxes I need to know if they were checked in order to set the status of $row. it would be exactly like the mentioned script,all and's, except I have and OR thrown in to the works.
if ( isset($_POST['Facial1'])
&& ($_POST['Facial2'])
&& ($_POST['Facial3'])
&& ($_POST['Facial4'])
&& ($_POST['Intra-Oral1'])
&& ($_POST['Intra-Oral2'])
&& ($_POST['Intra-Oral3'])
&& ($_POST['Intra-Oral4'])
&& ($_POST['Intra-Oral5'])
&& ($_POST['Intra-Oral6'])
&& (
(
($_POST['X-Ray1'])
&& ($_POST['X-Ray3'])
)
|| ($_POST['X-Ray5'])
)
)
{
$Status = 1;
} else {
$Status = 0;
}
Each time I run a test, as soon as it gets to an element that is not checked it throws an error: undefined index: xxx. Where xxx is the first none checked item.
Checkboxes that are not checked do not get sent as part of the POST body. Your code needs to use isset:
if ( isset($_POST['Facial1'])
&& isset($_POST['Facial2'])
&& isset($_POST['Facial3'])
&& isset($_POST['Facial4'])
&& isset($_POST['Intra-Oral1'])
&& isset($_POST['Intra-Oral2'])
&& isset($_POST['Intra-Oral3'])
&& isset($_POST['Intra-Oral4'])
&& isset($_POST['Intra-Oral5'])
&& isset($_POST['Intra-Oral6'])
&& (
(
isset($_POST['X-Ray1'])
&& isset($_POST['X-Ray3'])
)
|| isset($_POST['X-Ray5'])
)
)
{
$Status = 1;
} else {
$Status = 0;
}
the isset() allows you to check one variable isset($var) or multiple variables at a time isset($var,$var1,$var2) this will return true is all exists and false if one or more are not set.
Your code is only checking one and then doing well I am not sure what
Try
if ( isset(
$_POST['Facial1'], $_POST['Facial2'], $_POST['Facial3'],
$_POST['Facial4'], $_POST['Intra-Oral1'], $_POST['Intra-Oral2']
$_POST['Intra-Oral3'], $_POST['Intra-Oral4'],
$_POST['Intra-Oral5'], $_POST['Intra-Oral6']
)
&&
(
isset($_POST['X-Ray1'], $_POST['X-Ray3'])
||
isset($_POST['X-Ray5'])
)
)
{
$Status = 1;
} else {
$Status = 0;
}
I hope this is what you intended, its not totally obvious from your code.

Boolean handling in PHP

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.

php syntax for if statement with multiple conditions ( `||` as well as `&&` )

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.

Shortest way to code this php multiple if condition

I am trying to do a query like:
If $_GET['page'] == 'items' AND $_GET['action'] == 'new' OR 'edit'
Here's what I have:
if (isset($_GET['page']) && $_GET['page'] == 'items') {
if (isset($_GET['action']) && $_GET['action'] == 'new' || isset($_GET['action']) && $_GET['action'] == 'edit') {
// This is what Im looking for
}
}
Is this correct, and is this the easiest way to make this query?
You could have done it like this as well:
if (isset($_GET['page']) && $_GET['page'] == 'items') {
if (isset($_GET['action']) && ($_GET['action'] == 'new' || $_GET['action'] == 'edit')) {
}
}
Your way is perfectly fine, although I would almost be tempted to do it the following way. The only reason I suggest this is that your code requires that both action and page are set. If action is not set then there isn't much point checking if the page is == 'items'.
if(isset($_GET['page']) && isset($_GET['action'])) {
if($_GET['page'] == 'items' && ($_GET['action'] == 'new' || $_GET['action'] == 'edit')) {
//do code here
}
}
You may also try in_array like:
if (isset($_GET['page']) && $_GET['page'] == 'items')
{
if ( !empty( $_GET['action'] ) && in_array( $_GET['action'], array( 'new', 'edit' ) )
{
// This is what Im looking for
}
}
That is one of possible solutions
if ( #$_GET['page'] == 'items' && in_array(#$_GET['action'], array('new','edit')))
Everything is ok, but also you can use function:
function paramIs($param, $values) {
$result = false;
foreach ((array)$values as $value) {
$result = $result || isset($_GET[$param]) && $_GET[$param] == $value;
}
return $result;
}
Usage:
if (paramIs('page', 'items') && paramIs('action', array('new', 'edit')))
{
// your code here
}
It will reduce the number of repetitions in your code and encapsulate logic in one place

If conditional with or

Here is my script:
$user_data = user_data($name);
if($user_data['type'] == 'clerk') {
} elseif ($user_data['type'] == 'admin' ){
}else
{ header("Location: login_admin1.php");}
}
If I am using an if statement with blank {} it works but as soon as I use this script it's not working.
if($user_data['type'] != 'clerk' || $user_data['type'] != 'admin')
{ header("Location: login_admin1.php");}
I want to check if $user_data['type'] is clerk or admin, (only clerk or admin has access to page) can check page. If it's not one of them they can't access the page.
Why not use in_array()?
if ( ! in_array( $user_data['type'], array( "clerk", "admin" ) ) ) {
// Is neither Clerk, nor Admin.
}
Cuts down on having to write $user_data['type'] over and over.
Use && instead of || in if statement:
Try:
if( $user_data['type'] != 'clerk' && $user_data['type'] != 'admin' ) {
header("Location: login_admin1.php");
}

Categories