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
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.