Defualt Value for drop down list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jtmphp
    New Member
    • Feb 2008
    • 9

    Defualt Value for drop down list

    I have a drop down/list and am using a php and a mysql table to store my values. I am displaying an update page that already has the user's number stored in it. I want to default their current number value in the dropdown/list. How do I accomplish this? My code is below. What am I missing ?

    <select name="number" class="longfiel d-select" option value="<?echo "$number?>" >

    <option value="">Select Number</option>
    <option value="One">One </option>
    <option value="Two">Two </option>
    <option value="Three">T hree</option>

    </select>
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    [HTML]<select name="number" class="longfiel d-select">

    <option value="" <?php if ($number == NULL) { echo("selected= \"selected\" "); } ?> >Select Number</option>
    <option value="One" <?php if ($number == 1) { echo("selected= \"selected\" "); } ?>>One</option>
    <option value="Two" <?php if ($number == 2) { echo("selected= \"selected\" "); } ?>>Two</option>
    <option value="Three" <?php if ($number == 3) { echo("selected= \"selected\" "); } ?>>Three</option>

    </select>[/HTML]

    I'm no pro, and I've probably made some mistakes just because I am saying this, but your php syntax is not very good. Even if you don't want to do what I wrote, just have a look at what is different between my php code and yours.

    Regards,
    The Servant

    Comment

    • jtmphp
      New Member
      • Feb 2008
      • 9

      #3
      Thanks for the feedback. However, I wouldn't of posted this question if I knew how to handle this.

      Thanks

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        Originally posted by jtmphp
        Thanks for the feedback. However, I wouldn't of posted this question if I knew how to handle this.

        Thanks
        I meant no offense, I started teaching myself php about 3 months ago, so I have a little experience (most of it trial and error)! Is this what you wanted?

        Comment

        • jtmphp
          New Member
          • Feb 2008
          • 9

          #5
          Yes this will work. Thanks for your help

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by jtmphp
            Yes this will work. Thanks for your help
            While theservants does indeed work, a more effective way would be to loop through the options and use an if to check - this way you arent opening and closing the php parser:
            [php]
            <select>
            <option value="">Select one</option>
            <?php
            $_n = 4; // users selected number
            for($_i = 1; $_i < 6; ++$_i)
            {
            if($_i == $_n) // if this is the users number...
            echo "<option value=\"$_i\" selected=\"sele cted\">$_i</option>";
            else
            echo "<option value=\"$_i\">$ _i</option>";
            }
            ?>
            </select>
            [/php]

            Comment

            • TheServant
              Recognized Expert Top Contributor
              • Feb 2008
              • 1168

              #7
              Originally posted by markusn00b
              While theservants does indeed work, a more effective way would be to loop through the options and use an if to check - this way you arent opening and closing the php parser:
              [php]
              <select>
              <option value="">Select one</option>
              <?php
              $_n = 4; // users selected number
              for($_i = 1; $_i < 6; ++$_i)
              {
              if($_i == $_n) // if this is the users number...
              echo "<option value=\"$_i\" selected=\"sele cted\">$_i</option>";
              else
              echo "<option value=\"$_i\">$ _i</option>";
              }
              ?>
              </select>
              [/php]
              Nice! I tried to think of a loop way but still very inexperienced when it comes to that. That's why I stick around here, always learning!

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by TheServant
                Nice! I tried to think of a loop way but still very inexperienced when it comes to that. That's why I stick around here, always learning!
                Indeed, indeed. It's a wonderful place!

                Atleast now you know the logic for making a loop in this circumstance!

                Comment

                Working...