Drop Down Lists

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

    Drop Down Lists

    I have a drop down list with the town options 'Bury' and 'Ipswich' as
    shown below.

    When selecting one of the options, its value is passed to variable
    $townsearch.

    How do I change the drop down list so the option selected last time is
    then the current one shown in the drop down box?


    <body>

    <?php $townsearch = $_get['town']; ?>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">

    <select size="1" name="town">

    <option>ipswich </option>
    <option>bury</option>
    &nbsp;
    </select>&nbsp;

    <input type="submit" value="GO" />

    </form>

    Many thanks

    Alec

  • Rik

    #2
    Re: Drop Down Lists

    Alec wrote:[color=blue]
    > How do I change the drop down list so the option selected last time is
    > then the current one shown in the drop down box?[/color]
    [color=blue]
    > <?php $townsearch = $_get['town']; ?>[/color]

    //euhm, $_GET['town'].....
    [color=blue]
    > <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
    > <select size="1" name="town">[/color]

    <?php
    $towns = array('ipswich' ,'bury', 'eanske');
    foreach($towns as $town){
    $selected = (isset($_GET['town'])&&$_GET['town']==$town)? '
    selected="selec ted"':'';
    print("<option value=\"$town\" $selected>$town </option>");
    }
    [color=blue]
    > <option>ipswich </option>
    > <option>bury</option>
    > </select>
    > <input type="submit" value="GO" />
    > </form>[/color]

    Grtz
    --
    Rik Wasmus


    Comment

    • Alec

      #3
      Re: Drop Down Lists

      EXCELLENT!!!!!

      Many many thanks Rik

      Alec

      Comment

      • TristaSD

        #4
        Re: Drop Down Lists

        Very effifient. Mine is a monster - I did a query inside a query:

        $locationquery = mysql_query ("SELECT * FROM locations");
        $comparequery = mysql_query ("SELECT location FROM reservations WHERE
        id=" . $_GET['id']);
        while ($compareresult = mysql_fetch_arr ay ($comparequery) ) {
        while ($locationrow = mysql_fetch_arr ay ($locationquery )) {
        echo ("<option");
        if ($locationrow['name'] == $compareresult['location']) { //the
        magic happens here
        echo (" selected");
        }
        echo (">$location row[name]</option>");
        }
        }

        Because $comparequery fetches only one value, any way to retrieve it
        BEFORE going into the forst loop?

        Comment

        • David Haynes

          #5
          Re: Drop Down Lists

          Alec wrote:[color=blue]
          > I have a drop down list with the town options 'Bury' and 'Ipswich' as
          > shown below.
          >
          > When selecting one of the options, its value is passed to variable
          > $townsearch.
          >
          > How do I change the drop down list so the option selected last time is
          > then the current one shown in the drop down box?
          >
          >
          > <body>
          >
          > <?php $townsearch = $_get['town']; ?>
          >
          > <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
          >
          > <select size="1" name="town">
          >
          > <option>ipswich </option>
          > <option>bury</option>
          > &nbsp;
          > </select>&nbsp;
          >
          > <input type="submit" value="GO" />
          >
          > </form>
          >
          > Many thanks
          >
          > Alec
          >[/color]

          Here is one way:
          <?php $townsearch = $_GET['town']; ?>
          <body>
          <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET">
          <select name="town">
          <?php
          $choices = array('ipswitch ', 'bury');
          foreach( $choices as $choice ) {
          if( $choice == $townsearch ) {
          printf("<option selected>%s</option>\n", $choice);
          } else {
          printf("<option >%s</option>\n", $choice);
          }
          }
          ?>
          </select>
          <input type="submit" value="GO" />
          </form>

          Here is another:
          <?php $townsearch = $_GET['town']; ?>
          <body>
          <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET">
          <select name="town">
          <?php
          $choices = array('ipswitch ', 'bury');
          foreach( $choices as $choice ) {
          printf("<option %s>%s</option>\n", ($choice == $townsearch) ?
          'selected' : '', $choice);
          }
          ?>
          </select>
          <input type="submit" value="GO" />
          </form>

          -david-

          Comment

          • Rik

            #6
            Re: Drop Down Lists

            TristaSD wrote:[color=blue]
            > Very effifient. Mine is a monster - I did a query inside a query:
            >
            > $locationquery = mysql_query ("SELECT * FROM locations");
            > $comparequery = mysql_query ("SELECT location FROM reservations WHERE
            > id=" . $_GET['id']);
            > while ($compareresult = mysql_fetch_arr ay ($comparequery) ) {
            > while ($locationrow = mysql_fetch_arr ay ($locationquery )) {
            > echo ("<option");
            > if ($locationrow['name'] == $compareresult['location']) {
            > //the magic happens here
            > echo (" selected");
            > }
            > echo (">$location row[name]</option>");
            > }
            > }
            >
            > Because $comparequery fetches only one value, any way to retrieve it
            > BEFORE going into the forst loop?[/color]


            First of all, you don't need the first while loop,. I assume it's only on
            record that has that certain id, so you can just:
            $compareresult = mysql_fetch_arr ay ($comparequery)
            while ($locationrow = mysql_fetch_arr ay ($locationquery )) {
            if ($locationrow['name'] == $compareresult['location']) {
            echo (" selected");
            }
            }

            It's possible in one query:

            if(isset($_GET['id'])&&$_GET['id']!=''){
            $id = mysql_rel_escap e_string(GET['id']);
            $query = "SELECT x.name, IF(x.name=y.loc ation, ' selected=\"sele cted\"',
            '') as `selected` FROM locations x, reservations y WHERE y.id=$id";
            } else {
            $query = "SELECT name, '' as `selected` FROM locations";
            }

            $locationquery = mysql_query($qu ery);
            while ($locationrow = mysql_fetch_arr ay ($locationquery )){
            printf('<option %s>%s</option>', $locationrow['selected'],
            $locationrow['name']);
            }

            I'm not enterily sure it's quicker though... My MySQL is still not as it
            should be.

            Grtz,
            --
            Rik Wasmus


            Comment

            • Rik

              #7
              Re: Drop Down Lists

              David Haynes wrote:[color=blue]
              > printf("<option %s>%s</option>\n", ($choice == $townsearch) ?
              > 'selected' : '', $choice);[/color]

              Damn, didn't know one could nest the () ? : ; syntax like that.. nice.

              Grtz,
              --
              Rik Wasmus


              Comment

              • Rik

                #8
                Re: Drop Down Lists

                Rik wrote:[color=blue]
                > I'm not enterily sure it's quicker though... My MySQL is still not as
                > it should be.[/color]

                And allthough it's rusty, some pointers for efficiency:
                1. If you're not going to use the enumerated array, use mysql_fetc_asso c().
                2. Only use the * in "SELECT * FROM table" if strictly necessary: The less
                fields the faster:

                Querying a 4 field, 900 rows table for all data 600 times:

                Selecting 3 fields:
                7.1682569980621

                Selecting all fields by name:
                7.5627238750458

                Selecting *:
                8.8992791175842

                Grtz,
                --
                Rik Wasmus


                Comment

                • David Haynes

                  #9
                  Re: Drop Down Lists

                  Rik wrote:[color=blue]
                  > David Haynes wrote:[color=green]
                  >> printf("<option %s>%s</option>\n", ($choice == $townsearch) ?
                  >> 'selected' : '', $choice);[/color]
                  >
                  > Damn, didn't know one could nest the () ? : ; syntax like that.. nice.
                  >
                  > Grtz,[/color]

                  Yep!
                  One of my favorites is:

                  printf("aBoolea n is %s\n", $aBoolean ? 'true' : 'false');


                  -david-

                  Comment

                  • Alan Little

                    #10
                    Re: Drop Down Lists

                    Carved in mystic runes upon the very living rock, the last words of Rik
                    of comp.lang.php make plain:
                    [color=blue]
                    > Alec wrote:[color=green]
                    >> How do I change the drop down list so the option selected last time is
                    >> then the current one shown in the drop down box?[/color]
                    >[color=green]
                    >> <?php $townsearch = $_get['town']; ?>[/color]
                    >
                    > //euhm, $_GET['town'].....
                    >[color=green]
                    >> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
                    >> <select size="1" name="town">[/color]
                    >
                    > <?php
                    > $towns = array('ipswich' ,'bury', 'eanske');
                    > foreach($towns as $town){
                    > $selected = (isset($_GET['town'])&&$_GET['town']==$town)? '
                    > selected="selec ted"':'';[/color]

                    The SELECTED attribute is a boolean attribute, and does not require a
                    value (though it can have one)



                    --
                    Alan Little
                    Phorm PHP Form Processor

                    Comment

                    • strawberry

                      #11
                      Re: Drop Down Lists

                      'Bury' or 'Ipswich'? Are these the only options? :-(
                      Surely things aren't that bad.

                      Comment

                      • Rik

                        #12
                        Re: Drop Down Lists

                        Alan Little wrote:[color=blue]
                        > The SELECTED attribute is a boolean attribute, and does not require a
                        > value (though it can have one)
                        >
                        > http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.3.4.2[/color]

                        I'm strictly XHTML at the moment, so if you'd forgive me this quircks :-)

                        Grtz,
                        --
                        Rik Wasmus


                        Comment

                        • Alan Little

                          #13
                          Re: Drop Down Lists

                          Carved in mystic runes upon the very living rock, the last words of Rik of
                          comp.lang.php make plain:
                          [color=blue]
                          > Alan Little wrote:[color=green]
                          >> The SELECTED attribute is a boolean attribute, and does not require a
                          >> value (though it can have one)
                          >>
                          >> http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.3.4.2[/color]
                          >
                          > I'm strictly XHTML at the moment, so if you'd forgive me this quircks :-)[/color]

                          Ah. No criticism intended, just a <!-- --> :)

                          --
                          Alan Little
                          Phorm PHP Form Processor

                          Comment

                          Working...