Jump to content

How to properly get data from database and dump it into dropdown menu?


pioneerx01

Recommended Posts

Hi,

 

I have a MySQL database called "2011_database" that has a table called "2011_list." In that table I have fields, amongst others, called "name" and "district."

 

I need to find way to get the data from the table and put them into a drop down list on other PHP page. But they need to be listed as "name - district" on one line.

 

I am PHP beginner and if I understand it correctly there need to be two references to get all the data in all records, a third reference to merge them together with " - " in between; and what eludes me the most, putting them in a drop down menu.

 

Any help is greatly appreciated

Thanks

<?php
mysql_connect('server', 'username', 'password') or die('Can not connect 1');
mysql_select_db('2011_database') or die('Can not connect 2');

$sql = mysql_query("SELECT * FROM 2011_list");

echo "<select name=\"DROPDOWN">\n";
echo "<option value=\"NULL\">Select Value</option>\n";

while($r = mysql_fetch_assoc($sql)){
echo "<option value=\"$row['whatever']\">$row['name']."-".$row['district']</option>\n";
}
echo "</select>";

?>

somthing like that

 

Grrrr - slow typist here

as links...

(psuedo code)

connect to db
$query = "SELECT * FROM 2011_list";
$result = mysql_query($query);
while($row=mysql_fetch_array($result)) {
$display = $row['name'] . " - " . $row['district'];
?>
<a href="url_to_where_ever.php?id=<? echo $row['id']; ?>"><?PHP echo $display; ?></a><br>
<?PHP
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.