[Solved] Can I use an if on foreach in such a case? [closed]


As was said, if you’re merely going to skip the record then you may as well not retrieve them in the first place (and thus incur the overhead for having to extract them into PHP memory, etc):

SELECT * FROM `your_table` WHERE `gender` = 'female' AND `taken` = `available`;

However, if you have a specific reason to do this, you can merely do the following:

foreach ($hotels as $hotel) {
    // skip if the item is not available, logic can be changed if necessary
    if ($hotel->taken >= $hotel->available) continue;

    // do the other work here...
}

I interpreted your conditions a little here, assuming you wanted to skip people who aren’t ‘available’. Though it does look like you wanted the opposite, in which case you can switch the logic in the sql from != to = and in php from != to ==.

Updated: To reflect additional comments made.

1

solved Can I use an if on foreach in such a case? [closed]