Jump to content

Comparing Array element against string


rvdb86

Recommended Posts

Hey guys,

 

I feel like a complete NOOB I just dont understand why the if statement that I am trying to use isn't working and I would really appreciate some help!

 

I am looping through an array, and when one of the elements matches a string I would like to output something:

 

Here is my code:

 

while($line = mysql_fetch_array($result)){

		if($line['action'] != "wow_displayed"){
			echo "WE HAVE IT! - ".$line['action'];
			echo "<br />";
		}
}

 

I really don't understand what I am doing wrong  :confused:

TIA for any help or suggestions!

Link to comment
Share on other sites

Hey Barand,

 

Thanks for helping me. I have tried this earlier, and as you can see from the output below it does not seem to work:

Code:

if($line['action'] != "wow_displayed"){
			echo $line['action'];
			echo "<br />";
		}

Output:

wow_displayed 
[b]lower_device[/b] 
raise_device 
videoplaceholder 
lower_device 
switchToObject1 

 

(to get the output I changed the if statement to !=  when it was set to == nothing was outputted.)

Link to comment
Share on other sites

Your actual data likely contains some white-space characters. How did this data originally get inserted into the database table?

 

Use var_dump($line['action']); to display the actual length of each piece of data.

 

Lastly, in general, you should not be looping through a result set from a query to 'find' if the result set contains a particular value. You should have a WHERE clause in your query statement so that the query only returns the row(s) you are interested in.

 

edit: fixed inline-code tag and the missing 'not' in the last paragraph

Link to comment
Share on other sites

ignace your solution solved the problem!

 

Did you read PFMaBiSmAd's post? You should take note to what he said and apply it!

 

Lastly, in general, you should be looping through a result set from a query to 'find' if the result set contains a particular value. You should have a WHERE clause in your query statement so that the query only returns the row(s) you are interested in.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.