auto select actual month in dropdown box

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

    #1

    auto select actual month in dropdown box

    Hi,

    How can I do this,
    I've got a little form with a dropdown.

    <select name="maand">
    <option value='1'>janua ri</option>
    <option value='2'>febru ari</option>
    <option value='3'>maart </option>
    <option value='4'>april </option>
    <option value='5'>mei</option>
    <option value='6'>juni</option>
    <option value='7'>juli</option>
    <option value='8'>augus tus</option>
    <option value='9'>septe mber</option>
    <option value='10'>okto ber</option>
    <option value='11'>nove mber</option>
    <option value='12'>dece mber</option>
    </select>


    I would like to select the current month automatically,
    but don't know how to do this?
    I would start like this (I think)

    $maand = S_POST["maand"];
    $maandhuidig = date(n);
    if($maandhuidig == $maand){
    echo?....don't know...}

    Many tnx
    --
    Running GNU/Linux
    yvesdm[AT]telenet[dot]be | GnuPG: 0xD590F022
    Jabber: yvesdm[AT]jabber.belnet.b e
    "If windows is the solution, plz give me the problem back!"
  • Matthew White

    #2
    Re: auto select actual month in dropdown box

    "YvesDM" <no@way.invalid .bewrote in message
    news:LROni.5493 $kZ1.104551@pho bos.telenet-ops.be...
    Hi,
    >
    How can I do this,
    I've got a little form with a dropdown.
    >
    <select name="maand">
    <option value='1'>janua ri</option>
    <option value='2'>febru ari</option>
    <option value='3'>maart </option>
    <option value='4'>april </option>
    <option value='5'>mei</option>
    <option value='6'>juni</option>
    <option value='7'>juli</option>
    <option value='8'>augus tus</option>
    <option value='9'>septe mber</option>
    <option value='10'>okto ber</option>
    <option value='11'>nove mber</option>
    <option value='12'>dece mber</option>
    </select>
    >
    >
    I would like to select the current month automatically,
    but don't know how to do this?
    I would start like this (I think)
    >
    $maand = S_POST["maand"];
    $maandhuidig = date(n);
    if($maandhuidig == $maand){
    echo?....don't know...}
    >
    Many tnx
    --
    Running GNU/Linux
    yvesdm[AT]telenet[dot]be | GnuPG: 0xD590F022
    Jabber: yvesdm[AT]jabber.belnet.b e
    "If windows is the solution, plz give me the problem back!"
    The option tag has an attribute of selected. This needs to be set as "yes"
    for it to be the selected option. I would make the months/numbers an array,
    and then use a while loop to echo them all out. Then, if the month is the
    current month, add the option tag with the selected="yes" attribute.

    Matt

    Comment

    • YvesDM

      #3
      Re: auto select actual month in dropdown box

      Matthew White wrote:
      The option tag has an attribute of selected. This needs to be set as
      "yes"
      for it to be the selected option. I would make the months/numbers an
      array,
      and then use a while loop to echo them all out. Then, if the month is the
      current month, add the option tag with the selected="yes" attribute.
      >
      Matt
      Hi Matt tnx for your answer.
      I already resolved this.
      My solution is not 100% clean, but it does what I want :-)
      I just an extra option on top of the list which has the selected tag
      and always takes the value of the current month ;-)

      As simple as this:

      $monthnow = date("n");
      echo <<<EOM
      <html>
      <form method="post">
      <table>
      <select name="maand">
      <option value='$monthno w'selected>Curr ent month</option>
      <option value='1'>janua ri</option>
      <option value='2'>febru ari</option>
      <option value='3'>maart </option>
      <option value='4'>april </option>
      <option value='5'>mei</option>
      <option value='6'>juni</option>
      <option value='7'>juli</option>
      <option value='8'>augus tus</option>
      <option value='9'>septe mber</option>
      <option value='10'>okto ber</option>
      <option value='11'>nove mber</option>
      <option value='12'>dece mber</option>
      </select>


      Kind regards,
      Yves
      --
      Running GNU/Linux
      yvesdm[AT]telenet[dot]be | GnuPG: 0xD590F022
      Jabber: yvesdm[AT]jabber.belnet.b e
      "If windows is the solution, plz give me the problem back!"

      Comment

      • Michael Fesser

        #4
        Re: auto select actual month in dropdown box

        ..oO(YvesDM)
        >I already resolved this.
        >My solution is not 100% clean, but it does what I want :-)
        >I just an extra option on top of the list which has the selected tag
        >and always takes the value of the current month ;-)
        Ugly. Why not just let PHP print out all the options in a simple loop?
        You could even let it generate the month names automatically if you want
        (assuming proper locale support).
        <option value='$monthno w'selected>Curr ent month</option>
        There has to be a space between attributes. Additionally it's better
        style to not use minimized attributes, but to write them according to
        the stricter XHTML rules. This makes it easier to upgrade in the future.
        So the result should look like

        <option value='7' selected='selec ted'>Juli</option>

        Micha

        Comment

        • Joe Scylla

          #5
          Re: auto select actual month in dropdown box

          YvesDM wrote:
          Hi,
          >
          How can I do this,
          I've got a little form with a dropdown.
          >
          <select name="maand">
          <option value='1'>janua ri</option>
          <option value='2'>febru ari</option>
          <option value='3'>maart </option>
          <option value='4'>april </option>
          <option value='5'>mei</option>
          <option value='6'>juni</option>
          <option value='7'>juli</option>
          <option value='8'>augus tus</option>
          <option value='9'>septe mber</option>
          <option value='10'>okto ber</option>
          <option value='11'>nove mber</option>
          <option value='12'>dece mber</option>
          </select>
          >
          >
          I would like to select the current month automatically,
          but don't know how to do this?
          I would start like this (I think)
          >
          $maand = S_POST["maand"];
          $maandhuidig = date(n);
          if($maandhuidig == $maand){
          echo?....don't know...}
          >
          Many tnx
          imho a better solution. Our lovly god gave us arrays:

          <code>
          $months = array
          (
          1 ="januari",
          2 ="februari",
          3 ="maart",
          4 ="april",
          5 ="mei",
          6 ="juni",
          7 ="juli",
          8 ="augustus",
          9 ="september" ,
          10 ="oktober",
          11 ="november",
          12 ="december"
          );
          $selected = date("n");
          echo "<select>";
          foreach ($months as $i =$v)
          {
          echo "<option value=\"" . $i . "\"";
          if ($i == $selected) echo " selected=\"sele cted\"";
          echo ">" . $v . "</option>";
          }
          echo "</select>";
          </code>

          Comment

          • YvesDM

            #6
            Re: auto select actual month in dropdown box

            Michael Fesser wrote:
            Ugly.
            I know, but I'm pretty new to all this.
            Also, this is just a simple form on my intranet to check payments.
            (ok, i admit, no excuse)
            Why not just let PHP print out all the options in a simple loop?
            You could even let it generate the month names automatically if you want
            (assuming proper locale support).
            I will definately try this way after the php/mysql course i will be taking
            in september :-)
            There has to be a space between attributes. Additionally it's better
            style to not use minimized attributes, but to write them according to
            the stricter XHTML rules. This makes it easier to upgrade in the future.
            So the result should look like
            <option value='7' selected='selec ted'>Juli</option>
            Ok, noted that, tnx.

            Kind regards
            Yves
            --
            Running GNU/Linux
            yvesdm[AT]telenet[dot]be | GnuPG: 0xD590F022
            Jabber: yvesdm[AT]jabber.belnet.b e
            "If windows is the solution, plz give me the problem back!"

            Comment

            • Michael Fesser

              #7
              Re: auto select actual month in dropdown box

              ..oO(Joe Scylla)
              >foreach ($months as $i =$v)
              > {
              > echo "<option value=\"" . $i . "\"";
              > if ($i == $selected) echo " selected=\"sele cted\"";
              > echo ">" . $v . "</option>";
              > }
              HTML also allows single quotes (avoids escaping) and variables can be
              embedded directly into the string (avoids concatenation).

              Just my 2 cents

              Micha

              Comment

              • Joe Scylla

                #8
                Re: auto select actual month in dropdown box

                Michael Fesser wrote:
                .oO(Joe Scylla)
                >
                >foreach ($months as $i =$v)
                > {
                > echo "<option value=\"" . $i . "\"";
                > if ($i == $selected) echo " selected=\"sele cted\"";
                > echo ">" . $v . "</option>";
                > }
                >
                HTML also allows single quotes (avoids escaping) and variables can be
                embedded directly into the string (avoids concatenation).
                >
                Just my 2 cents
                >
                Micha
                it's just my coding style to use only double quotes and never embedd
                variables into string.

                While theres no difference in using double or single quotes - but
                concatenation is much faster than embedd variables into strings.

                Comment

                • Michael Fesser

                  #9
                  Re: auto select actual month in dropdown box

                  ..oO(Joe Scylla)
                  >it's just my coding style to use only double quotes
                  I've seen horrific code with a lot of escaping, concatenation, single-
                  and double quotes mixed within to build a complex SQL query. It can be
                  really hard to find a missing quote in such a mess, but YMMV.
                  >While theres no difference in using double or single quotes - but
                  >concatenatio n is much faster than embedd variables into strings.
                  Not necessarily. In the UCNs on php.net you'll find "benchmarks " that
                  state one or the other

                  And performance is hardly ever a reason when printing out something.
                  BTW, the fastest would probably be an echo with a comma-separated list
                  of arguments.

                  Micha

                  Comment

                  • Andy Hassall

                    #10
                    Re: auto select actual month in dropdown box

                    On Fri, 20 Jul 2007 18:54:25 +0200, Michael Fesser <netizen@gmx.de wrote:
                    >>While theres no difference in using double or single quotes - but
                    >>concatenati on is much faster than embedd variables into strings.
                    >
                    >Not necessarily. In the UCNs on php.net you'll find "benchmarks " that
                    >state one or the other
                    It should probably be mentioned that PHP 5.2.3 included optimisations in the
                    way PHP parses and evaluates interpolated strings and heredocs, so any
                    differences are quite possibly even less noticable in versions from now on.
                    >And performance is hardly ever a reason when printing out something.
                    Yep. On a related note, I've recently been using xdebug's profiler option,
                    feeding the data into kcachegrind - this works quite nicely to find the real
                    bottlenecks in PHP code:



                    --
                    Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
                    http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

                    Comment

                    • Rik

                      #11
                      Re: auto select actual month in dropdown box

                      On Fri, 20 Jul 2007 20:24:39 +0200, Andy Hassall <andy@andyh.co. ukwrote:
                      Yep. On a related note, I've recently been using xdebug's profiler
                      option,
                      feeding the data into kcachegrind - this works quite nicely to find the
                      real
                      bottlenecks in PHP code:
                      >
                      http://xdebug.org/docs/profiler
                      Ooh, very nice, appreciate it!

                      --
                      Rik Wasmus

                      Comment

                      Working...