Jump to content
ohno

OR statement with multiple AND statements

Recommended Posts

Hi, can't get my head around this one! I have this code :-

if (isset($_SESSION['cartid']) && !isset($_SESSION['lockedcard']) && isset($_POST['cartitem']) && isset($_POST['quantity']) && is_numeric($_POST['quantity'])) {
        if ($_POST['quantity'] > 0) {

Which I need to add an OR statement to : - (!isset($_SESSION['lockedpaypal']).

Both SESSION ID's are either a 1 or 0 so not sure why the original coder used isset but in any case how do I code this so it evaluates whether locked card or locked paypal are either NOT a 1 or ARE a 0? I tried this but it does not work:-

 if (isset($_SESSION['cartid']) && ($_SESSION['lockedpaypal'] != '1' || ['lockedcard'] != '1') && isset($_POST['cartitem']) && isset($_POST['quantity']) && is_numeric($_POST['quantity'])) {
        if ($_POST['quantity'] > 0) {

Thanks!

Share this post


Link to post
Share on other sites
Posted (edited)

I re-arranged your long if to make it easier to interpret:

	if (isset($_SESSION['cartid']) && 
		( $_SESSION['lockedpaypal'] != '1' || ['lockedcard'] != '1' ) && 
			isset($_POST['cartitem']) && 
			isset($_POST['quantity']) && 
			is_numeric($_POST['quantity'])
		) 
	{
		if ($_POST['quantity'] > 0) 
		{
	

You have what looks like an index being used without a containing element.   The use of 'lockedcard' is invalid.

Do you have php error checking enabled?  That would help.

error_reporting(E_ALL);
ini_set('display_errors', '1');

Place at the top.

Edited by ginerjm

Share this post


Link to post
Share on other sites

Thanks for the reply. I've added the code & also enabled PHP error reporting in cpanel but no errors are displayed? 'lockedcard' & lockedpaypal' are both sessions if that helps?

Share this post


Link to post
Share on other sites

OK, this works : -

 

if (isset($_SESSION['cartid']) && !isset($_SESSION['lockedcard']) && isset($_POST['cartitem']) && isset($_POST['quantity']) && is_numeric($_POST['quantity'])) {

But I need to check whether 'lockedpaypal' is not set, so if either 'lockedcard' or 'lockedpaypal' is not set? Maybe I'm completely misinterpreting the logic, it's hard working with kids at home! Thanks again for any help.

Share this post


Link to post
Share on other sites

Tried this :-

 

if (isset($_SESSION['cartid']) && !isset($_SESSION['lockedcard'] || ($_SESSION['lockedpaypal']) && isset($_POST['cartitem']) && isset($_POST['quantity']) && is_numeric($_POST['quantity']))) {

& get this PHP error :-Cannot use isset() on the result of an expression (you can use "null !== expression" instead)

Share this post


Link to post
Share on other sites

Make sure to use parentheses to enforce the correct logic when mixng AND with OR

Instead of

A AND B OR C

specify

(A AND B) OR C

or

A AND (B OR C)

Share this post


Link to post
Share on other sites

Thanks, how would I do A AND B AND C AND (D OR E)? Tried this but doesn't work...

 

if (isset($_SESSION['cartid']) && isset($_POST['cartitem']) && isset($_POST['quantity']) && is_numeric($_POST['quantity']) && (!isset($_SESSION['lockedcard'] || $_SESSION['lockedpaypal']) {

 

Share this post


Link to post
Share on other sites

Can't say as you are the only person who knows what, precisely, you are trying to specify.

Share this post


Link to post
Share on other sites

Well the original code is this :-

 

 if (isset($_SESSION['cartid']) && !isset($_SESSION['locked']) && isset($_POST['cartitem']) && isset($_POST['quantity']) && is_numeric($_POST['quantity'])) {

Which I am reading as if session 'cartid' AND 'cartitem' AND 'quantity' AND 'quantity' is a numeric value AND( 'locked' is NOT(NOT what though? It's either a 1 or 0 when i check by using print_r($_SESSION);) then do this....

 

What I wish to do is 'cartid' AND 'cartitem' AND 'quantity' AND 'quantity' is a numeric value AND( either 'lockedcard' OR' lockedpaypal' is NOT set to 1) then do this....

Share this post


Link to post
Share on other sites
Posted (edited)

You have been given an error message. Stop doing what it says you are doing.   You can't use isset the way you are trying to use it.

BTW - try and write statements that don't run on so long.  There's nothing wrong with making your lines shorted so that they are more readable instead of running off the page/screen.  Use the enter key to keep them in view.  

Edited by ginerjm

Share this post


Link to post
Share on other sites

So what is required? I'm pasting the code as a pro developer wrote it. (I'm no PHP developer hence asking for help on here).

Share this post


Link to post
Share on other sites
Posted (edited)

You posted this:

	if (isset($_SESSION['cartid']) && !isset($_SESSION['lockedcard'] || ($_SESSION['lockedpaypal']) && isset($_POST['cartitem']) && isset($_POST['quantity']) && is_numeric($_POST['quantity']))) {
	& get this PHP error :-Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
	

If you look at the manual it might make more sense.  The isset function can work only on variables.  It cannot work on the result of a calculation.  Think about it - the function tells you if a variable has been declared.  If it exists.  If it is set.  Simple huh?  So you have this in your statement:

!isset($_SESSION['lockedcard'] || ($_SESSION['lockedpaypal'])

which is calling the function with an argument that evaluates to a boolean value.  That is NOT a variable.

 

Edited by ginerjm

Share this post


Link to post
Share on other sites

No, PHP makes no sense to me what's so ever. I give up, what I thought would be a simple task turns into a headache. I've looked at god knows how many manuals guides etc & have tried everything I have found. None of which worked. Is it really so hard to achieve this?! :-

'cartid' AND 'cartitem' AND 'quantity' AND 'quantity' is a numeric value AND( either 'lockedcard' OR' lockedpaypal' is NOT set to 1) then do this....

 

Share this post


Link to post
Share on other sites
Posted (edited)

Perhaps

$cartidOK = $_SESSION['cartid'] ?? 0;
$cartitemOK = $_POST['cartitem'] ?? 0
$quantityOK = isset($_POST['quantity']) && is_numeric($_POST['quantity']);
$lockedcard = $_SESSION['lockedcard'] ?? 0;
$lockedpaypal = $_SESSION['lockedpaypal'] ?? 0;

if ( $cartidOK && $cartitemOK && $quantityOK && !$lockedcard && !$lockedpaypal) {
    // do it
}

 

Edited by Barand
Spulling errors
  • Great Answer 1

Share this post


Link to post
Share on other sites

This "WORKS" ....

 

if (isset($_SESSION['cartid']) 
&& isset($_POST['cartitem']) 
&& isset($_POST['quantity']) 
&& is_numeric($_POST['quantity']) 
&& (!isset($_SESSION['lockedcard']) 
&& $_SESSION['lockedcard'] == '') 
&& (!isset($_SESSION['lockedpaypal']) 
&& $_SESSION['lockedpaypal'] == '')) {

But now I get

 

ERRNO: 8 TEXT: Undefined index: lockedcard

Share this post


Link to post
Share on other sites

Very nice looking!!  

Now you have to research why you haven't set up the variable $_SESSION['lockedcard'].  All the others have been declared/set but that one hasn't.  Is it perhaps mis-spelled?

Share this post


Link to post
Share on other sites

Spelling is fine. SESSION['lockedcard'] is only set if user has visited card payment page, SESSION['lockedpaypal'] is only set if Pay Pal is visited. I guess I should be setting SESSION['lockedcard'] to '0' if PayPal page is visited & SESSION['lockedpaypal'] to '0' if card page is visited perhaps?

Share this post


Link to post
Share on other sites

My code example defaults to 0 for both of those if they aren't set.

  • Like 1

Share this post


Link to post
Share on other sites

I mis-read your newest code sample.  Why do you not do a check of lockedcard  item with the isset function before seeing if it has a value?  That is the problem.  You can't check the value of something that doesn't exist.  

Share this post


Link to post
Share on other sites

I don't know how to. Leaving it for now, thanks for you help. The code posted by Barand didn't work either. Perhaps I'm missing something but with kids screaming and god knows what else going on I've run out of patience for today! Give me 200 pairs of wires to connect correctly no problem but THIS may as well be in Chinese.

Share this post


Link to post
Share on other sites

So why are you doing this?  Do you also pull apart your car's engine to work on it without the knowledge?

Programming is not easy especially if you don't begin at the beginning.

Share this post


Link to post
Share on other sites

I tried as what I said above & it works. No errors. Most probably not the correct way but works!

 

So I have this : -

if (isset($_SESSION['cartid']) 
&& isset($_POST['cartitem']) 
&& isset($_POST['quantity']) 
&& is_numeric($_POST['quantity']) 
&& (!isset($_SESSION['lockedcard']) 
&& $_SESSION['lockedcard'] == '') 
&& (!isset($_SESSION['lockedpaypal']) 
&& $_SESSION['lockedpaypal'] == '')) {

On the card page :-

 

$_SESSION['lockedpaypal'] = 0;
$_SESSION['lockedcard'] = 1;

 On the PayPal page :-

 

$_SESSION['lockedcard'] = 0;
$_SESSION['lockedpaypal'] = 1;

 I'll take a look at what was suggested another day :) Right now I need some piece & a cool beer. Take care all :)

Share this post


Link to post
Share on other sites
Just now, ginerjm said:

So why are you doing this?  Do you also pull apart your car's engine to work on it without the knowledge?

Programming is not easy especially if you don't begin at the beginning.

Do you never help someone? Wiring a telecom system is not easy but I could help you blindfolded with my hands behind my back ;) & yes, I've pulled apart many things in the past. That's how some people learn. I really don't have time to start learning PHP from the beginning & finding a coder to something as small as this is impossible (much like finding a telecoms engineer to fit a telephone socket ;) )

Share this post


Link to post
Share on other sites

Looking at your last set of code I see you will still have the same problem with the lockedcard part.  You need to use isset on it before trying to see if it has the value you are looking for.

And - no - I don't help someone if I don't know what I am doing.  I politely say 'No - I can't help you.'  and leave it at that.

Share this post


Link to post
Share on other sites
Posted (edited)

this logic should NOT be combined in one statement. here's why. this code is trying to process a form submission. the visitor who was presented with the form and submitted it expects the form processing code to run or to be told exactly why it didn't. these session variables are inputs to the process and must be validated, along with validating the form data, before actually using the form data. each condition that will prevent the form processing code from running should result in a unique and helpful error message being setup and displayed telling the visitor why the submitted form was not processed. for those things that the visitor has control over, he can correct, then go through the process of submitting the form again. if the process is in a state where no further forms of the type that was submitted can be processed, the error message should indicate this.

your form processing code should -

  1. detect if a post method form has been submitted.
  2. if there is more than one type of form, detect which form was submitted.
  3. trim all the form data, so that you can detect if all white-space characters where entered.
  4. validate all the input data, setting error messages in an array, using an array index indicating the input the error corresponds to. you would display the contents of the errors array at the appropriate point in the html document.
  5. if there are no errors (the array holding the error messages is empty), use the submitted form data.

btw - once a form has been submitted, except for unchecked check boxes and radio buttons, all form inputs will be set. there's no good reason to have isset() tests for these type of inputs as this will just hide programming mistakes.

Edited by mac_gyver

Share this post


Link to post
Share on other sites

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.