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}.
Loop to check something is in any one of 10 variables - how?
Moderator: General Moderators
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Loop to check something is in any one of 10 variables - how?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Loop to check something is in any one of 10 variables -
if you are using arrays then array_count_values() should help.
http://www.php.net/manual/en/function.a ... values.php
http://www.php.net/manual/en/function.a ... values.php
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: Loop to check something is in any one of 10 variables -
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!
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!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Loop to check something is in any one of 10 variables -
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";
}
?>-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: Loop to check something is in any one of 10 variables -
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.
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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Loop to check something is in any one of 10 variables -
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.
I only ask because it seems this problem as you've described it & your solution are more complicated than they need to be.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: Loop to check something is in any one of 10 variables -
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".
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".
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Loop to check something is in any one of 10 variables -
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.
Also, you don't need to check each colour - just check until you find one colour in stock, then stop.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.