tags:

views:

96

answers:

2

I have a list of options (booked seats) from which I want to exclude certain values (e.g., 3, 4, 8 and 19). The code I have for constructing the list is:

    <?php
    for ($i=1; $i<=27; $i++)
      {
        echo "<option value=$i>$i</option>";
      }
    ?>

How do I exclude 3, 4, 8 and 19 from the list?

Thanks.

+11  A: 

You can use continue to skip the current iteration of a loop.

$exclude = array(3, 4, 8, 19);

for ($i=1; $i<=27; $i++)
{

    if (in_array($i, $exclude)) continue;

    echo "<option value=$i>$i</option>";
}

Documentation.

alex
it works. thank you so much
andesign
@andesign - Don't forget to mark your question as answered!
webdestroya
of course, thanks.
andesign
sheesh. +10 for such a friggin primitive syntax knowledge. This is really an enthusiast site, and nothing of professional.
Col. Shrapnel
@Col. Shrapnel SO? :P
alex
Well this site pretends to be an "ultimate source of knowledge" but actually it seems more of ultimate source of crap.
Col. Shrapnel
@Col. Shrapnel I'd say it is more of a place for people to get their programming related questions answered. Every great programmer had to start somewhere too.
alex
No great programmer could be grown in such a lame background. This is actually a place for people to get a code snippet to be copy-pasted. Nothing really *programming* related. To become a great programmer requires a shitload of hard work. And this site encourage people not to think nor work. Just ask for free code.
Col. Shrapnel
I learn by copy other code first, then make a new one
andesign
@andesign thank you for your support. You are the perfect example of what I am talking about. You've been told you are doing wrong way, but you prefer not to listen. But just copy-paste. Nuff said.
Col. Shrapnel
@Col. Shrapnel I disagree. I signed up to this site knowing a great deal less than I do know (just look at some of my early questions). The majority of that learning (somewhere around 80%) was done by using Stack Overflow.
alex
I agree with @alex. I gained very good knowledge after to join SO. It's a great site.
metal-gear-solid
A: 

Explicitly exclude them:

if (($i != 3) && ($i != 4) && ($i !=8) && ($i != 19))
Martin York