ohno Posted March 27, 2020 Share Posted March 27, 2020 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! Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/ Share on other sites More sharing options...
ginerjm Posted March 27, 2020 Share Posted March 27, 2020 (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 March 27, 2020 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/#findComment-1575879 Share on other sites More sharing options...
ohno Posted March 27, 2020 Author Share Posted March 27, 2020 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? Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/#findComment-1575881 Share on other sites More sharing options...
ohno Posted March 27, 2020 Author Share Posted March 27, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/#findComment-1575883 Share on other sites More sharing options...
ohno Posted March 27, 2020 Author Share Posted March 27, 2020 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) Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/#findComment-1575884 Share on other sites More sharing options...
Barand Posted March 27, 2020 Share Posted March 27, 2020 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) Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/#findComment-1575886 Share on other sites More sharing options...
ohno Posted March 27, 2020 Author Share Posted March 27, 2020 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']) { Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/#findComment-1575887 Share on other sites More sharing options...
Barand Posted March 27, 2020 Share Posted March 27, 2020 Can't say as you are the only person who knows what, precisely, you are trying to specify. Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/#findComment-1575888 Share on other sites More sharing options...
ohno Posted March 27, 2020 Author Share Posted March 27, 2020 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.... Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/#findComment-1575889 Share on other sites More sharing options...
ginerjm Posted March 27, 2020 Share Posted March 27, 2020 (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 March 27, 2020 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/#findComment-1575892 Share on other sites More sharing options...
ohno Posted March 27, 2020 Author Share Posted March 27, 2020 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). Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/#findComment-1575893 Share on other sites More sharing options...
ginerjm Posted March 27, 2020 Share Posted March 27, 2020 (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 March 27, 2020 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/#findComment-1575895 Share on other sites More sharing options...
ohno Posted March 27, 2020 Author Share Posted March 27, 2020 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.... Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/#findComment-1575896 Share on other sites More sharing options...
Barand Posted March 27, 2020 Share Posted March 27, 2020 (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 March 27, 2020 by Barand Spulling errors 1 Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/#findComment-1575897 Share on other sites More sharing options...
ohno Posted March 27, 2020 Author Share Posted March 27, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/#findComment-1575898 Share on other sites More sharing options...
ginerjm Posted March 27, 2020 Share Posted March 27, 2020 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? Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/#findComment-1575900 Share on other sites More sharing options...
ohno Posted March 27, 2020 Author Share Posted March 27, 2020 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? Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/#findComment-1575902 Share on other sites More sharing options...
Barand Posted March 27, 2020 Share Posted March 27, 2020 My code example defaults to 0 for both of those if they aren't set. 1 Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/#findComment-1575903 Share on other sites More sharing options...
ginerjm Posted March 27, 2020 Share Posted March 27, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/#findComment-1575904 Share on other sites More sharing options...
ohno Posted March 27, 2020 Author Share Posted March 27, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/#findComment-1575905 Share on other sites More sharing options...
ginerjm Posted March 27, 2020 Share Posted March 27, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/#findComment-1575907 Share on other sites More sharing options...
ohno Posted March 27, 2020 Author Share Posted March 27, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/#findComment-1575908 Share on other sites More sharing options...
ohno Posted March 27, 2020 Author Share Posted March 27, 2020 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/#findComment-1575909 Share on other sites More sharing options...
ginerjm Posted March 27, 2020 Share Posted March 27, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/#findComment-1575911 Share on other sites More sharing options...
mac_gyver Posted March 27, 2020 Share Posted March 27, 2020 (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 - detect if a post method form has been submitted. if there is more than one type of form, detect which form was submitted. trim all the form data, so that you can detect if all white-space characters where entered. 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. 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 March 27, 2020 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/310386-or-statement-with-multiple-and-statements/#findComment-1575913 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.