PNewCode Posted August 19, 2023 Share Posted August 19, 2023 (edited) Hello My new task is trying to have 2 different things to look at to echo a phrase. One is to look if the user has a column set to no, and the other is if it contains a phrase. So for example, if BOB has viewspec_pref set to NO, then he will not see any posts that contain the words "And Click" I have tested that all of it is connecting to the database by echo'ing out each column. This line works to hide the message if it says 'And Click' (this is inside the column $message) ////// works for hiding from every user ////// if (strpos($row["message"], 'And Click') == true) { } else { echo $message; } What I need to do is have it for more than just the word Click, AND... also only if the user has $viewspec_pref set to "no" This is what I tried and I get no result at all Also, for some reason I am not seeing any errors (yes I have that turned on and there is nothing in the log.. and the consol is a bit confusing to me) ////// I Tried ////// if (strpos($row["message"], 'And Click') == true) && if ($viewspec_pref == "no") { } else { echo $message; } ////// Also Tried ////// if (strpos($row["message"], 'And Click') == true) && ($viewspec_pref == "no") { } else { echo $message; } ////// AND Also Tried ////// if (strpos($row["message"], 'And Click') == true) && $viewspec_pref == "no" { } else { echo $message; } ////// AND Also Tried ////// if (strpos($row["message"], 'And Click') == true) && $viewspec_pref == "no"; { } else { echo $message; } Edited August 19, 2023 by PNewCode Quote Link to comment https://forums.phpfreaks.com/topic/317208-php-mysql-2-if-statements-for-one-hide-echo/ Share on other sites More sharing options...
ginerjm Posted August 19, 2023 Share Posted August 19, 2023 First thing I would change is this: if (strpos($row["message"], 'And Click') == true) { if (strpos($row["message"], 'And Click') == true) { The strpos function returns an integer result of a boolean of false. It does not return a true value. Sure - any non-zero integer could be interpreted as true but what if your string begins with the target value? The return will be 0 which would not be true. Quote Link to comment https://forums.phpfreaks.com/topic/317208-php-mysql-2-if-statements-for-one-hide-echo/#findComment-1611288 Share on other sites More sharing options...
PNewCode Posted August 19, 2023 Author Share Posted August 19, 2023 @ginerjm Thank you. I added the spaces as you suggested. I'm getting the same result. What I need to do with this is make it so it looks for that and also if the $viewspec_pref == "no". I need both of these to be as so for it to work right, otherwise it's all hidden regardless if the user has yes or no entered Quote Link to comment https://forums.phpfreaks.com/topic/317208-php-mysql-2-if-statements-for-one-hide-echo/#findComment-1611289 Share on other sites More sharing options...
ginerjm Posted August 19, 2023 Share Posted August 19, 2023 (edited) I don't know why my post looks like that. I didn't add any spaces. Your line of code is not doing a proper test, as I explained. OOPS my previous post has a typo. I meant to say "strpos function returns an integer result OR a boolean of false.". (It never returns a boolean of true.) Edited August 19, 2023 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/317208-php-mysql-2-if-statements-for-one-hide-echo/#findComment-1611290 Share on other sites More sharing options...
PNewCode Posted August 19, 2023 Author Share Posted August 19, 2023 @ginerjm Oh I see it's just how it looks when adding a code. You didni't change anything from mine. Still, this isn't the issue. It's working. I know this tends to be a thing where I have to keep saying that, but since it's not the reason for this post at all, can we just move on from weather it works or not? Because trust me, it does. It's on my webpage, published to the website, and it's working. The problem...... is that I need it to ALSO look for $viewspec_pref == "no" ... as I explained I'm sorry but I don't want to focus on a part of this post that I'm not even asking to change or get help for. If it's not broken then why change it? That part that you explained is working. So can I get some help with what this post is about please Quote Link to comment https://forums.phpfreaks.com/topic/317208-php-mysql-2-if-statements-for-one-hide-echo/#findComment-1611291 Share on other sites More sharing options...
ginerjm Posted August 19, 2023 Share Posted August 19, 2023 I cannot move past that line of code since it will give you a wrong result in certain situations. Why would you not listen and think about how you need to change it? And as for the rest of the topic I have no idea what your title even means. "2 if statements for one hide echo"? What the heck does that mean?? Quote Link to comment https://forums.phpfreaks.com/topic/317208-php-mysql-2-if-statements-for-one-hide-echo/#findComment-1611292 Share on other sites More sharing options...
PNewCode Posted August 19, 2023 Author Share Posted August 19, 2023 (edited) @ginerjm sorry that you can't move past it. I don't know how to convince you that it works since I am literally looking at the web page right now and it is hiding the info as needed. You should forget about that because it's working as it should. THAT is NOT the problem. The problem is that I need this to look at TWO if conditions (like I said)1: that it hides the message if it contains the words "And Click" (THIS WORKS) 2: That it only hides this if the user has a value of "no" in the column "$viewspec_pref" (I CANNOT GET THIS PART ADDED... WHICH IS THE ENTIRE POINT OF THIS POST!) See... Number 1 and number 2.... Two if statements! Thats what the heck that means! Edited August 19, 2023 by PNewCode Quote Link to comment https://forums.phpfreaks.com/topic/317208-php-mysql-2-if-statements-for-one-hide-echo/#findComment-1611293 Share on other sites More sharing options...
ginerjm Posted August 19, 2023 Share Posted August 19, 2023 Look at this code and run it. The string exists in both tests but the answer that your code produces is incorrect. //Test 1 echo "Test 1:<br>"; $str = 'And click'; $a = strpos($str,'And click'); echo "str is '$str'<br> "; echo "Search result is $a<br>"; if ($a==true) echo "$a returns true<br>"; else echo "$a did not return true<br>"; //Test 2 echo "<br><br>Test 2:<br>"; $str = 'xAnd click'; echo "str is '$str'<br> "; $a = strpos($str,'And click'); echo "Search result is $a<br>"; if ($a==true) echo "$a returns true<br>"; else echo "$a did not return true<br>"; Quote Link to comment https://forums.phpfreaks.com/topic/317208-php-mysql-2-if-statements-for-one-hide-echo/#findComment-1611294 Share on other sites More sharing options...
PNewCode Posted August 19, 2023 Author Share Posted August 19, 2023 (edited) @ginerjm To help you move on from that... here's the proof THESE ARE SCREEN SHOTS OF THE WEBSITE RIGHT NOW!!! This is how it looks with AND WITHOUT using if (strpos($row["message"], 'And Click') == true) { Now can we PLEASE move on from the part that is not what I even made this post about please??? You keep dwelling on something that isn't an issue. It's like I'm asking how to fix a flat tire, and you ask me why I don't have a spare! Edited August 19, 2023 by PNewCode Quote Link to comment https://forums.phpfreaks.com/topic/317208-php-mysql-2-if-statements-for-one-hide-echo/#findComment-1611295 Share on other sites More sharing options...
ginerjm Posted August 19, 2023 Share Posted August 19, 2023 you have a nice day Quote Link to comment https://forums.phpfreaks.com/topic/317208-php-mysql-2-if-statements-for-one-hide-echo/#findComment-1611296 Share on other sites More sharing options...
Solution kicken Posted August 19, 2023 Solution Share Posted August 19, 2023 (edited) The syntax of an if statement looks like this: if (some condition){ //do stuff } The some condition part could be a single comparison, or multiple comparisons joined with logical operators. Note though, how there needs to be a set of parenthesis surrounding the entire condition. Now, look at your attempt: if (strpos($row["message"], 'And Click') == true) && ($viewspec_pref == "no") { You have your two conditions combined with the logical AND operator. What you're missing through, is the parenthesis that surrounds the entire combined condition. Instead, you put parenthesis around the individual conditions (which is ok, but unnecessary). Add the required parenthesis around the entire condition and you'll have something that is syntactically valid. 28 minutes ago, PNewCode said: You keep dwelling on something that isn't an issue. It is a potential issue. The solution, which was never really pointed out, is that you need to strictly compare against false to determine if a match was found or not. Using a loose comparison against true will fail if your message starts with the target string (since strpos would return zero, and zero is falsely). if (strpos($row["message"], 'And Click') !== false && $viewspec_pref === "no") { It can generally be worth getting in the habit of using the strict comparison (identical) operators whenever possible, as it helps avoid such surprise outcomes. If you are using PHP 8.0 or better, you can avoid this whole problem by using str_contains instead. Edited August 19, 2023 by kicken 1 Quote Link to comment https://forums.phpfreaks.com/topic/317208-php-mysql-2-if-statements-for-one-hide-echo/#findComment-1611297 Share on other sites More sharing options...
PNewCode Posted August 19, 2023 Author Share Posted August 19, 2023 @kicken Thank you so much for that. This explains a lot, however isn't working. The !== false is giving me the same result as == true, but I understand now the need to put it as a strict statement tp make it correct. The problem though, is that even with the following, the condition of "no" is being ignored. If I have no or just empty then it has the same result. Any thoughts? The first part should only work if the viewspec_pref has "no" as the value 6 minutes ago, kicken said: if (strpos($row["message"], 'And Click') !== false && $viewspec_pref === "no") { Quote Link to comment https://forums.phpfreaks.com/topic/317208-php-mysql-2-if-statements-for-one-hide-echo/#findComment-1611298 Share on other sites More sharing options...
kicken Posted August 19, 2023 Share Posted August 19, 2023 If it's not working correctly for you, then you have some other issue besides just your syntax. Maybe your data is not what you think it is, or your condition is not correctly defined. The code correctly identifies data where the message contains 'And Click' and the view spec is no. Quote Link to comment https://forums.phpfreaks.com/topic/317208-php-mysql-2-if-statements-for-one-hide-echo/#findComment-1611299 Share on other sites More sharing options...
PNewCode Posted August 19, 2023 Author Share Posted August 19, 2023 I'm not sure what I'm missing then. If I put in echo $viewspec_pref; I get the output of no or blank (nothing written), depending on what the value is in the db so I'm not sure why that is being ignored. Quote Link to comment https://forums.phpfreaks.com/topic/317208-php-mysql-2-if-statements-for-one-hide-echo/#findComment-1611300 Share on other sites More sharing options...
PNewCode Posted August 19, 2023 Author Share Posted August 19, 2023 @kicken I APOLOGIZE!!! I'm having a total DUH moment. Your suggestion is spot on! The problem was that I was reupling the wrong file, and thats why I didn't see a change. THAT is embarrassing to say the least haha. Thank you for the help and also for explaining it so I can understand. Huge help. THANK YOU! Quote Link to comment https://forums.phpfreaks.com/topic/317208-php-mysql-2-if-statements-for-one-hide-echo/#findComment-1611301 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.