View Full Version : Values from DB into a dropdown menu?

07-14-2007, 02:54 PM
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):


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

// Update the new values in the database

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>

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

<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?



07-15-2007, 01:49 AM
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..:(

07-15-2007, 01:54 AM
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