Jump to content

Archived

This topic is now archived and is closed to further replies.

CanMan2004

php if statement question

Recommended Posts

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

[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 help

Thanks

Ed

Share this post


Link to post
Share on other sites
Try this good luck

edited sorry and corrected.
<?

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

echo"done";

}else{

echo"not done!";

}

?>

Share this post


Link to post
Share on other sites
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

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
would this do it in intrest

with $ar being the database value.
<?

$ar="a,b,f";

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

echo"done";

}

?>

Share this post


Link to post
Share on other sites

×

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.