Forum Moderators: coopster

Message Too Old, No Replies

populate combobox / listbox

populate combobox / listbox

         

cbrink

1:59 pm on Jun 21, 2007 (gmt 0)

10+ Year Member



I am new to php/mysql applications and would like to know if it's possible to populate a combobox from a database query? I can't find anything like that in the forum.
I have a list of places that a user entered into the database and want to present that list to him at a later stage through a select box.

Any help will be appreciated.
Cor

eelixduppy

2:36 pm on Jun 21, 2007 (gmt 0)



Welcome to WebmasterWorld!

We have a great library thread on the basics of extracting data from mysql [webmasterworld.com]. Read up on the basics. Once you have them down, to put the content into a 'select', you have to echo each 'option' in the while loop.

cbrink

8:13 am on Jun 22, 2007 (gmt 0)

10+ Year Member



Thank you,
I knew there was something, I will look at the library.

cbrink

9:58 am on Jun 22, 2007 (gmt 0)

10+ Year Member



Hi Again,
I got it working fine but cannot populate my selectbox with the array values. Any ideas on how to do this?

Here is my code:
<?
//make the connection to the database
$connection = @mysql_connect($server, $dbusername, $dbpassword) or die(mysql_error());
$db = @mysql_select_db($db_name,$connection)or die(mysql_error());
//make query to database
$sql ="SELECT course FROM coursetable WHERE user= '$_SESSION[user_name]'";
//$result = @mysql_query($sql,$connection) or die(mysql_error());
$query = mysql_query($sql);

while ($row = mysql_fetch_array($query)) {
echo "<p>",$row['course'];
}
?>
I need to populate my selectbox with ,$row['course']. Is it possible?

Thank you.

jatar_k

12:42 pm on Jun 22, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



of course it is possible, you just need to add it into the loop

echo '<select name="something">';
while ($row = mysql_fetch_array($query)) {
echo '<option value="',$row['course'],'">',$row['course'],'</option>';
}
echo '</select>';

didn't test it but it should be right

cbrink

6:26 pm on Jun 23, 2007 (gmt 0)

10+ Year Member



Thank you for the advice, it works 100%