www.webdeveloper.com
Results 1 to 6 of 6

Thread: year select

  1. #1
    Join Date
    Mar 2006
    Location
    Canada
    Posts
    1,197

    year select

    Hi I have the below code that putt to year in the past into a select option.

    I want two years in the past, the current year and two years in the future to be offered in the select menu and I want the current year to be the default value

    any idea how to make the changes

    PHP Code:
    <?php
                
    $thisYear 
    date('Y');
    $yearsN range($thisYear 2$thisYear);
    $yearsN array_reverse($yearsN); 

    foreach(
    array_reverse($yearsN) as $value)
    {
      echo 
    '<option ';
      if(
    $value == $year)
      {
        echo 
    " selected='selected'";
      }
      echo 
    ">$value</option>\n";
    }
    ?>
    Kevin

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,388
    I think this is what you're looking for (untested):
    PHP Code:
    <?php            
    $thisYear 
    date('Y');
    $yearsN range($thisYear 2$thisYear 2);
    $yearsN array_reverse($yearsN);

    foreach(
    array_reverse($yearsN) as $value)
    {
      echo 
    '<option ';
      if(
    $value == $thisYear)
      {
        echo 
    " selected='selected'";
      }
      echo 
    ">$value</option>\n";
    }
    ?>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Mar 2006
    Location
    Canada
    Posts
    1,197
    thank you, that did the trick
    Kevin

  4. #4
    Join Date
    Mar 2006
    Location
    Canada
    Posts
    1,197
    how do I set the value for each potion equal to the year
    I trie dthe below but no luck
    PHP Code:
    <?php            
    $thisYear 
    date('Y');
    $yearsN range($thisYear 2$thisYear 2);
    $yearsN array_reverse($yearsN);

    foreach(
    array_reverse($yearsN) as $value)
    {
      echo 
    '<option value ='.$yearsN;
      if(
    $value == $thisYear)
      {
        echo 
    " selected='selected'";
      }
      echo 
    ">$value</option>\n";
    }
    ?>
    Kevin

  5. #5
    Join Date
    Nov 2006
    Posts
    47
    u r missing the quote in the value
    that y it not selecting the value for the year
    if u look at it carefully

  6. #6
    Join Date
    Aug 2006
    Posts
    255
    PHP Code:
    <?php            
    $thisYear 
    date('Y');
    $yearsN range($thisYear 2$thisYear 2);

    echo 
    '<select name="year">';
    foreach(
    $yearsN as $value) {
        echo 
    '<option value="' $value '"';
        if(
    $value == $thisYear) {
            echo 
    ' selected="selected';
        }
        echo 
    "\">$value</option>\n";
    }
    echo 
    '</select>';
    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles