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.

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 
?>

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) ;

Edited 7 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

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

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

Hello,
I have the drop down box as follows:

<label for="country">Country</label>
<select name="countryID"><?php echo $HTML;?></select>

how can I display data from mysql, I tried the following but did not work:

<label for="country">Country</label>
<select name="countryID"><?php $query="select country from countries where active ='YES'";
$result = mysql_query($query, $GLOBALS);


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


$HTML=$nt[country];


//<option value=$nt[country]></option>;


}


echo $HTML;?></select>

Thanks

Edited 3 Years Ago by Nick Evan: Fixed formatting

@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 = (string) $so;
$something = (string) $so;
$assignItHere[] = $something;

//end of while loop

return $assignItHere;

@ 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>

Edited 5 Years Ago by veedeoo: n/a

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 page

Warning: 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!

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

This article has been dead for over six months. Start a new discussion instead.