我在網上搜索相當長的一段時間以找到合適的解決方案,但迄今爲止我還沒有成功。創建動態下拉列表
我試圖達到的目標:我創建了一個省下載的動態下拉框。只要用戶選擇其中一個動態創建的條目,區域的第二個動態下拉框應該僅根據所選省份顯示正確的條目。
爲此我已經建立了下面的代碼:
的search.php
//$i is running up to the max amount of provinces
//for each province there shall be one option in the html select
//the session array provinceresults contains key1, key2, province (english), province (other language)
for($i=0; $i < $countProvinces; $i++) {
echo "<option value=".$_SESSION['provincesresults'][$i][0]." onClick=\"removeSelected(\".$i.\")\">".$_SESSION['provincesresults'][$i][1]."</option>";
}
在search.php中我有應得到執行下面的JavaScript代碼的主體部分時,我選擇的一個每的onClick上述生成的選項:
<!--Javascript which is needed to call the dynamic_drop_down.php function per onClick event-->
<script type=\"text/javascript\">
function removeUnselected($key){
document.getElementById(\"php_code\").innerHTML=\"
<?php
//Get all the values out of the district array out of pdo.vhd
$districtsSelected = removeUnselected(".$_SESSION['provincesresults'][$key][0].");
}
?>\";
}
所以我需要把第一個PHP部分的$ i計數器放到Javascript函數中。 之後,我需要在我的search.php
填充$ districtsSelected爲進一步使用我的search.php PHP函數removeUnselected(「。$ _ SESSION ['provincesresults'] [$ key] [0]。」);看起來是這樣的,幷包含在search.php中:
<?php
/**
*This php file is used to retrieve the districts for a selected province.
*This file is called by search.php.
*As input, the function needs to be called with the ID of the selected province, it will return all related districts.
*/
function removeUnselected($provinceKey) {
//Build the array which returns the needed districts
$districtsSelected = array();
//Loop through all the districts that have been read out of the database by get_value_help.php
for($i = 0; $i < count($_SESSION['districtsresults']); $i++) {
//Check if the province id out of the table dbo.vhp matches with the id out of the table dbo.vhd
if(strcmp($provinceKey, $_SESSION['districtsresults'][$i][1]) == 0) {
//Give back the district names in english if the language settings are english
if($_SESSION['lng'] == "english") {
//Remove any districts that were not selected
$districtsSelected[] = $_SESSION['districtsresults'][$i][2];
}
//Give back the district names in khmer if the language settings are khmer
elseif ($_SESSION['lng'] == "khmer") {
//Remove any districts that were not selected
$districtsSelected[] = $_SESSION['districtsresults'][$i][3];
}
}
}
//Return the result
return($districtsSelected);
}
?>
我如何能在工作方式構建這個任何建議高度讚賞!
謝謝和問候,
codac
編輯: 我使用下面的JavaScript: http://www.mattkruse.com/javascript/dynamicoptionlist/index.html
我現在有兩個問題: 由於有24個省,86個區, 1600個公社和13500個村莊,爲下拉框創建需要幾秒鐘的時間。
的表格如下所示(省):
country_id province_id province_en province_kh
000000 010000 Banteay Mean Chey Khmer 1
000000 020000 Bat Dambang Khmer 2
000000 030000 Kampong Cham Khmer 3
000000 040000 Kampong Chhnang Khmer
(區) province_id district_id district_en district_kh 010000 010200 Mongkol博瑞高棉1 010000 010300普農Srok高棉2 010000 010400柏威夏NETR柏威夏高棉3 010000 010500 Ou Chrov高棉4
...同樣的公社和村莊。
我通過獲取值了微軟的SQL Server:
$sqlProvinces = "SELECT country_id, province_id, province_en, province_kh FROM dbo.vhp";
$sqlDistricts = "SELECT province_id, district_id, district_en, district_kh FROM dbo.vhd";
...同爲鄉鎮和村莊。
就像在我的評論中提到,我存儲在$ _SESSION這些值:
$_SESSION["provincesresults"]=$provincesResults;
$_SESSION["districtsresults"]=$districtsResults;
之後,我使用的功能「dyndrpdwn。PHP的 「生成下拉列表:
<?php
function dyndrpdwn() {
//count the number of provinces, districts, communes and villages for the counters of the for-statements
$countProvinces = count($_SESSION["provincesresults"]);
$countDistricts = count($_SESSION["districtsresults"]);
$countCommunes = count($_SESSION["communesresults"]);
$countVillages = count($_SESSION["villagesresults"]);
//NULL the return values
$returnPD = NULL;
$returnC = NULL;
$returnV = NULL;
$defaultP = NULL;
$defaultD = NULL;
$defaultC = NULL;
$defaultV = NULL;
//Set start value for the counters to 0
$j = 0;
$k = 0;
$l = 0;
//Start the JavaScript and create the DynamitOptionList
$returnPD = "<script type=\"text/javascript\">
var makeGeo = new DynamicOptionList(\"provinces\",\"districts\",\"communes\",\"villages\");";
//Loop through all Provinces
for($i = 0; $i < $countProvinces; $i++) {
//Create the optionlist for provinces
$returnPD = $returnPD."makeGeo.forValue(\"".$_SESSION["provincesresults"][$i][1]."\").addOptionsTextValue(";
//Create the optionlist for districts ("while" if more performant than "for"!) and make sure that counter is not getting out of index
while($j < $countDistricts && $_SESSION["provincesresults"][$i][1] == $_SESSION["districtsresults"][$j][0]) {
$returnPD = $returnPD."\"".$_SESSION["districtsresults"][$j][2]."\",\"".$_SESSION["districtsresults"][$j][1]."\",";
$returnC = $returnC."makeGeo.forValue(\"".$_SESSION["provincesresults"][$i][1]."\").forValue(\"".$_SESSION["districtsresults"][$j][1]."\").addOptionsTextValue(";
//Create the optionlist for communes ("while" if more performant than "for"!) and make sure that counter is not getting out of index
while($k < $countCommunes && $_SESSION["districtsresults"][$j][1] == $_SESSION["communesresults"][$k][0]) {
$returnC = $returnC."\"".$_SESSION["communesresults"][$k][2]."\",\"".$_SESSION["communesresults"][$k][1]."\",";
$returnV = $returnV."makeGeo.forValue(\"".$_SESSION["provincesresults"][$i][1]."\").forValue(\"".$_SESSION["districtsresults"][$j][1]."\").forValue(\"".$_SESSION["communesresults"][$k][1]."\").addOptionsTextValue(";
//Create the optionlist for villages ("while" if more performant than "for"!) and make sure that counter is not getting out of index
while($l < $countVillages && $_SESSION["communesresults"][$k][1] == $_SESSION["villagesresults"][$l][0]) {
$returnV = $returnV."\"".$_SESSION["villagesresults"][$l][2]."\",\"".$_SESSION["villagesresults"][$l][1]."\",";
//Set the Default Value
$defaultV = "makeGeo.forValue(\"".$_SESSION["communesresults"][$k][1]."\").setDefaultOptions(\"".$_SESSION["searchresultspmd"][0][8]."\");";
//Increase the counter by 1
$l++;
}
//Cut the last "," of the string after the last value of $returnC
$returnV = substr($returnV, 0, -1);
//Close the JavaScript statement
$returnV = $returnV.");";
//If there is no village for the commune, remove the already prepared string "makeGeo.forValue(\"".$_SESSION["provincesresults"][$i][1]."\").forValue(\"".$_SESSION["districtsresults"][$j][1]."\").forValue(\"".$_SESSION["communesresults"][$k][1]."\").addOptionsTextValue("
if(substr($returnV,-21) == "addOptionsTextValue);") {
$returnV = substr($returnV, 0, -86);
}
//Set the Default Value
$defaultC = "makeGeo.forValue(\"".$_SESSION["districtsresults"][$j][1]."\").setDefaultOptions(\"".$_SESSION["searchresultspmd"][0][7]."\");";
//Increase the counter by 1
$k++;
}
//Cut the last "," of the string after the last value of $returnC
$returnC = substr($returnC, 0, -1);
//Close the JavaScript statement
$returnC = $returnC.");";
//If there is no commune for the district, remove the already prepared string "makeGeo.forValue(\"".$_SESSION["provincesresults"][$i][1]."\").forValue(\"".$_SESSION["districtsresults"][$j][1]."\").addOptionsTextValue("
if(substr($returnC,-21) == "addOptionsTextValue);") {
$returnC = substr($returnC, 0, -66);
}
//Set the Default Value
$defaultD = "makeGeo.forValue(\"".$_SESSION["provincesresults"][$i][1]."\").setDefaultOptions(\"".$_SESSION["searchresultspmd"][0][8]."\");";
//Increase the counter by 1
$j++;
}
//Cut the last "," of the string after the last value of $returnPD
$returnPD = substr($returnPD, 0, -1);
//Close the JavaScript statement
$returnPD = $returnPD.");";
//If there is no district for the province, remove the already prepared string "makeGeo.forValue(\"".$_SESSION["provincesresults"][$i][1]."\").addOptionsTextValue("
if(substr($returnPD,-21) == "addOptionsTextValue);") {
$returnPD = substr($returnPD, 0, -47);
}
//Set the Default Value
$defaultP = "makeGeo.forValue(\"provinces\").setDefaultOptions(\"".$_SESSION["provincesresults"][$i][1]."\");";
}
//Put Provinces, Districts, Communes and Villages together and close the Javascript
$returnPDCV = $returnPD.$returnC.$returnV.$defaultD.$defaultC.$defaultV."</script>";
//Return the result
return sprintf($returnPDCV);
}
?>
爲了使用JavaScript,我用在patient_update.php下面的代碼:
//include the dynamic drop down generator
include("/functions/dyndrpdwn.php");
<!--Adding JavaScript for dynamic dropdown list-->
<script type=\"text/javascript\" src=\"/js/dynamicoptionlist.js\"></script>
//Call the dynamic drop down function
echo dyndrpdwn();
echo"<!--Province Create Drop-Down Field-->
<select name=\"provinces\" class =\"dropdown\">";
//Fill the drop down, when data is received by get_value_help.php or if session array is already filled
if(isset($_GET["value"]) == "true" && $_GET["value"] == "receive" || isset($_SESSION["provincesresults"]) == "true" && count($_SESSION["provincesresults"]) > 0) {
//Get all the values out of the Province array out of pdo.vhd
$ countProvinces =計數($ _ SESSION [」 provincesresults「]);
for($i=0; $i < $countProvinces; $i++) {
//Display the Khmer or the English language depending on the website settings. $_SESSION["searchresultspmd"][0][5]) = province_id in dbo.pmd
//$_SESSION["provincesresults"][$i][1] = province_id in dbo.vhp
if(isset($_SESSION["lng"]) == "true") {
switch ($_SESSION["lng"]) {
case "english":
if($_SESSION["provincesresults"][$i][2] == $_SESSION["searchresultspmd"][0][5]) {
echo"<option value=".$_SESSION["provincesresults"][$i][1]." SELECTED>";
}
else {
echo"<option value=".$_SESSION["provincesresults"][$i][1].">";
}
echo"".$_SESSION["provincesresults"][$i][2]."
</option>";
break;
case "khmer":
if($_SESSION["provincesresults"][$i][2] == $_SESSION["searchresultspmd"][0][5]) {
echo"<option value=".$_SESSION["provincesresults"][$i][1]." SELECTED>";
}
else {
echo"<option value=".$_SESSION["provincesresults"][$i][1].">";
}
echo"".$_SESSION["provincesresults"][$i][3]."
</option>";
break;
}
}
}
}
echo" </select>
</td>
<td>
<!--District Create Drop-Down Field-->
<select name=\"districts\" class =\"dropdown\">
<script type=\"text/javascript\">
makeGeo.printOptions(\"districts\")
</script>
</select>
</td>
<td>
<!--Commune Create Drop-Down Field-->
<select name=\"communes\" class =\"dropdown\">
<script type=\"text/javascript\">
makeGeo.printOptions(\"communes\")
</script>
</select>
</td>
<td>
<!--Village Create Drop-Down Field-->
<select name=\"villages\" class =\"dropdown\">
<script type=\"text/javascript\">
makeGeo.printOptions(\"villages\")
</script>
</select>
</td>
...
因此,有兩個問題,現在:
- 調用dyndrpdwn.php函數需要幾秒鐘的時間,因爲有〜24個省,〜86個區,〜1600個社區和〜13500個村莊和我循環它們。有沒有辦法改善性能?
- With
setDefaultOptions
我嘗試爲正確的下拉列表設置默認值。但在我的代碼中不起作用。我將它與[JavaScript網站] [1]上的示例進行了比較,但是我找不到錯誤...您能否看到它爲什麼不能像我這樣做?
結果目前看起來像這樣:
<script type="text/javascript">
var makeGeo = new DynamicOptionList("provinces","districts","communes","villages");
這將創建一個省份和地區的條目:
makeGeo.forValue("010000").addOptionsTextValue("Mongkol Borei","010200","Phnum Srok","010300","Preah Netr Preah","010400","Ou Chrov","010500","Serei Saophoan","010600","Thma Puok","010700","Svay Chek","010800","Malai","010900");
這對公社創建條目
makeGeo.forValue("010000").forValue("010200").addOptionsTextValue("Banteay Neang","010201","Bat Trang","010202","Chamnaom","010203","Kouk Ballangk","010204","Koy Maeng","010205","Ou Prasat","010206","Phnum Touch","010207","Rohat Tuek","010208","Ruessei Kraok","010209","Sambuor","010210","Soea","010211","Srah Reang","010212","Ta Lam","010213");
這創建了村莊的條目:
makeGeo.forValue("010000").forValue("010200").forValue("010201").addOptionsTextValue("Ou Thum","01020101","Phnum","01020102","Banteay Neang","01020103","Kouk Pnov","01020104","Trang","01020105","Pongro","01020106","Kouk Tonloab","01020107","Trabaek","01020108","Khile","01020109","Samraong Pen","01020110","Dang Run Lech","01020111","Dang Run Kaeut","01020112","Ou Snguot","01020113","Prey Changha Lech","01020114","Prey Changha Kaeut","01020115","Ou Andoung Lech","01020116","Ou Andoung Kandal","01020117","Ou Andoung Kaeut","01020118","Kouk Kduoch","01020119");
而這應該設置省,區和公社的默認值(只有3個例子;他們沒有工作至今...):
makeGeo.forValue("240000").setDefaultOptions("Boeng Trakuon");
makeGeo.forValue("240200").setDefaultOptions("Ou Andoung");
makeGeo.forValue("240204").setDefaultOptions("Boeng Trakuon");</script>
<!--Province Create Drop-Down Field-->
<select name="provinces" class ="dropdown">
<option value=010000>Banteay Mean Chey
</option><option value=020000>Bat Dambang
</option><option value=030000>Kampong Cham
</option><option value=040000>Kampong Chhnang
</option><option value=050000>Kampong Spueu
</option><option value=060000>Kampong Thum
</option><option value=070000>Kampot
</option><option value=080000>Kandal
</option><option value=090000>Kaoh Kong
</option><option value=100000>Kracheh
</option><option value=110000>Mondol Kiri
</option><option value=120000>Phnom Penh
</option><option value=130000>Preah Vihear
</option><option value=140000>Prey Veaeng
</option><option value=150000>Pousat
</option><option value=160000>Rotanak Kiri
</option><option value=170000>Siem Reab
</option><option value=180000>Krong Preah Sihanouk
</option><option value=190000>Stueng Traeng
</option><option value=200000>Svay Rieng
</option><option value=210000>Takaev
</option><option value=220000>Otdar Mean Chey
</option><option value=230000>Krong Kaeb
</option><option value=240000 SELECTED>Krong Pailin
</option></select>
</td>
<td>
<!--District Create Drop-Down Field-->
<select name="districts" class ="dropdown">
<script type="text/javascript">
makeGeo.printOptions("districts")
</script>
</select>
</td>
<td>
<!--Commune Create Drop-Down Field-->
<select name="communes" class ="dropdown">
<script type="text/javascript">
makeGeo.printOptions("communes")
</script>
</select>
</td>
<td>
<!--Village Create Drop-Down Field-->
<select name="villages" class ="dropdown">
<script type="text/javascript">
makeGeo.printOptions("villages")
</script>
</select>
</td>
我知道它的相當多的東西,通過看,對不起那個butevery幫助高度讚賞!
謝謝RMD開發者, 這是相當有幫助的。 我正在使用JavaScript [鏈接](http://www.mattkruse.com/javascript/dynamicoptionlist/index.html)創建列表。 要回答你的問題:我爲各省使用$ _SESSION,因爲我有一個充滿信息的Microsoft SQL Server數據庫。由於我需要將(例如省份)信息傳遞給不同的網站,因此我將它們存儲在$ _SESSION中。 你知道更好的方式如何在不同的php文件之間傳遞大量的數據嗎? – Daniel 2011-03-28 03:07:28