I definitely need to explain what I mean by the thread title.
Suppose I have a database table “household_objects” with… a large list of household objects… Sofa, rug, table, frying pan, etc… The purpose of this table is for randomly selecting one of these objects. However, I only want each one to be an option if certain other conditions are met. For example, the row for frying pan is only eligible in the random selection if a variable “$I_like_fried_food == true”. Sofa is only an option if “$livingroom_w * $livingroom_l >= 100”. The conditions do not use the same variables for each row (and may not even use the same number of variables), so it’s not like I can just have a column with a value to test.
So the way I am currently approaching this is the brute-force way. I bring in the whole table (“SELECT * FROM household_objects”, followed up with a fetchall), and then go through an if-statement for each resulting element in the array, and based on the result of the if-statement I either leave the element intact or I unset it. Then at the end of this process I use array_rand to get the random option.
I am aware of another option. I could have a column in the household_objects table that contains the if-statement condition to test, and then use eval() on that to determine if the array element gets unset. This would allow me to have the if-statement embedded in the foreach loop, and thus only have to type it once. But I’ve been told that using eval() almost always means you’re doing something wrong. There is no user input involved anywhere close to this code.
So I just wanted to see how anyone else would go about this problem.