Populating "Month" Dropdown Menu

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cover

    #1

    Populating "Month" Dropdown Menu

    I can get the current month to display in a text box by inserting
    value="<? print strftime("%B"); ?>"> into the code for that
    particular text box on a form but how can I populate a drop down with
    names of all the months with the current one being 'default' as it
    would be in the text box example? TIA
  • Richard Conway

    #2
    Re: Populating &quot;Month&quo t; Dropdown Menu

    cover wrote:[color=blue]
    > I can get the current month to display in a text box by inserting
    > value="<? print strftime("%B"); ?>"> into the code for that
    > particular text box on a form but how can I populate a drop down with
    > names of all the months with the current one being 'default' as it
    > would be in the text box example? TIA[/color]

    You want the following:

    <select name="month">

    <option <?php if (date("m")=="01 ") echo "selected" ?>value="01">Ja n</option>

    <option <?php if (date("m")=="02 ") echo "selected" ?>value="02">Fe b</option>

    ... and so on...

    </select>

    Other tidier ways to do it using for loops too if you think about it.

    Comment

    • Justin Koivisto

      #3
      Re: Populating &quot;Month&quo t; Dropdown Menu

      cover wrote:[color=blue]
      > I can get the current month to display in a text box by inserting
      > value="<? print strftime("%B"); ?>"> into the code for that
      > particular text box on a form but how can I populate a drop down with
      > names of all the months with the current one being 'default' as it
      > would be in the text box example? TIA[/color]

      /**
      * generates an HTML select element for the month
      *
      * @param string $name The name of the HTML field
      * @param int $this_month=-1 The month number to have selected. If not
      supplied, current month is used. If 0, no month is selected.
      * @return string
      */
      function get_html_select _month($name='' ,$this_month=-1){
      if(empty($name) ) $name='month';
      if($this_month= =-1) $this_month=dat e('n');
      $months=range(1 ,12);
      $str='<select name="'.$name.' ">'."\n";
      foreach($months as $month){
      $str.=' <option value="'.$month .'"';
      if($month==$thi s_month) $str.=' selected="selec ted"';
      $str.='>'.date( 'F',mktime(0,0, 0,$month,1,2006 )).'</option>'."\n";
      }
      $str.='</select>'."\n";
      return $str;
      }


      --
      Justin Koivisto, ZCE - justin@koivi.co m

      Comment

      • Colin McKinnon

        #4
        Re: Populating &quot;Month&quo t; Dropdown Menu

        Justin Koivisto wrote:
        [color=blue]
        > cover wrote:[color=green]
        >> names of all the months with the current one being 'default' as it
        >> would be in the text box example? TIA[/color]
        >
        > /**
        > * generates an HTML select element for the month
        > *[/color]

        Nice code.

        C.

        Comment

        • Janwillem Borleffs

          #5
          Re: Populating &quot;Month&quo t; Dropdown Menu

          cover wrote:[color=blue]
          > I can get the current month to display in a text box by inserting
          > value="<? print strftime("%B"); ?>"> into the code for that
          > particular text box on a form but how can I populate a drop down with
          > names of all the months with the current one being 'default' as it
          > would be in the text box example? TIA
          >[/color]

          $current_month = strftime("%B");
          print "<select>\n ";
          for ($i = 1; $i < 13 && $m = strftime('%B', mktime(0,0,0,$i )); $i++) {
          if ($m == $current_month) {
          print "<option selected='selec ted'>$m</option>\n";
          } else {
          print "<option>$m </option>\n";
          }
          }
          print "</select>";


          JW


          Comment

          • finalwebsites.com

            #6
            Re: Populating &quot;Month&quo t; Dropdown Menu

            I have one where you can use labels in your own language:

            http://www.finalwebsites.com/snippets.php?id=1

            ;-)

            regards Olaf

            Comment

            • Janwillem Borleffs

              #7
              Re: Populating &quot;Month&quo t; Dropdown Menu

              finalwebsites.c om wrote:[color=blue]
              > I have one where you can use labels in your own language:
              >
              > http://www.finalwebsites.com/snippets.php?id=1
              >
              > ;-)
              >[/color]

              Just prepend:

              setlocale(LC_TI ME, 'nl_NL');

              or any other available locale...


              JW


              Comment

              • Justin Koivisto

                #8
                Re: Populating &quot;Month&quo t; Dropdown Menu

                finalwebsites.c om wrote:[color=blue]
                > I have one where you can use labels in your own language:
                >
                > http://www.finalwebsites.com/snippets.php?id=1
                >
                > ;-)[/color]

                OK, so I modified my version to use setlocale and strftime and now mine
                does the same. ;)

                <?php
                /**
                * Generates an HTML select element for the month. Uses strftime, so
                * obeys the current (or provided) locale. If no month is supplied,
                * current month is used. If 0 is passed, no month is selected.
                *
                * @param string $name The name of the HTML field
                * @param int $this_month=-1 The month number to have selected.
                * @param mixed $locale=0 The locale(s) to use. 2nd parameter to setlocale
                * @return string
                */
                function get_html_select _month($name='' ,$this_month=-1,$locale=0){
                if($locale!==0) {
                // get current locale setting
                $curr_locale=se tlocale(LC_TIME ,0);

                // set to the passed locale
                if(!setlocale(L C_TIME,$locale) ){
                // unable to use that locale on this system
                return FALSE;
                }
                }
                if(empty($name) ) $name='month';
                if($this_month= =-1) $this_month=dat e('n');
                $months=range(1 ,12);
                $str='<select name="'.$name.' ">'."\n";
                foreach($months as $month){
                $str.=' <option value="'.$month .'"';
                if($month==$thi s_month) $str.=' selected="selec ted"';
                $str.='>'.strft ime('%B',mktime (0,0,0,$month,1 ,2006)).'</option>'."\n";
                }
                $str.='</select>'."\n";
                if($locale!==0) {
                setlocale(LC_TI ME,0);
                }
                if(isset($curr_ locale)){
                // set the locale back.
                if(!setlocale(L C_TIME,$curr_lo cale)){
                // this should never happen!
                return FALSE;
                }
                }
                return $str;
                }
                ?>


                --
                Justin Koivisto, ZCE - justin@koivi.co m

                Comment

                • Darkstar 3D

                  #9
                  Re: Populating &quot;Month&quo t; Dropdown Menu

                  And the race is on! :)

                  Comment

                  • finalwebsites.com

                    #10
                    Re: Populating &quot;Month&quo t; Dropdown Menu


                    Janwillem Borleffs wrote:[color=blue]
                    > finalwebsites.c om wrote:[color=green]
                    > > I have one where you can use labels in your own language:
                    > >
                    > > http://www.finalwebsites.com/snippets.php?id=1
                    > >
                    > > ;-)
                    > >[/color]
                    >
                    > Just prepend:
                    >
                    > setlocale(LC_TI ME, 'nl_NL');
                    >
                    > or any other available locale...
                    >
                    >
                    > JW[/color]
                    You're right but this will not work on every server on the same way.

                    Olaf

                    Comment

                    • Janwillem Borleffs

                      #11
                      Re: Populating &quot;Month&quo t; Dropdown Menu

                      Justin Koivisto wrote:
                      [color=blue]
                      > // this should never happen!
                      >[/color]

                      Definitely one of the most seen messages in log files ;-)


                      JW

                      Comment

                      Working...