Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Posts
    310
    Thanks
    86
    Thanked 3 Times in 3 Posts

    Values from DB into a dropdown menu?

    Hi Guys,

    Here's my database which shows which type of cards have been issued to a football player in my database:



    The below script looks through all of my records in my database and outputs the type of card issued to each player (stats_id):

    PHP Code:
    <?php

    $databaseInfo 
    mysql_query("SELECT * From player_stats") or die(mysql_error());

    // Update the new values in the database
    if(isset($_POST['Submit']))
    {

    for(
    $i 0$i count($_REQUEST['bks']) ; $i++)
    {
      
    $result mysql_query("Update player_stats set cards ='".$_REQUEST['bks'][$i]."' WHERE stats_id=".$_REQUEST['id'][$i]) OR DIE(mysql_error());
    }

    //end of post

    ?>
            <?php while ($databaseArray mysql_fetch_array($databaseInfo)){ ?>
              <input name="bks[]" type="text" id="bks" value="<?php echo $databaseArray['cards']?>" size="36" />
            <?php // end of while loop ?>
            <input type="submit" name="Submit" value="Submit" class="button">
            <input type="reset" name="Reset" value="Reset" class="button"></td>
    </form>
    </body>
    </html>
    And this works great, and as you can see each value in the database holds the word yellow or red or nothing. So on this line where it outputs the data from the database

    PHP Code:
    <input name="bks[]" type="text" id="bks" value="<?php echo $databaseArray['cards']?>" size="36" />
    I want to change the form element to a dropdown menu rather than a input box.

    With the value in the database automatically selected ie yellow or red or nothing (which says the words please select)

    And then have the option to change those values for each record?

    Hope that's clearer - can you help?

    Thanks

    Chris

  • #2
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    PHP Code:
    echo "<select>"//open the dropdown box tag

    if ( $databaseArray['cards'] =="red") {
          echo 
    "<option>Please Select</option>";
          echo 
    "<option selected=\"selected\">Red</option>";
          echo 
    "<option>Yellow</option>";
    }
    elseif ( 
    $databaseArray['cards'] =="yellow") {
          echo 
    "<option>Please Select</option>";
          echo 
    "<option>Red</option>";
          echo 
    "<option selected=\"selected\">Yellow</option>";
    }
    else { 
          echo 
    "<option selected=\"selected\">Please Select</option>";
          echo 
    "<option>Red</option>";
          echo 
    "<option>Yellow</option>";
    }

    echo 
    "</select>"//close the dropdown box tag 
    WoW I really forgot how to do it. This will work but its not the cleanest way to do it. I might as well used a switch statement. I really can't remember what to do..
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #3
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,043
    Thanks
    19
    Thanked 42 Times in 42 Posts
    PHP Code:
    while($row mysql_fetch_assoc($result))
    {
      
    $selected $row['card'] == $default 'selected = "selected"' '';
      echo 
    '<option value = "'.$row['card'].'" '.$selected.'>'.$row['card'].'</option>';
    ?> </select><BR /> 
    somethin like this


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •