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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.