How to get table data in combo box from database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpjawahar
Forum Newbie
Posts: 19
Joined: Thu Jan 12, 2012 6:06 am
Location: Chennai, India

How to get table data in combo box from database

Post by phpjawahar »

Please any one help me on this,
I am new to php.
I want to display my database contents in a combo box. So that users can select only the options i have entered in my database.

advance thanks for your help. Expecting your replies soon.

Jawahar
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: How to get table data in combo box from database

Post by califdon »

You will need to connect to your database, then send a query to the database that will return the data that you will use in your combo box, then loop through the database result set, and as each value is recovered, your PHP code will use that value as it sends (echos or prints) the appropriate HTML to create the <select> and <option> elements that produce the combo box. If this doesn't make sense to you, I suggest that you first write out what HTML code you would like to see in your browser, then see how you could construct this HTML code with PHP, collecting the data from the database, one piece of data at a time.

The customary loop that is used for such a task is something like this:

Code: Select all

$echo "<select name='mychoice'>";
$con = $sql = 'SELECT choice FROM mytable WHERE .......';
$res = mysql_query($sql) or die(mysql_error);
while($row = mysql_fetch_assoc($sql)) {
    echo "<option value='$row['choice']'>$row['choice']</option>";
}
echo "</select>";
Of course you must substitute the appropriate names for such example names as 'mychoice', 'host', 'user', 'password', 'choice', 'mytable'.
phpjawahar
Forum Newbie
Posts: 19
Joined: Thu Jan 12, 2012 6:06 am
Location: Chennai, India

Re: How to get table data in combo box from database

Post by phpjawahar »

Thanks Mr. Califdon. for your reply.

The code is working fine.

One more doubt for me.

I have a form with two fields. One is STATE NAME(combo box) and the second one is CITY NAME(text box).
The user has to select the STATE NAME(combo box) from the form, then enter the CITY NAME(text box).
My question is how to store the CITY NAME entered by the user corresponding to the STATE NAMES, which was selected from the combo box.
When i try this in php the CITY NAME get stored from the first row of my table.

With Regards,
Jawahar
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: How to get table data in combo box from database

Post by califdon »

phpjawahar wrote:I have a form with two fields. One is STATE NAME(combo box) and the second one is CITY NAME(text box).
The user has to select the STATE NAME(combo box) from the form, then enter the CITY NAME(text box).
My question is how to store the CITY NAME entered by the user corresponding to the STATE NAMES, which was selected from the combo box.
When i try this in php the CITY NAME get stored from the first row of my table.
Can you be a little more specific? How are you going to store this data and what else will be stored? I don't quite understand what you are attempting to do. Perhaps if you begin by telling us what your project is, such as "I am developing a user login script, where the user fills in their name and city, and selects their state from a drop-down box, and I will store this in a MySQL database after checking to see if it is a duplicate..." or something like that; then we can help you do that.
Post Reply