rvdb86 Posted July 22, 2012 Share Posted July 22, 2012 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 TIA for any help or suggestions! Link to comment https://forums.phpfreaks.com/topic/266071-comparing-array-element-against-string/ Share on other sites More sharing options...
Barand Posted July 22, 2012 Share Posted July 22, 2012 perhaps if you used "==" instead of "!=" Link to comment https://forums.phpfreaks.com/topic/266071-comparing-array-element-against-string/#findComment-1363436 Share on other sites More sharing options...
rvdb86 Posted July 22, 2012 Author Share Posted July 22, 2012 Sorry, I post the worng code (I was playing around trying different things to try get it working). Also == doesnt work Link to comment https://forums.phpfreaks.com/topic/266071-comparing-array-element-against-string/#findComment-1363437 Share on other sites More sharing options...
Barand Posted July 22, 2012 Share Posted July 22, 2012 try echoing $line['action'] every time so you can see what it contains. Link to comment https://forums.phpfreaks.com/topic/266071-comparing-array-element-against-string/#findComment-1363438 Share on other sites More sharing options...
rvdb86 Posted July 22, 2012 Author Share Posted July 22, 2012 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 https://forums.phpfreaks.com/topic/266071-comparing-array-element-against-string/#findComment-1363457 Share on other sites More sharing options...
PFMaBiSmAd Posted July 22, 2012 Share Posted July 22, 2012 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 https://forums.phpfreaks.com/topic/266071-comparing-array-element-against-string/#findComment-1363462 Share on other sites More sharing options...
ignace Posted July 22, 2012 Share Posted July 22, 2012 Try: if (trim($line['action']) == "wow_displayed") { Edit: Do as instructed by PFMaBiSmAd Link to comment https://forums.phpfreaks.com/topic/266071-comparing-array-element-against-string/#findComment-1363464 Share on other sites More sharing options...
rvdb86 Posted July 22, 2012 Author Share Posted July 22, 2012 Thanks for everyones help! ignace your solution solved the problem! But never the less thanks to everyone for taking the time to reply and help out Link to comment https://forums.phpfreaks.com/topic/266071-comparing-array-element-against-string/#findComment-1363465 Share on other sites More sharing options...
jazzman1 Posted July 22, 2012 Share Posted July 22, 2012 Try: if (trim($line['action']) == "wow_displayed") { Edit: Do as instructed by PFMaBiSmAd OR: if(trim(strip_tags($line['action'])) == "wow_displayed") Link to comment https://forums.phpfreaks.com/topic/266071-comparing-array-element-against-string/#findComment-1363468 Share on other sites More sharing options...
ignace Posted July 22, 2012 Share Posted July 22, 2012 OR: if(trim(strip_tags($line['action'])) == "wow_displayed") That won't do what you think it will: var_dump(strip_tags('[b]foo?[/b]')); // Output: [b]foo?[/b] Link to comment https://forums.phpfreaks.com/topic/266071-comparing-array-element-against-string/#findComment-1363474 Share on other sites More sharing options...
ignace Posted July 22, 2012 Share Posted July 22, 2012 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 https://forums.phpfreaks.com/topic/266071-comparing-array-element-against-string/#findComment-1363475 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.