Page 1 of 1

Is there a better way to write this if statement

Posted: Fri Dec 05, 2003 12:10 pm
by fariquzeli
I have an if statement that looks through a product database for certain IPs based on a a previous query. I don't know exactly how to look for about 20 of the id numbers so this is what I've come up with:

Code: Select all

<?php
if (($order->products[$i]['id'] == "150") || ($order->products[$i]['id'] == "149") || ($order->products[$i]['id'] == "147") || ($order->products[$i]['id'] == "146") || ($order->products[$i]['id'] == "143") || ($order->products[$i]['id'] == "142") || ($order->products[$i]['id'] == "139") || ($order->products[$i]['id'] == "138") || ($order->products[$i]['id'] == "137") || ($order->products[$i]['id'] == "136") || ($order->products[$i]['id'] == "135") || ($order->products[$i]['id'] == "134") || ($order->products[$i]['id'] == "132") || ($order->products[$i]['id'] == "131") || ($order->products[$i]['id'] == "129") || ($order->products[$i]['id'] == "128") || ($order->products[$i]['id'] == "127") || ($order->products[$i]['id'] == "126") || ($order->products[$i]['id'] == "125") || ($order->products[$i]['id'] == "123") || ($order->products[$i]['id'] == "121") || ($order->products[$i]['id'] == "120") || ($order->products[$i]['id'] == "119") || ($order->products[$i]['id'] == "118") || ($order->products[$i]['id'] == "117")) {
$Imprint_service = "Yes";
}
?>
As of now I do not think it is working. Is there a better way to do this or make this work? I want to look for product ids listed above and if any or all or a few of them is found I need to set that imprint service variable to yes.

any help is greatly appreciated!

Posted: Fri Dec 05, 2003 12:13 pm
by microthick
Create an array that contains all these values.

Then you can use the in_array() function like this:

Code: Select all

if (in_array($order->products&#1111;$i]&#1111;'id'], myArrayOfValues)) &#123;
     $Imprint_service = "Yes"; 
&#125;

Posted: Fri Dec 05, 2003 12:15 pm
by Chambrln
is there any sort of pattern to the numbers you are checking for?

You could do a loop until you find a match and if you do error out or what not and perform you other code.

Posted: Fri Dec 05, 2003 12:17 pm
by fariquzeli
Wow, what a quick response, thanks guys.

There is no pattern, they correspond to products that contain a certain quality. I'll try that and post back here if I have any problems.

Posted: Fri Dec 05, 2003 12:52 pm
by m3mn0n
Good array searching resource page: http://php.net/array_search