protected function fieldRegion($jsAction = "") { $output = ""; $allRegions = Region::getAllRows(); echo "HERE"; $output .= "<select name=\"" . static::regionIdFieldName . "\" {$jsAction}>\n"; /* The following logic will determine the selected option */ /* Chose a do...while() in order to avoid code duplication for the 'selected' option logic */ $regionId = static::anyRegionCode; $regionName = "Any region"; $firstRow = true; do { if (!$firstRow) { $regionId = Region::getRegionId($row); $regionName = Region::getRegionName($row); } else { $firstRow = false; } if ($this->searchedRegionId == $regionId) { $selected = "selected"; } else { $selected = ""; } /* here is the actual option output */ $output .= " <option {$selected} value =\"{$regionId}\">{$regionName}</option>\n"; } while (($row = $allRegions->fetch_row()) != NULL); $output .= "</select>\n"; return $output; }
protected function jsDistrictUpdate() { $output = ""; /* create the region <-> district connection */ $output .= "var districtNames = new Array();\n"; $output .= "var districtIds = new Array();\n"; $output .= "districtNames[" . static::anyRegionCode . "] = [\"Select region first\"];\n"; $output .= "districtIds[" . static::anyRegionCode . "] = [\"" . static::anyDistrictCode . "\"];\n"; $regionList = Region::getAllRows(); $regionCount = 0; while (($region = $regionList->fetch_row()) != NULL) { $regionId = Region::getRegionId($region); $districtList = District::getDistrictList($regionId); /* first the district names */ $output .= "districtNames[{$regionId}] = [\"Any district\""; while (($district = $districtList->fetch_row()) != NULL) { $output .= ", \"" . District::getDistrictName($district) . "\""; } $output .= "];\n"; /* then the district Ids */ $output .= "districtIds[{$regionId}] = [\"" . static::anyDistrictCode . "\""; $districtList->data_seek(0); while (($district = $districtList->fetch_row()) != NULL) { $output .= ", \"" . District::getDistrictId($district) . "\""; } $output .= "];\n"; } /* print the districtUpdate() function */ /* the below code is JavaScript */ $output .= "\n"; $output .= "districtList = document." . static::formName . "." . static::districtIdFieldName . ";\n"; $output .= "function districtUpdate(selectedRegionId){\n"; $output .= " districtList.options.length = 0;\n"; $output .= " for (i=0; i < districtNames[selectedRegionId].length; i++) {\n"; $output .= " districtList.options[districtList.options.length] = new Option(districtNames[selectedRegionId][i],districtIds[selectedRegionId][i]);\n"; $output .= " }\n"; $output .= "}\n"; return $output; }