Hi using the following drop down system, the second drop down is populated depending on what is selected in the first.
Now, the user wants to be able to select about 10 files for each article they add, any idea how I could have the following code repeated 10 times without having to paste it 10 times and change some of the names of the selects? Or is that the only way?
Cheers,
Chris
<?
//read the database
$getfilecategory = mysql_query("SELECT FILES_CATEGORIES_SUB.subcategory_id,FILES_CATEGORIES_SUB.subcategory_name,FILES.subid,FILES.title,FILES.fileid
FROM FILES, FILES_CATEGORIES_SUB WHERE FILES.subid=FILES_CATEGORIES_SUB.subcategory_id ORDER BY FILES_CATEGORIES_SUB.subcategory_name ASC");
//write the table
echo
"<table width=\"100%\" border=\"0\" cellspacing=\"5\" cellpadding=\"0\">
<tr>
<td align=\"left\" width=\"50%\" valign=\"top\">";
// write the country's listbox...
echo "Main File Category<br><select name=\"country\" size=\"10\" style=\"width: 100%\" onchange=\"countryselected(this);\">\n";
// write the entry code for the javascript...\n is used to force a new line so the resultant code is more readable
$sJavaScript = "function countryselected(elem){\n for (var i = document.f1.city.options.length; i >= 0; i--){ \n document.f1.city.options[i] = null;\n";
// loop through the database..
$sLastCountry="";
while ( $row2 = mysql_fetch_array($getfilecategory))
{
// is this a new country?
If ($sLastCountry!=$row2["subcategory_name"]){
// if yes, add the entry to the countrys listbox
$sLastCountry = $row2["subcategory_name"];
echo "\n<option value='".$row2["subid"]."' >".$sLastCountry."</option>";
// and add a new section to the javascript...
$sJavaScript = $sJavaScript."}\n"."if (elem.options[elem.selectedIndex].value==".$row2["subid"]."){\n";
}
// and add a new city line to the javascript
$sJavaScript = $sJavaScript."document.f1.city.options[document.f1.city.options.length] = new Option('".$row2["title"]."','".$row2["fileid"]."');\n";
}
// finish the country's listbox
echo "</select></td><td align=\"left\" valign=\"top\">";
// create the city listbox for no selection
echo "Sub File Category<br><select name=\"city\" size=\"10\" style=\"width: 100%\" >";
echo "<option></option>";
echo "</select>";
echo "</td>
</tr>
</table>";
// finish the javascript and write out
$sJavaScript = $sJavaScript."\n}\n}\n";
echo "\n<SCRIPT LANGUAGE=\"JavaScript\">";
echo "\n".$sJavaScript."\n</SCRIPT>\n";
//close the form
?>