Martin Wegeberg - 2006-08-27 12:14:04
These changes makes it possible to return a select box with option values corresponding to eg. unique keys in an mysql-table, in place of the default 0, 1, 2... scheme:
<?php
class cascadeSelect {
// 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 cascadeSelect( $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 $mkey=>$mval ) {
echo "\nif(select_value =='" . $mkey . "'){\t\n";
echo "/*---- Processing child node for " . $mkey . " ---- */\n";
echo "document.$this->workForm.$this->childName.options.length = 0;";
foreach( $this->childData[ $mkey ] as $ckey=>$cval ) {
echo "\nIntPath.options[IntPath.options.length] = new Option('" . $cval . "','" . $ckey . "');";
}
echo "\ndocument.$this->workForm.$this->childName.disabled = false;\n";
echo "}\n";
}
echo "}\n";
echo "</script>\n";
}
function drawMother(){
echo "<select name=\"$this->motherName\" size=\"$this->motherHeight\" onChange='change_selection(this.value)' >\n";
echo '<option value="0">-- Vælg Bureau</option>' . "\n";
foreach( $this->motherData as $key=>$val ) {
echo "\t" . '<option value="' . $key . '">';
echo $val;
echo "</option>\n";
}
echo "</select>\n";
}
function drawChild(){
echo "\n\t<select name=\"$this->childName\" disabled size=\"$this->childHeight\">";
echo "<option value='none'>Vælg først bureau</option>";
echo "</select>\n";
}
}
?>