Let me try to flesh it out a little. Here's an example of something I wrote that I use that's similar to your problem. Obviously, the javascript that's used is a little different than yours. But you should be able to see, at least conceptually, what's going on here.
<?
$categoryquery = "SELECT DISTINCT category FROM subcategories ORDER BY category";
$result = mysql_query($categoryquery);
echo "<!-- Begin
subcategory = new Array(
new Array(\n";
while (list($kidscat) = mysql_fetch_row($result)) {
echo "new Array(\"Main Page\", 0),\n";
echo "new Array(\"ALL Subcategories\", \"all\")";
$subcatquery = "SELECT subcategory, display FROM subcategories WHERE category =
'$kidscat' ORDER BY display";
$subcatresult = mysql_query($subcatquery);
while (list($subcat, $display) = mysql_fetch_row($subcatresult)) {
echo ",\nnew Array(\"$display\", \"$subcat\")";
}
echo "\n),\nnew Array(\n";
}
echo ")\n);\n";
?>
function fillSelectFromArray(selectCtrl, itemArray, goodPrompt, badPrompt, defaultItem) {
var i, j;
var prompt;
// empty existing items
for (i = selectCtrl.options.length; i >= 0; i--) {
selectCtrl.options[i] = null;
}
prompt = (itemArray != null) ? goodPrompt : badPrompt;
if (prompt == null) {
j = 0;
}
else {
selectCtrl.options[0] = new Option(prompt);
j = 1;
}
if (itemArray != null) {
// add new items
for (i = 0; i < itemArray.length; i++) {
selectCtrl.options[j] = new Option(itemArray[i][0]);
if (itemArray[i][1] != null) {
selectCtrl.options[j].value = itemArray[i][1];
}
j++;
}
// select first item (prompt) for sub list
selectCtrl.options[0].selected = true;
}
}
// End -->
The page source that's generated looks like this (in my case):
<!-- Begin
subcategory = new Array(
new Array(
new Array("Main Page", 0),
new Array("ALL Subcategories", "all"),
new Array("Arts and Crafts", "artsandcrafts"),
new Array("Culinary", "culinary"),
new Array("Dance", "dance"),
new Array("Enrichment", "enrichment"),
new Array("Music", "music"),
new Array("Scouting", "scouting"),
new Array("Sports", "sports"),
new Array("Tutoring", "tutoring")
),
new Array(
new Array("Main Page", 0),
new Array("ALL Subcategories", "all"),
new Array("Art", "art"),
new Array("Crafts", "crafts"),
new Array("Culinary", "culinary"),
new Array("Dance", "dance"),
new Array("Music", "music"),
new Array("Theater", "theater")
),
new Array(
new Array("Main Page", 0),
new Array("ALL Subcategories", "all"),
new Array("Art Camp", "art"),
new Array("Day Camps", "daycamp"),
new Array("Educational", "educational"),
new Array("Group", "groups"),
new Array("Overnight Camps", "overnight"),
new Array("Religious", "religious"),
new Array("Special Needs", "specialneeds"),
new Array("Sports", "sports"),
new Array("Travel", "travel"),
new Array("Wilderness", "wilderness")
),
new Array(
new Array("Main Page", 0),
new Array("ALL Subcategories", "all"),
new Array("Baby Sitting", "babysitting"),
new Array("Daycare/Preschool", "preschool"),
new Array("Foster Care", "foster"),
new Array("Nanny Agencies", "nanny"),
new Array("Special Needs", "specialneeds")
),
new Array(
new Array("Main Page", 0),
new Array("ALL Subcategories", "all"),
new Array("Kid Friendly Restaurant", "restaurant"),
new Array("Kids Eat Free", "eatfree")
),
new Array(
new Array("Main Page", 0),
new Array("ALL Subcategories", "all"),
new Array("Educational Activities", "activity"),
new Array("Enrichment", "enrichment"),
new Array("Libraries", "library"),
new Array("Schools", "school"),
new Array("Teacher Resources", "resource"),
new Array("Tutoring", "tutor")
),
new Array(
new Array("Main Page", 0),
new Array("ALL Subcategories", "all"),
new Array("Animal Adventures", "animal"),
new Array("Attractions", "attraction"),
new Array("Museums", "museum"),
new Array("Nature/Geology", "nature"),
new Array("Overnight", "overnight"),
new Array("Planetariums", "observatory"),
new Array("Seasonal", "seasonal"),
new Array("Technology", "tech"),
new Array("Theater/Arts", "theater"),
new Array("Transportation", "transportation")
),
new Array(
new Array("Main Page", 0),
new Array("ALL Subcategories", "all"),
new Array("Attractions", "attraction"),
new Array("Fun Centers", "funcenter"),
new Array("Museums", "museum"),
new Array("Nature & Geology", "outdoor"),
new Array("Sporting Events", "event"),
new Array("Zoos", "zoo")
),
new Array(
new Array("Main Page", 0),
new Array("ALL Subcategories", "all"),
new Array("Kid Friendly Lodging", "lodging")
),
new Array(
new Array("Main Page", 0),
new Array("ALL Subcategories", "all"),
new Array("Family Memberships", "membership"),
new Array("Kids Clubs", "clubs")
),
new Array(
new Array("Main Page", 0),
new Array("ALL Subcategories", "all"),
new Array("Business Opportunities", "business"),
new Array("Crafts & Classes", "crafts"),
new Array("Event and Party Planners", "planning"),
new Array("Health & Fitness", "health"),
new Array("Home & Garden", "home"),
new Array("Lunch & Outings", "lunch"),
new Array("Maternity", "maternity"),
new Array("Spas & Salons", "spas")
),
new Array(
new Array("Main Page", 0),
new Array("ALL Subcategories", "all"),
new Array("Business Opportunities", "business"),
new Array("Child Safety", "safety"),
new Array("Financial Planning", "financial"),
new Array("Foster Care", "fostercare"),
new Array("Haircuts", "haircuts"),
new Array("Insurance", "insurance"),
new Array("Medical Services", "medical"),
new Array("Mother's Helpers", "mothers"),
new Array("Orthodontics", "orthodontics"),
new Array("Photography", "photography"),
new Array("Special Needs", "specialneeds")
),
new Array(
new Array("Main Page", 0),
new Array("ALL Subcategories", "all"),
new Array("Birthday Gift Ideas", "gift"),
new Array("Cakes/Caterers", "caterer"),
new Array("Entertainers", "entertainer"),
new Array("Event and Party Planners", "planning"),
new Array("Party Packages/Places", "packages"),
new Array("Party Supplies/Rentals", "supplies")
),
new Array(
new Array("Main Page", 0),
new Array("ALL Subcategories", "all"),
new Array("Education", "education"),
new Array("Independent Schools", "independent"),
new Array("Kindergarten/Preschool", "kindergarten"),
new Array("Language", "language"),
new Array("Montessori Schools", "montessori"),
new Array("Private Schools", "private"),
new Array("Teacher Resources", "resource"),
new Array("Tutoring", "tutor")
),
new Array(
new Array("Main Page", 0),
new Array("ALL Subcategories", "all"),
new Array("Book Stores", "book"),
new Array("Clothing Stores", "clothing"),
new Array("Music Stores", "music"),
new Array("Nutrition", "nutrition"),
new Array("Online Stores", "online"),
new Array("Playsets", "playset"),
new Array("Safety Products", "safety"),
new Array("Specialty Shops", "novelty"),
new Array("Toy Stores", "toy")
),
new Array(
new Array("Main Page", 0),
new Array("ALL Subcategories", "all"),
new Array("Baseball", "baseball"),
new Array("Basketball", "basketball"),
new Array("Equestrian", "equestrian"),
new Array("Golf", "golf"),
new Array("Gymnastics", "gymnastics"),
new Array("Martial Arts", "karate"),
new Array("Pro Sports", "prosports"),
new Array("Skating/Hockey", "skating"),
new Array("Skiing/Boarding", "skiing"),
new Array("Soccer", "soccer"),
new Array("Swimming", "swimming"),
new Array("Tennis", "tennis")
),
new Array(
new Array("Main Page", 0),
new Array("ALL Subcategories", "all"),
new Array("Biking", "biking"),
new Array("Camping / Hiking", "camping"),
new Array("Fishing", "fishing"),
new Array("Horseback Riding", "horseback"),
new Array("Rafting / Kayaking", "rafting"),
new Array("Summer Camps", "summercamp"),
new Array("Summer Sports", "sports"),
new Array("Swimming", "swimming")
),
new Array(
new Array("Main Page", 0),
new Array("ALL Subcategories", "all"),
new Array("Field Trips", "fieldtrips"),
new Array("Teachers Education", "workshops"),
new Array("Teaching Supplies", "teachsupplies"),
new Array("Tutoring", "tutoring")
),
new Array(
new Array("Main Page", 0),
new Array("ALL Subcategories", "all"),
new Array("Ice Skating", "skating"),
new Array("Indoor Activities", "indoor"),
new Array("Skiing/ Snowboarding", "skiing"),
new Array("Sledding/ Tubing", "sledding"),
new Array("Snowmobiling", "snowmobiling"),
new Array("Swimming", "swimming")
),
new Array(
)
);
function fillSelectFromArray(selectCtrl, itemArray, goodPrompt, badPrompt, defaultItem) {
var i, j;
var prompt;
// empty existing items
for (i = selectCtrl.options.length; i >= 0; i--) {
selectCtrl.options[i] = null;
}
prompt = (itemArray != null) ? goodPrompt : badPrompt;
if (prompt == null) {
j = 0;
}
else {
selectCtrl.options[0] = new Option(prompt);
j = 1;
}
if (itemArray != null) {
// add new items
for (i = 0; i < itemArray.length; i++) {
selectCtrl.options[j] = new Option(itemArray[i][0]);
if (itemArray[i][1] != null) {
selectCtrl.options[j].value = itemArray[i][1];
}
j++;
}
// select first item (prompt) for sub list
selectCtrl.options[0].selected = true;
}
}
// End -->