MySQL and HTML Dropdowns

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
OldRod
Posts: 1321
Joined: Sun Sep 20, 2009 4:26 pm

MySQL and HTML Dropdowns

Post by OldRod »

I have a dropdown list on my character creation form that is populated from a table in my database. This works fine and it gives me a number corresponding to that item's position on the list when the player selects it.

However, what I would like it to do is give me the ID number from the table that corresponds to the item the player picked. That way I can sort the list alphabetically before putting it in the dropdown.

Is there an easy way to do this or am I going to have to make another query after the player has made his selection?
User avatar
Nihilant
Posts: 47
Joined: Wed Aug 31, 2011 8:24 pm

Re: MySQL and HTML Dropdowns

Post by Nihilant »

OldRod wrote:However, what I would like it to do is give me the ID number from the table that corresponds to the item the player picked. That way I can sort the list alphabetically before putting it in the dropdown.
I'm reading it over and over again and still not sure what you mean there xD

Do you want to list the items in dropdown alphabetically, or do you want to make it so that the selection from dropdown actually hands you the name/id of the table to query again?
User avatar
OldRod
Posts: 1321
Joined: Sun Sep 20, 2009 4:26 pm

Re: MySQL and HTML Dropdowns

Post by OldRod »

I have a table with 2 fields: ID and Name

Simplified example: let's say I have the following in my table:

ID, Name
2, Dwarf
3, Elf
5, Human
4, Gnome
1, Dryad

I populate the dropdown with the name field, so the dropdown contains: Dwarf, Elf, Human, Gnome, Dryad

As it is now, when I click on Human, the dropdown gives me a value of 3, since it is in the 3rd position in the list. What I want is for it to give me the 5 which represents the ID from the table corresponding to Human. That way I can sort the list alphabetically and still get the proper ID number to look up other information later. This is a simplified example, as the data in my list is much longer, but hopefully that shows what I am looking for :)
User avatar
Nihilant
Posts: 47
Joined: Wed Aug 31, 2011 8:24 pm

Re: MySQL and HTML Dropdowns

Post by Nihilant »

Well looks like you can pass races' IDs to dropdown list and use them for 'value' of each option in list?

Don't know if you display php results in html page that has the dropdown or you build the dropdown itself with php, but anyway you can select both the IDs and Names of races and build the dropdown menu in a while loop:
(assuming you have the $result query from selecting the 2):

Code: Select all

echo "<select name='ChosingRace'>";
    while ($row = mysql_fetch_array($result)) {
        echo "<option value=' ".$row ['ID']." '>".$row ['Race']."</option>";
    }
echo "</select>";
Thus you can pass the ID to php when player selects the race.

EDIT: Hm, while I think of it, why would you need the ID anyway? You can get data from the corresponding table's row (races) by using the Name (column) as passed from dropdown itselt. I mean, you can select Nth column in 'Human' row by referring to it via Human's ID column, but you can do the same by referring to the row using the race Name (Human) lol
User avatar
OldRod
Posts: 1321
Joined: Sun Sep 20, 2009 4:26 pm

Re: MySQL and HTML Dropdowns

Post by OldRod »

Ah, didn't know you could do it that way. It's working now, thanks :)
User avatar
Nihilant
Posts: 47
Joined: Wed Aug 31, 2011 8:24 pm

Re: MySQL and HTML Dropdowns

Post by Nihilant »

Np, I noticed I have some weird blank spaces in that code there lol XD
User avatar
OldRod
Posts: 1321
Joined: Sun Sep 20, 2009 4:26 pm

Re: MySQL and HTML Dropdowns

Post by OldRod »

I'm using CodeIgniter anyway, so that code isn't exactly what I ended up with, but it works, so thanks again :)
Post Reply

Return to “Advanced Help and Support”