Jump to content

Default on dropdown box


wwfc_barmy_army

Recommended Posts

Hello.

I think this is in the right section

I use this php code to get the records from a database and put them in a drop down box:

[code=php:0]
<?php

$result = @mysql_query("SELECT * FROM artist");
echo "<select name='artists'>";
while ($row = mysql_fetch_assoc($result)) {
  echo "  <option value='{$row['artist_id']}'>{$row['name']}</option>";
}
echo "</select>";
print $row;
?>
[/code]

Is there a way to make it so that the drop down box has the last entered name field (the highest artist_id) as it's default selected field?

So if i entered:

ID  Name
1    Red
2    Blue

when i loaded up the page with the dropdown box on it would already have blue selected as default but i could if i wanted select Red, so if it then changed to:

ID  Name
1    Red
2    Blue
3    Green

When i loaded up the page with the dropdown box it would have green selected by default but i could choose Red, or blue if i wanted to.

Is this possible?

Thanks
Link to comment
https://forums.phpfreaks.com/topic/26298-default-on-dropdown-box/
Share on other sites

Of course its possible, but, and Im sorry if this sounds harsh, but if your going to learn to program your going to need to learn to think a problem through.

You can change the order your query returns results by using an ORDER BY clause. You can then use some html to select the first or last record.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.