Jump to content

Php Mysql 2 if statements for one hide echo


PNewCode
Go to solution Solved by kicken,

Recommended Posts

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 by PNewCode
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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

Link to comment
Share on other sites

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 by ginerjm
Link to comment
Share on other sites

@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 :)

Link to comment
Share on other sites

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??

Link to comment
Share on other sites

@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 by PNewCode
Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

@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) {
 

1-a-delete.jpg

2-a-delete.jpg

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 by PNewCode
Link to comment
Share on other sites

  • Solution

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 by kicken
  • Like 1
Link to comment
Share on other sites

@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") {

 

Link to comment
Share on other sites

@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!

Link to comment
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.