School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,462 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,662 people online right now. Registration is fast and FREE... Join Now!




Populating a dropdown/bombo box from MySQL.

 
Reply to this topicStart new topic

> Populating a dropdown/bombo box from MySQL., How to get your database information into a dropdown box.

Rating  5
Valek
Group Icon



post 21 Mar, 2009 - 01:07 PM
Post #1


Chances are, if you're reading this now, you've been wracking your brain over how to get that pesky combo box to accept your database information. Today, I have a solution for you. I'm going to demonstrate how you can make this happen.

We'll start by assuming we have a table called 'users'. We're using MySQL, and we want a dropdown box containing all of our users from our users table, by name. So, we'll go step by step. I'll assume for the purposes of this tutorial that, prior to this code, the database connection has already been established. The first thing we'll want to do is to run our query to get the information into a result resource. Since we want to display their user names in the dropdown box, we can speed up the operation and use up less memory by pulling only the field that we need.

CODE
// Write out our query.
$query = "SELECT username FROM users";
// Execute it, or return the error message if there's a problem.
$result = mysql_query($query) or die(mysql_error());


Now $result contains the result resource we'll be using to populate our dropdown box.

Fast forward to where we'll actually be doing the HTML for the dropdown box. As you're probably aware, the dropdown box is a select element, and each choice is an option sub-element inside of the select. We can use string concatenation to build our dropdown box, and use the echo construct to output it afterward. We'll name our dropdown box 'users', in keeping with its contents.

CODE
$dropdown = "<select name='users'>";
while($row = mysql_fetch_assoc($result)) {
  $dropdown .= "\r\n<option value='{$row['username']}'>{$row['username']}</option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;


You'll notice that our dropdown box is output containing option elements for all of our users. To control that, make the dropdown box part of a form, and do a select statement based on the value of 'usernames', after it has been cleaned and validated (gotta watch out for spoofed pages), and you can make this a select box that control what information gets passed to the form for modifying information on users in your database.

I hope you find this helpful.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

wrighty
*



post 15 May, 2009 - 02:31 AM
Post #2
Any way of getting the value out of this and storing as a variable?
Go to the top of the page
+Quote Post

Valek
Group Icon



post 20 Jun, 2009 - 08:36 PM
Post #3
Yes. After the form containing the combo box is submitted, simply use $_GET or $_POST, depending on your preference, just as you would for regular form elements.
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/8/09 02:20AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month