Page 1 of 1

Loop to check something is in any one of 10 variables - how?

Posted: Thu Nov 03, 2011 10:16 am
by simonmlewis
Hi

I need to run a loop that checks if any number of about 10 variables has anything in it.

If it find 5 have something in them, then I can action something. But how do I spot those that have something in them, in one query?

It's a list of variables 'colours':
black
orange
green
silver
blue
pink
And some more.

Sometimes only Orange and Green be used, sometimes silver and blue, but it can be completely at random, from a list of about 100 products.

I have checked that there is something in each one, and then put a result into a variabled named $red, $green........
But at the end of it, I need to ask if each one is being used - and then if it is, I can work from there.
It would be something like this:

"variable array - $red, $green $blue.....";
For each one that has something in it, it will be the word "out" or "in".
If "ALL in that array" say "out", then { do this} else {do that}.

Re: Loop to check something is in any one of 10 variables -

Posted: Thu Nov 03, 2011 10:47 am
by mikosiko
if you are using arrays then array_count_values() should help.
http://www.php.net/manual/en/function.a ... values.php

Re: Loop to check something is in any one of 10 variables -

Posted: Thu Nov 03, 2011 10:59 am
by simonmlewis
Good heavens, that's a minefield.
I am not using arrays, I just gave that as an example of how it might happen, as I think arrays store values from multiple variables.

I'm open to any method.
I did just have an idea of querying, if $red == "in" || $orange == "in".... as if ANY if them are 'in', then the answer is given. As I need to know if they are all "out".

So doing it the other way around kind of does the job I think. Rather than querying that they are all "out"... if just one is 'in'.... done!

Re: Loop to check something is in any one of 10 variables -

Posted: Thu Nov 03, 2011 11:31 am
by mikosiko
this is one way to do it using arrays (... minefields are no so bad.. )

Code: Select all

<?php

  // Instead of variables use an array of colors (as many as you want)
   $colors = array("blue"=>0, "red"=>0, "green"=>0, "black"=>0, "orange"=>0);

   // Instead of "out" or "in" you can use simply boolean values (0, 1)   
   $colors['blue']   = 'out';
   $colors['red']    = 'out';
   $colors['green']  = 'in';
   $colors['black']  = 'out';
   $colors['orange'] = 'in';
   
   // just to see the results .. no neccesary in the final code
   var_dump($colors);
   var_dump(array_count_values($colors));
   
   // array_count_values return an array.. 
   $xx = array_count_values($colors);
   
   if ($xx['in'] > 0)  {
     echo "<BR /> there are {$xx['in']} IN's";
   }
?>

Re: Loop to check something is in any one of 10 variables -

Posted: Thu Nov 03, 2011 12:01 pm
by simonmlewis
Thank you.
I had further issues, but in the end, rather than asking for each one, and then checking that each one is OUT - I just asked if ONE of those was 'in'. If one was, then I can assign it as "in stock" as one of the colours are in stock.

Ta.

Re: Loop to check something is in any one of 10 variables -

Posted: Thu Nov 03, 2011 3:36 pm
by pickle
Could you explain your overall goal? IE: It looks like you're checking if any colour is in stock & if so, set the product to be "in stock". Is this the case?

I only ask because it seems this problem as you've described it & your solution are more complicated than they need to be.

Re: Loop to check something is in any one of 10 variables -

Posted: Thu Nov 03, 2011 4:27 pm
by simonmlewis
Ok no problem.
We are checking for stock status. The problem is, we do have one field for stock for certain products, but others have varying colors, so each color field has a stock number from an external company.

If some of those colours are instock, we want the Category pages to show in stock (as it's not completely out of stock).
But if all those colours are "out of stock", we want the Category pages to show it's out.

So what I did - tho it is a long script - is first to check the product in the front loop is one that has colors. If it does, then it checks if Red has anything in the field (the stock code), if it does, it checks to see the stock status of that colour. If it is in stock, it assigns 'in' to $redm if out of stock, it assigns 'out' to $red.

It then does this for all 9 or 10 colours.

At the end of the script, it asks if ANY of those colours are "in". If any one of them is, that product is technically in stock.
IT then assigns "in stock" to a separate field, and on the Category page, that product is shown as "in stock".

Re: Loop to check something is in any one of 10 variables -

Posted: Thu Nov 03, 2011 4:38 pm
by pickle
Is any of this in a database?

Also, you don't need to check each colour - just check until you find one colour in stock, then stop.