This is a question I get quite a bit, so I figured I’d write a post on it:
How do I create an HTML select box with options from data in my database?
First, let’s look at our query. Yours might look something like this:
That will select everything in our table and loop it into an array of objects containing the results. Now, we can take that array and create a HTML select box with it.
The HTML Select Box
Now, that we have our array of objects from our database we can use it to populate an HTML select box dynamically with PHP. Here’s how that might look:
That’s it. The code above will loop through the array creating a new option for each element in the array… and populating each option with the desired values from your database. Of course, this is just sample code, you’ll need to work with your database structure and code to make it work for you… but, that should give you the framework for getting this kind of thing done.
Here’s a video walk-through of this, as well:
If you enjoyed this tutorial and want to keep learning, check out my free tutorial site here: https://johnsfreetuts.com