Jump to content


Photo

php if statement question


  • Please log in to reply
4 replies to this topic

#1 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 03 July 2006 - 09:53 PM

Hi all

I have a field in my database which contains letters such as

d,h,u,z

I 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

if ($arow['letters'] == 'a,b,f') { print "done"; }

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 help

Thanks

Ed

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 03 July 2006 - 09:59 PM

Try this good luck

edited sorry and corrected.
<?

if ($arow['letters'] == "abf" ) {

echo"done";

}else{

echo"not done!";

}

?>
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 03 July 2006 - 10:07 PM

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 has

d,f,g

What I want to do is run a if statement like the following

if ($arow['letters'] == 'a') { print "done"; }

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 as

e,g,j
b,d,f
a,d,m

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

Thanks

Ed

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 03 July 2006 - 10:12 PM

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

#5 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 03 July 2006 - 10:17 PM

would this do it in intrest

with $ar being the database value.
<?

$ar="a,b,f";

if (eregi("a,b,f",$ar)) {

echo"done";

}

?>
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users