954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

populating drop down menu from MySQL

hi guys.

i hav this drop down menu, which i need to populate with data from one column of a particular table of MySQL dbase.

i hav read a lot on this, but have not been able to come up wid any code.

any and all help in this regard will be greatly appreciated.

thx a ton.

akshit
Light Poster
46 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

Use this code in your application:

<? 
$query="SELECT sub_cat_id, sub_cat_name FROM category";

/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */

$result = mysql_query ($query);
echo "<select name=category value=''></option>";
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[sub_cat_id]>$nt[sub_cat_name]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box 
?>
Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

I you want to display the second dropdown from first dropdown,then see this thread:
http://www.daniweb.com/forums/thread77281.html

Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

Hi to all
I have a drop down list that is populated from my mysql database, and works fine.
How to eliminate duplicate entries. I have seen the answer a 100 times but cant get it to work.
Here is the snip of code that I have

$result = mysql_query ($query);
echo "<select name=Location value=''>Location</option>";
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[Town]>$nt[Location]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box

mysql_free_result($result) ;
phobia1
Light Poster
25 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

0. $query="SELECT DISTINCT * FROM Garant GROUP BY Location";

Thanks for all that helped and apologies that I upset the moderators

phobia1
Light Poster
25 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

Hello,
I have the drop down box as follows:
Country
<?php echo $HTML['country_options_escape'];?>

how can I display data from mysql, I tried the following but did not work:
Country
<?php $query="select country from countries where active ='YES'";
$result = mysql_query($query, $GLOBALS['DB']);

while($nt=mysql_fetch_array($result)){ //Array or records stored in $nt

$HTML['country_options_escape']=$nt[country];

//;

}

echo $HTML['country_options_escape'];?>

Thanks

rse
Light Poster
28 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

@rse

fix this first, and then repost your codes. Unless, you define it somewhere it would make sense, but right now, I don't see it making any sense at all. Can you please elaborate or shades some light on where this line of codes coming from??

$HTML['country_options_escape']=$nt[country];


The only time I remember writing such arrangement was for smarty array, but not like yours.

this is valid
//while begins here
$something['it_is'] = (string) $so['this_is'];
$something['am_i'] = (string) $so['am_i'];
$assignItHere[] = $something;

//end of while loop

return $assignItHere;

veedeoo
Posting Pro in Training
438 posts since Oct 2011
Reputation Points: 149
Solved Threads: 60
 

@ rse,

Try using this simple example. Maybe, it will help you locate the problems on your codes.

<?php

## we can do it the simpler way, and if it is working properly, we can add more into
## it to make it fancier. For now it is always nice to start in simpler form.

## define your database connection credentials
mysql_connect( $db_host, $db_user, $db_password ) or die(mysql_error());
mysql_select_db( $db_database ) or die(mysql_error());

## make sure that 'YES' can be found as shown on in the country rows under active column
$countries = mysql_query("SELECT * country FROM countries WHERE active='YES'")
or die(mysql_error());

## lets loop the crap out of the toid
while($country = mysql_fetch_array( $countries ))
{

## I am assuming here that your countries table has the following columns
##  id    | name      | active    
##  100   | Bundagle  |  YES 
##  200   | Somewhere |  YES
    $country_id = $country['id'];
    $country_name = $country['name'];
## Now, let's assign the country inside the option form tags.
## the country named bundagle and somewhere should be now inside the option tags.
    $country_block .= '<OPTION value="'.$country_id.'">'.$country_name.'</OPTION>'; 

} ## end while loop

## now lets show the result from the table countries.
?>
<!-- notice the country_block is outside the crap, of which will give you more freedom to modify the html tags without so much clutter-->

<label for="country">Country</label>
<select name="countryID"><?php echo $country_block; ?></select>
veedeoo
Posting Pro in Training
438 posts since Oct 2011
Reputation Points: 149
Solved Threads: 60
 

wonderful thanks

rse
Light Poster
28 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Ok guys I need some help... I am having a similar issue let me tell you a little bit of what I am wanting to do.

I have a MySQL database with my data in it that I want on my web page.

What I need is a table with all of the data that is in that database listed and above it I want drop downs people can select from to filter the table to display just what they are looking for. With each drop down options coming from my data base and depending on what the first drop down had selected...

What I have so far on the MAIN page is this.

<?

$sql="SELECT id, bar_name FROM barlist"; 
$result=mysql_query($sql); 

$options=""; 

while ($row=mysql_fetch_array($result)) { 

    $id=$row["id"]; 
    $thing=$row["bar_name"]; 
    $options.="<OPTION VALUE=\"$id\">$bar_name</option>";
} 
?>

<SELECT NAME=id> 
<OPTION VALUE=0>Choose 
<? echo $options?> 
</SELECT>


and on the data base connection page I have this

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>

<body>
<?php
$q=$_GET["q"];

$con = mysql_connect('DATABASE', 'USER', 'PASSWORD');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("DATABASE", $con);

$sql="SELECT * FROM bar WHERE id = '".$q."'";

$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>Bar Name</th>
<th>Address</th>
<th>Time they open</th>
<th>Time they close</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['bar_name'] . "</td>";
  echo "<td>" . $row['address'] . "</td>";
  echo "<td>" . $row['bar_open'] . "</td>";
  echo "<td>" . $row['bar_close'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>
</body>

</html>


The above code gives me the following errors on the main pageWarning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/40/8248640/html/murfreesboro.php on line 96

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/content/40/8248640/html/murfreesboro.php on line 96

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/40/8248640/html/murfreesboro.php on line 100

I know a lot of reading I am sorry if you guys could help me out I would greatly appreciate it!

jgilliam
Newbie Poster
2 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

No one yet? Veedeoo it would be wonderful if you could break my code down like you did for rse :)

jgilliam
Newbie Poster
2 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You