Mysql Rows in HTML Option Tag

Let’s say you want to have a simple HTML <select> form as a drop down for rows in a Mysql table. This could be for things like categories, pages, games, anything you want to have in a drop down to navigate to another page or submit a form. What ever the case is, I’m going to make a simple layout for displaying rows returned from a mysql query as <option>’s in a HTML <select> or drop down.

Before we can grab anything from a mysql database we have to connect to it. Find how to connect to a mysql database here.

Let’s make our mysql query first:

[code lang=”php”]

[/code]

This will grab all the rows in “table1” ordered by the value of “id” descending. You can make this query whatever you want whether you want it ordered differently or with other requirements, etc. Now we will just make a <select> inside of a HTML form that holds each of these values as a option.

[code lang=”html”]


Categories

Category:



[/code]

If you have rows returned in you mysql query you will have a HTML drop down that looks like this:

Categories:

This HTML form is being submitted to “index.php?do=nav.” Of course you can point this where ever you want to do whatever you want with it, but let’s say we want to have it direct you to a category with PHP. So we are going to run a function on index.php?do=nav that will redirect the viewer to the category.

[code lang=”php”]

[/code]

Filed under: PHP, Tutorials, Web ProgrammingTagged with: , , ,

6 Comments

  1. This thing is pretty awesome, I was doing something similar once and it ended up well… just like this!

    Thanks for sharing your tips 🙂

  2. Great information here! I have been looking for this everywhere, and I’m happy I finally found it! Keep up the great work.

  3. A very interesting read and a great post alltogether. Would you mind if I posted the same article on my blog (with a reference to your website)?

  4. Hi, I just tried to email you regarding this post but cannot get in touch with you. Please email me if get a moment. Thank you.

  5. I really like your blog. Very good content. Please keep posting such great cotent.


Add a Comment

Your email address will not be published. Required fields are marked *

Comment *
Name *
Email *
Website