CanMan2004 Posted July 3, 2006 Share Posted July 3, 2006 Hi allI have a field in my database which contains letters such asd,h,u,zI want to write a if statement to print the word "done" if the statement equals any of the letters in the field, I currently have[code]if ($arow['letters'] == 'a,b,f') { print "done"; }[/code]But that has to match what is in the database, where I want to be able to have just one letter in the if statement and if will print if the letter appears anywhere in the field.Does that make sense?Thanks in advance for any helpThanksEd Quote Link to comment https://forums.phpfreaks.com/topic/13595-php-if-statement-question/ Share on other sites More sharing options...
redarrow Posted July 3, 2006 Share Posted July 3, 2006 Try this good luckedited sorry and corrected.<?if ($arow['letters'] == "abf" ) { echo"done"; }else{echo"not done!";}?> Quote Link to comment https://forums.phpfreaks.com/topic/13595-php-if-statement-question/#findComment-52680 Share on other sites More sharing options...
CanMan2004 Posted July 3, 2006 Author Share Posted July 3, 2006 Sorry I think you misunderstood me, basically at the moment I have loads of rows in my database and in the database field called "letters" there is letters seperated by a commas, for example one row hasd,f,gWhat I want to do is run a if statement like the following[code]if ($arow['letters'] == 'a') { print "done"; }[/code]At the moment it will never print the word "done", simply because there is no row with a single value in it such as 'a' or 'g', there is only multiple values such ase,g,jb,d,fa,d,mWhat I want to do is to get the if statement to look for any rows which have the given value in them, ignoring the fact that there is other values in it, the if statement would just look for rows where the value appeared, not match the field value exacly.Does that make more sense?ThanksEd Quote Link to comment https://forums.phpfreaks.com/topic/13595-php-if-statement-question/#findComment-52682 Share on other sites More sharing options...
kenrbnsn Posted July 3, 2006 Share Posted July 3, 2006 Create a temporary array from the value in the variable using the explode() function and then use the in_array() function to check.This could be put into a function if you wanted to.Ken Quote Link to comment https://forums.phpfreaks.com/topic/13595-php-if-statement-question/#findComment-52684 Share on other sites More sharing options...
redarrow Posted July 3, 2006 Share Posted July 3, 2006 would this do it in intrestwith $ar being the database value.<?$ar="a,b,f";if (eregi("a,b,f",$ar)) { echo"done"; }?> Quote Link to comment https://forums.phpfreaks.com/topic/13595-php-if-statement-question/#findComment-52685 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.