# Having an idiot moment - why is my loop counting by 2s?

Page 1 of 1

## 6 Replies - 1233 Views - Last Post: 12 September 2012 - 10:03 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=291632&amp;s=e5a7affa7dbdd3222f4f3a6c95e2f211&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 BenignDesign

• holy shitin shishkebobs

Reputation: 7501
• Posts: 12,089
• Joined: 28-September 07

# Having an idiot moment - why is my loop counting by 2s?

Posted 11 September 2012 - 11:50 AM

The code:

```<select name='last_year_attended'>
<option value=''>Select one...</option>
<?php
for (\$start_yr=1971;\$start_yr<2012;\$start_yr++) {
\$end_yr=\$start_yr+1;
echo "<option value='".\$start_yr."-".\$end_yr.">".\$start_yr."-".\$end_yr."</option>";
}
?>
</select>

```

The problem:

I need this thing to give me a dropdown list of years in one-year increments:

1971-1972
1972-1973
1973-1974
1974-1975

and so on and so forth.

Instead, it is giving me a dropdown list of years in two-year increments:

1972-1973
1974-1975
1976-1977
1978-1979

and so on and so forth.

By my logic, \$start_yr is being incremented by one with each iteration of the loop, so why is it echoing two year spans instead of one year spans?

I know this is something simple and my mush mind is just missing it.

Is This A Good Question/Topic? 0

## Replies To: Having an idiot moment - why is my loop counting by 2s?

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: Having an idiot moment - why is my loop counting by 2s?

Posted 11 September 2012 - 12:00 PM

POPULAR

Works for me?

```php -f b9.php
<option value='1971-1972>1971-1972</option>
<option value='1972-1973>1972-1973</option>
<option value='1973-1974>1973-1974</option>
<option value='1974-1975>1974-1975</option>
<option value='1975-1976>1975-1976</option>
<option value='1976-1977>1976-1977</option>
<option value='1977-1978>1977-1978</option>
<option value='1978-1979>1978-1979</option>
<option value='1979-1980>1979-1980</option>
<option value='1980-1981>1980-1981</option>
<option value='1981-1982>1981-1982</option>
<option value='1982-1983>1982-1983</option>
<option value='1983-1984>1983-1984</option>
<option value='1984-1985>1984-1985</option>
<option value='1985-1986>1985-1986</option>
<option value='1986-1987>1986-1987</option>
<option value='1987-1988>1987-1988</option>
<option value='1988-1989>1988-1989</option>
<option value='1989-1990>1989-1990</option>
<option value='1990-1991>1990-1991</option>
<option value='1991-1992>1991-1992</option>
<option value='1992-1993>1992-1993</option>
<option value='1993-1994>1993-1994</option>
<option value='1994-1995>1994-1995</option>
<option value='1995-1996>1995-1996</option>
<option value='1996-1997>1996-1997</option>
<option value='1997-1998>1997-1998</option>
<option value='1998-1999>1998-1999</option>
<option value='1999-2000>1999-2000</option>
<option value='2000-2001>2000-2001</option>
<option value='2001-2002>2001-2002</option>
<option value='2002-2003>2002-2003</option>
<option value='2003-2004>2003-2004</option>
<option value='2004-2005>2004-2005</option>
<option value='2005-2006>2005-2006</option>
<option value='2006-2007>2006-2007</option>
<option value='2007-2008>2007-2008</option>
<option value='2008-2009>2008-2009</option>
<option value='2009-2010>2009-2010</option>
<option value='2010-2011>2010-2011</option>
<option value='2011-2012>2011-2012</option>
```

That's all the code in the loop?

Oh, you're missing a closing ' in there...

This post has been edited by JackOfAllTrades: 11 September 2012 - 12:00 PM

### #3 BenignDesign

• holy shitin shishkebobs

Reputation: 7501
• Posts: 12,089
• Joined: 28-September 07

## Re: Having an idiot moment - why is my loop counting by 2s?

Posted 11 September 2012 - 12:02 PM

That is every ounce of code in the loop.

Here's what I get:

Seriously? A single quote? I KNEW it had to be something simple! Works beautifully now! Thank you! Apparently I just needed a better pair of eyes!

### #4 Dormilich

• 痛覚残留

Reputation: 4184
• Posts: 13,229
• Joined: 08-June 10

## Re: Having an idiot moment - why is my loop counting by 2s?

Posted 12 September 2012 - 06:47 AM

POPULAR

tip: printf() makes the output somewhat easier to recognise:
```\$option = '<option value="%1\$d-%2\$d">%1\$d-%2\$d</option>';
for (\$start_yr = 1971; \$start_yr < 2012; \$start_yr++) {
printf(\$option, \$start_yr, \$start_yr + 1);
}
```

### #5 CTphpnwb

• D.I.C Lover

Reputation: 3778
• Posts: 13,688
• Joined: 08-August 08

## Re: Having an idiot moment - why is my loop counting by 2s?

Posted 12 September 2012 - 08:24 AM

This is why I use this site even with the gimme teh codez requests and the "I just want it to work" attitudes! Occasionally I learn something new.

I'd have done it using just str_replace(), but printf is nicer. Here's how I'd do it now:
```\$menu = <<<HERE
<select name='last_year_attended'>
{options}
</select>

HERE;
\$option = '	<option value="%1\$d-%2\$d">%1\$d-%2\$d</option>
';
\$selections = "	<option value=''>Select one...</option>
";
for (\$start_yr = 1971; \$start_yr < 2012; \$start_yr++) {
\$selections .=sprintf(\$option, \$start_yr, \$start_yr + 1);
}

```

### #6 BenignDesign

• holy shitin shishkebobs

Reputation: 7501
• Posts: 12,089
• Joined: 28-September 07

## Re: Having an idiot moment - why is my loop counting by 2s?

Posted 12 September 2012 - 08:45 AM

I love you guys. I just want you to know that.

### #7 CTphpnwb

• D.I.C Lover

Reputation: 3778
• Posts: 13,688
• Joined: 08-August 08

## Re: Having an idiot moment - why is my loop counting by 2s?

Posted 12 September 2012 - 10:03 AM

It just occurred to me that it might be easier to validate an integer like 1971 than a string like "1971-1972" so you might do it this way:
```\$menu = <<<HERE
<select name='last_year_attended'>
{options}
</select>

HERE;
\$option = '	<option value="%1\$d">%1\$d-%2\$d</option>
';
\$selections = "	<option value=''>Select one...</option>
";
for (\$start_yr = 1971; \$start_yr < 2012; \$start_yr++) {
\$selections .=sprintf(\$option, \$start_yr, \$start_yr + 1);
}