6 Replies - 749 Views - Last Post: 12 September 2012 - 10:03 AM Rate Topic: -----

#1 BenignDesign  Icon User is online

  • holy shitin shishkebobs
  • member icon




Reputation: 6005
  • View blog
  • Posts: 10,428
  • 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.

Your help is greatly appreciated.

Is This A Good Question/Topic? 0
  • +

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

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6051
  • View blog
  • Posts: 23,482
  • 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

Was This Post Helpful? 5
  • +
  • -

#3 BenignDesign  Icon User is online

  • holy shitin shishkebobs
  • member icon




Reputation: 6005
  • View blog
  • Posts: 10,428
  • 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!
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3512
  • View blog
  • Posts: 10,137
  • 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);
}

Was This Post Helpful? 6
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,082
  • 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);
}
echo str_replace("{options}",$selections, $menu);


Was This Post Helpful? 1
  • +
  • -

#6 BenignDesign  Icon User is online

  • holy shitin shishkebobs
  • member icon




Reputation: 6005
  • View blog
  • Posts: 10,428
  • 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. :wub:
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,082
  • 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);
}
echo str_replace("{options}",$selections, $menu);


This post has been edited by CTphpnwb: 12 September 2012 - 10:04 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1