Is there a better way to write this if statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Is there a better way to write this if statement

Post 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!
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post 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;
Chambrln
Forum Commoner
Posts: 43
Joined: Tue Dec 02, 2003 10:45 am
Location: Oregon

Post 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.
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post 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.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Good array searching resource page: http://php.net/array_search
Post Reply