Populating drop down menu

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

    Populating drop down menu

    I use the following code snippet to build a drop down menu with the
    results of a query. How can I set the initial value of this input based
    on the result of another query?

    What I am trying to do is update records in the data base. The field
    WkEndDate is pulled along with the rest of the record and the drop down
    menu is built from a table of valid WkEndDate values.

    echo ("Weekend Date: <select name='WkEndDate '/> ");

    while ($row = mysql_fetch_arr ay($result2)) {

    echo ("<option value='" . $row["Date"] . "'>" .

    date('F j, Y', strtotime($row["Date"])));

    }

    echo ("</select><br>");

    The initial value in the form is always the first value from result2 and
    I would like it to be the value from result1 (not shown).

    Thanks,

    Mike
  • Pedro Graca

    #2
    Re: Populating drop down menu

    Mike Wilcox wrote:[color=blue]
    > echo ("Weekend Date: <select name='WkEndDate '/> ");[/color]
    // extraneous "/" _______________ ______________^ ___ ?

    (snip)[color=blue]
    > The initial value in the form is always the first value from result2 and
    > I would like it to be the value from result1 (not shown).[/color]

    The echo inside the while should compare result2 to result1 and set the
    appropriate "selected". Something like:


    while($row = mysql_fetch_arr ay($result2)) {
    echo "<option value='", $row["Date"], "'";

    # I'm guessing $result1 was fetched into $row1
    if ($row1['Date'] == $row['Date']) echo " selected";

    echo ">", date('F j, Y', strtotime($row["Date"])), '</option>';
    }
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • Mike Wilcox

      #3
      Re: Populating drop down menu

      Pedro Graca <hexkid@hotpop. com> wrote in
      news:c1d7ul$1hl e73$1@ID-203069.news.uni-berlin.de:
      [color=blue]
      > Mike Wilcox wrote:[color=green]
      >> echo ("Weekend Date: <select name='WkEndDate '/> ");[/color]
      > // extraneous "/" _______________ ______________^ ___ ?
      >
      > (snip)[color=green]
      >> The initial value in the form is always the first value from result2
      >> and I would like it to be the value from result1 (not shown).[/color]
      >
      > The echo inside the while should compare result2 to result1 and set
      > the appropriate "selected". Something like:
      >
      >
      > while($row = mysql_fetch_arr ay($result2)) {
      > echo "<option value='", $row["Date"], "'";
      >
      > # I'm guessing $result1 was fetched into $row1
      > if ($row1['Date'] == $row['Date']) echo " selected";
      >
      > echo ">", date('F j, Y', strtotime($row["Date"])), '</option>';
      > }[/color]

      Sorry to show such a newbie streak.

      So 'selected' makes the particular option selected as 'checked' does in a
      radio button?

      Thanks,

      Mike

      Comment

      • Pedro Graca

        #4
        Re: Populating drop down menu

        Mike Wilcox wrote:[color=blue]
        > Pedro Graca <hexkid@hotpop. com> wrote in
        > news:c1d7ul$1hl e73$1@ID-203069.news.uni-berlin.de:[color=green]
        >> while($row = mysql_fetch_arr ay($result2)) {
        >> echo "<option value='", $row["Date"], "'";
        >>
        >> # I'm guessing $result1 was fetched into $row1
        >> if ($row1['Date'] == $row['Date']) echo " selected";
        >>
        >> echo ">", date('F j, Y', strtotime($row["Date"])), '</option>';
        >> }[/color]
        >
        > Sorry to show such a newbie streak.
        >
        > So 'selected' makes the particular option selected as 'checked' does in a
        > radio button?[/color]

        Right. Read all about it at

        --
        --= my mail box only accepts =--
        --= Content-Type: text/plain =--
        --= Size below 10001 bytes =--

        Comment

        • Mike Wilcox

          #5
          Re: Populating drop down menu

          Pedro Graca <hexkid@hotpop. com> wrote in
          news:c1e6k4$1hi 5v6$1@ID-203069.news.uni-berlin.de:
          [color=blue]
          > Mike Wilcox wrote:[color=green]
          >> Pedro Graca <hexkid@hotpop. com> wrote in
          >> news:c1d7ul$1hl e73$1@ID-203069.news.uni-berlin.de:[color=darkred]
          >>> while($row = mysql_fetch_arr ay($result2)) {
          >>> echo "<option value='", $row["Date"], "'";
          >>>
          >>> # I'm guessing $result1 was fetched into $row1
          >>> if ($row1['Date'] == $row['Date']) echo " selected";
          >>>
          >>> echo ">", date('F j, Y', strtotime($row["Date"])), '</option>';
          >>> }[/color]
          >>
          >> Sorry to show such a newbie streak.
          >>
          >> So 'selected' makes the particular option selected as 'checked' does
          >> in a radio button?[/color]
          >
          > Right. Read all about it at
          > http://www.w3.org/TR/html4/interact/forms.html#h-17.6[/color]

          Pedro,

          Thanks a lot. That code worked after a tried several times and finally
          got it copied in correctly. :)

          Mike

          Comment

          Working...