Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drop down populated by query 1

Status
Not open for further replies.

shamrox

Programmer
Sep 27, 2001
81
US
How can I get a dropdown menu in a form to be populated by a field of data from my database? Specifically, it would list the unique course id numbers that are in the db. I don't want it to list every item in that field, it would group into uniques. I apologize if I have worded this in a confusing manner.

I appreciate any examples of code.

Thanks
Bill
 
echo &quot;<select name=&quot;whateveryouwant&quot;>
<option selected>Course I.D.s&quot;;

$q = &quot;select distinct courseId from Courses order by CourseId&quot;;

$r = mysql_query($q) or die(mysql_error());

while($w = mysql_fetch_array($r, MYSQL_ASSOC))
{
$cid = $w[&quot;courseId&quot;];
echo &quot;<option>&quot;.$cid;
}
echo &quot;</select>&quot;;


The echo statements would depend on how your generating your HTML and whether you are using this in an include or in the page itself. In genereal, you create the select box in your HTML, break out to PHP to run the query and let PHP generate the options, then, when the options are finished you close the select box and keep going with your HTML.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top