Jump to content

Creating Drop-Down list from SQL Data


darkhappy

Recommended Posts

Hi folks, hope you don't mind a newbie question. I have found a lot of posts on this subject but I think on more advanced issues regarding it rather than the basic concept.

 

I have a SQL db with company data (name, street, zip, phone, etc..) and I have a php array that pulls the data from the db and prints out the Company name:

 

 

$qry = "SELECT * FROM companies";
$result = mysql_query($qry)
or die(mysql_error());


$var = "Company Name";
echo "<u>",$var,"</u>"; 
echo "<br><br>";


while ($r = mysql_fetch_array($result)) {
$cname = $r["cname"];
$cstreet = $r["cstreet"];
$cphone = $r["cphone"];
$ccity = $r["ccity"];
$cstate = $r["cstate"];
$czip = $r["czip"];

echo $cname . "<br>";

}

 

 

 

What I am trying to do, is put the company name ($cname) in a drop-down list, so that when the company name is selected, all of the other data for that company displays in a table beneath the list. This is a "View company data" screen. I'm really not sure which direction to go in, any advice would be appreciated.

 

 

 

Thanks,

bobby

Link to comment
https://forums.phpfreaks.com/topic/91402-creating-drop-down-list-from-sql-data/
Share on other sites

<?php
$qry = "SELECT * FROM companies";
$result = mysql_query($qry)
or die(mysql_error());


$var = "Company Name";
echo "<u>",$var,"</u>"; 
echo "<br><br>";

?>
<select name="company">
<?php
while ($r = mysql_fetch_array($result)) {
$cname = $r["cname"];
$cstreet = $r["cstreet"];
$cphone = $r["cphone"];
$ccity = $r["ccity"];
$cstate = $r["cstate"];
$czip = $r["czip"];

?>
<option value="<?php echo $cname; ?>"><?php echo $cname; ?></option>
<?php
}
?>

 

Nested php script with HTML. Simply but ugly. But you can catch the concept well by doing this.

 

Good luck.

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.