Location: PHPKode > scripts > Cascade Select Associative > cascade-select-associative/cascadeSelectAssoc.php
<?php
////////////////////////////////////////////////////////////////////////////////////
// cascadeSelectAssoc Class : Create Cascade Select Input Boxes with HTML and Javascript
// 				using an associative array as data source.
// <randradefonseca at gmail dot com>
// <vedanta.barooah at gmail dot com>
////////////////////////////////////////////////////////////////////////////////////
class cascadeSelectAssoc{
	// attributes for the select box
	var $motherName; 
	var $childName;
	var $motherHeight;
	var $childHeight;
	var $motherLength;
	var $childLength;
	// data, passed as array
	var $motherData;
	var $childData;
	var $workForm;
	
	function cascadeSelectAssoc($formName){
		$this->workForm=$formName;
	}
	
	function setName($mother,$child){
		$this->motherName=$mother;
		$this->childName=$child;
	}
		
	function setHeight($mother=5,$child=5){
		$this->motherHeight=$mother;
		$this->childHeight=$child;
	}
	

	function setData($mother,$child){
		$this->motherData=$mother;
		$this->childData=$child;
	}

	function writeScript(){
	// write the javascript
	echo "<script type='text/javascript'>\n";
	echo "<!--\n";
	echo "function change_selection(select_value) {\n";
	echo "IntPath = document.$this->workForm.$this->childName\n";
	echo "TheOptions = IntPath.options.length";
	foreach($this->motherData as $key=>$value) {
	echo "\nif(select_value =='".$key."'){\t\n";
	echo "/*---- Processing child node for ". $key ." ---- */\n";
	echo "document.$this->workForm.$this->childName.options.length = 0;";
		foreach($this->childData[$key] as $childKey=>$childValue) {
			echo "\nIntPath.options[IntPath.options.length] = new Option('".$this->childData[$key][$childKey]."','".$childKey."');";
		}
	echo "\ndocument.$this->workForm.$this->childName.disabled = false;\n";
	echo "}\n";
	}
	echo "}\n";
	#echo "\n--!>\n";
	echo "</script>\n";
	}

	function drawMother(){
	#print_r($this->motherData);
		echo "<select name=\"$this->motherName\" size=\"$this->motherHeight\" onChange='change_selection(this.value)' >\n";
			echo "<option value='none'>Select Option</option>\n";
			#echo "<option value='none'>Seleccione una opciĆ³n</option>\n";
			foreach($this->motherData as $key=>$value) {
				echo "\n\t<option value=\"".$key."\">";
				echo $value;
				echo "</option>\n";
			}
		echo "</select>\n";
	}
	
	function drawChild(){
		echo "\n\t<select name=\"$this->childName\" disabled size=\"$this->childHeight\">";
		echo "<option value='none'>Pick from select one first</option>";
		#echo "<option value='none'>Primero seleccione un elemento de la lista anterior</option>";
		echo "</select>\n";
	}
}
?>




Return current item: Cascade Select Associative