Hi!

I am trying to populate a select box based on a radio button choice with a list of categories from a database. I have the first part working (i.e. the box is populated with the categories), unfortunately though it prints the category name as many times as it appears in the database. I only want it to print the category name once.

I have a feeling this is something to do with a while loop being used but I don't know what to use instead.

I have included the code below to show how I have set this up. Any help would be greatly appreciated.

This is the code in the <HEAD> area:
PHP Code:
<script type="text/javascript">
<!--
<?php 
// connect to the database server 
$dbcnx = @mysql_connect("server""user""password" ); 
if (!
$dbcnx) { 
echo(
"<p>Unable to connect to the " 
"database server at this time.</p>" ); 
exit(); 


// select the nutritional healing database 
if (! @mysql_select_db("database" )) { 
echo(
"<p>Unable to locate the nutritional healing " 
"database at this time.</p>" ); 
exit(); 


// request the contents of the database 
$result = @mysql_query("SELECT * FROM nhreferrals WHERE category='Professional Referrals'" ); 
if (!
$result) { 
echo(
"<p>Error performing query: " mysql_error() . "</p>" ); 
exit(); 


echo( 
"var profrefs = new Array (" ); 
$first true

// display the conditions in the select box 
while ($row mysql_fetch_array($result)) { 
echo( 
$first "\n " ",\n " ); 
echo( 
'"'.$row['country'].'"' ); 
$first false


echo( 
"\n);\n\n" ); 

// request the contents of the database 
$result = @mysql_query("SELECT * FROM nhreferrals WHERE category='Recommended Laboratories'" ); 
if (!
$result) { 
echo(
"<p>Error performing query: " mysql_error() . "</p>" ); 
exit(); 


echo( 
"var reclab = new Array (" ); 
$first true

// display the conditions in the select box 
while ($row mysql_fetch_array($result)) { 
echo( 
$first "\n " ",\n " ); 
echo( 
'"'.$row['country'].'"' ); 
$first false


echo( 
"\n);\n\n" ); 

// request the contents of the database 
$result = @mysql_query("SELECT * FROM nhreferrals WHERE category='Professionals who supervise therapeutic water fasting'" ); 
if (!
$result) { 
echo(
"<p>Error performing query: " mysql_error() . "</p>" ); 
exit(); 


echo( 
"var waterfast = new Array (" ); 
$first true

// display the conditions in the select box 
while ($row mysql_fetch_array($result)) { 
echo( 
$first "\n " ",\n " ); 
echo( 
'"'.$row['country'].'"' ); 
$first false


echo( 
"\n);\n\n" ); 
?>

function fillSelectFromArray(selectCtrl, itemArray) { 
var i; 

// empty existing items 
for (i = selectCtrl.options.length; i >= 0; i--) { 
selectCtrl.options[i] = null; 


if (itemArray != null) { 
// add new items 
for (i = 0; i < itemArray.length; i++) { 
selectCtrl.options[i] = new Option(itemArray[i]); 
selectCtrl.options[i].value = itemArray[i]; 


if ( itemArray.length > 0 ) {
// select first item (prompt) for sub list
selectCtrl.options[0].selected = true;



-->
</script>
And this is the part in the body that actually makes the form:
PHP Code:
<?php
print("<form name='referralSelector' action='referrals.php' method='get'>\n" );
print(
"<h2>Select a category:</h2>\n" );
print(
"<blockquote>\n" );
print(
"<p>\n" );
print(
"<input type='radio' name='category' value='prefrefs' onclick='fillSelectFromArray(document.referralSelector.location, profrefs)' /> Professional Referrals\n" );
print(
"<br />\n" );
print(
"<input type='radio' name='category' value='reclab' onclick='fillSelectFromArray(document.referralSelector.location, reclab)' /> Recommended Laboratories\n" );
print(
"<br />\n" );
print(
"<input type='radio' name='category' value='waterfast' onclick='fillSelectFromArray(document.referralSelector.location, waterfast)' /> Professionals who supervise therapeutic water fasting\n" );
print(
"</p>\n" );
print(
"</blockquote>\n" );
print(
"<h2>Select a location:</h2>\n" );
print(
"<blockquote>\n" );
print(
"<select class='referralLocationSelector' name='location' size='1'>\n" );
print(
"<option value='" $row['country'] . "'>" $row['country'] . "</option>\n" );
print(
"</select>\n" );
print(
"</blockquote>\n" );
print(
"</form>\n" );
?>