Posted By

gdonald on 09/27/06


Tagged

month options days


Versions (?)

PHP month days options


 / Published in: PHP
 

  1. function getDayOptions( $selected )
  2. {
  3. $options = '';
  4.  
  5. for( $x = 1; $x < 32; $x++ )
  6. {
  7. if( strlen( $x ) == 1 ) $x = '0' . $x;
  8.  
  9. $options .= '<option value="' . $x . '"';
  10.  
  11. if( $x == $selected )
  12. {
  13. $options .= ' selected="selected"';
  14. }
  15.  
  16. $options .= '>' . $x . '</option>';
  17. }
  18.  
  19. return $options;
  20. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: joefoo on September 27, 2006
if( strlen( $x ) == 1 ) $x = '0' . $x;

You were looking for this;

sprintf('%02d', $x)

In fact some further use of sprintf it reduces to

function getDayOptions( $selected )
{
    $options = '';
    $optmask = '<option value="%1$02d"%2$s>%1$02d</option>';

    for( $x = 1; $x < 32; $x++ )
    {
        $selstr = $x == $selected ? ' selected="selected"' : '';
        $options .= sprintf($optmask, $x, $selstr);
    }

    return $options;
}

You need to login to post a comment.