SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Select in foreach loop with 2 variables?!?

    Is this possible?

    I'm trying to make an Month loop in a selectbox which is set default to current date/month:
    PHP Code:
    $currMonth date('m');
    $months = array( 
       
    1=>''._JANUARY.''
          
    ''._FEBRUARY.''
          
    ''._MARCH.''
          
    ''._APRIL.''
          
    ''._MAY.''
          
    ''._JUNE.''
          
    ''._JULY.''
          
    ''._AUGUST.''
          
    ''._SEPTEMBER.''
          
    ''._OCTOBER.''
          
    ''._NOVEMBER.''
          
    ''._DECEMBER.'' 
              
    );

    echo 
    '<select name="matchdate_month">';

    foreach (
    $months AS $m) {
                
        if(
    $m == $currMonth){
            echo 
    '<option value="'.$m.'" selected>'.$m.'</option>';
        } else {
            echo 
    '<option value="'.$m.'">'.$m.'</option>';
        }
            
    }

    echo 
    '</select>'
    As it is now i shows January as default in month and not July as it is now, and it is due to the months is not in "digits" like "date('m')" is (07).

    How do I do that?

    Thanks in advance...

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    What about...
    PHP Code:
    <?php
    echo '<select name="month">';
    for(
    $iMonth 1$iMonth <= 12$iMonth++)
    {
        
    $sMonth date(
            
    'F',
            
    mktime(
                
    null,
                
    null,
                
    null,
                
    $iMonth
            
    )
        );
        
    printf(
            
    '<option value="%1$s" %2$s>%1$s</option>',
            
    $sMonth,
            (
    date('F') === $sMonth) ? 'selected="selected"' ''
        
    );
    }
    echo 
    '</select>';
    /*
    <select name="month">
        <option value="January">
            January
        </option>
        <option value="February">
            February
        </option>
        <option value="March">
            March
        </option>
        <option value="April">
            April
        </option>
        <option value="May">
            May
        </option>
        <option value="June">
            June
        </option>
        <option value="July" selected="selected">
            July
        </option>
        <option value="August">
            August
        </option>
        <option value="September">
            September
        </option>
        <option value="October">
            October
        </option>
        <option value="November">
            November
        </option>
        <option value="December">
            December
        </option>
    </select>
    */
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Many options but this looks easiest.

    $currMonth = date('n') ;

    date
    "n : Numeric representation of a month, without leading zeros : 1 through 12 "

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    More ways ...
    PHP Code:
    $m range(112 );

    foreach ( 
    $m as $k ) {

    $t strtotime'01-' $k '-2009' );
    $month[$k] = '_' strtoupperdate('F'$t ) ) ;

    }

    var_dump$month ) ; 

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2008
    Location
    Gloucestershire, UK
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nothing flash, but day and month:

    PHP Code:

    $currentdate 
    getdate();
                    
    $months = array(
                    
    => 'January',
                    
    'February',
                    
    'March',
                    
    'April',
                    
    'May',
                    
    'June',
                    
    'July',
                    
    'August',
                    
    'September',
                    
    'October',
                    
    'November',
                    
    'December');
                    
    echo 
    '<select class="select" id="day" name="day">';
                    
    for (
    $d 1$d <= 31$d++) {
        echo 
    '<option value="'.$d.'"';
        if (
    $d == $currentdate['mday']) {
            echo 
    ' selected="selected"';
        }
        echo 
    '>'.$d.'</option>';
    }
                    
    echo 
    '</select>';

    //months dropdown
                        
    echo '<select id="month" name="month">';
                        
    foreach (
    $months as $key => $value) {
        echo 
    '<option value="'.$key.'"';
        if (
    $key == $currentdate['mon']) {
            echo 
    ' selected="selected"';
        }
        echo 
    ">$value</option>\n";
    }
                        
    echo 
    "</select>\n"

    Cheers,

    Jon


Bookmarks

Posting Permissions

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