Forum Moderators: coopster

Message Too Old, No Replies

3 combo boxes

         

ttkt

4:07 pm on Jul 13, 2005 (gmt 0)

10+ Year Member



may i know how to create 3 combo boxes which are dependable on each other? the options in the combo boxes are retrieved from the database and if i select item A in the 1st combo box, related items of A will be displayed in second combo box.

grandpa

5:51 am on Jul 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your use of the term 'combo box' is throwing me a little, because you can do this with a list of items (A) and a query to return the related items.

One way is to create a list of items from a data table, and wrap the items in a form with a radio button on each item. When the button is selected and the form is run you can pass the selected item(s) to your query and display the results stored in the same, or another, data table.

mcibor

10:05 am on Jul 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think that you mean javascript combobox. If you select one item A, then you get a list of relevant items to A, if you select B, then relevant list to B, etc.

With php you can write javascript the same as you write html.

<script>
<!--
var date = '<?php echo date("Y-m-d");?>';
...
//-->
</scipt>

See the hotscripts [hotscripts.com] for code (it will be in javascript section).
Certainly you can use php to generate the combos, but it will require the page to be submitted as many times as you change the selection.

Hope this helps
Michal Cibor

ttkt

5:30 pm on Jul 15, 2005 (gmt 0)

10+ Year Member



hi mcibor, yup, thats what i want. thanks.

ttkt

5:11 pm on Jul 17, 2005 (gmt 0)

10+ Year Member



hi all, i have created the codes below:

createfrmObject($frmObjName) function is to pass in the form name as a parameter and create the javascript dynamically.

CreateSelect function is to create the select options.

DataFetch function is to fetch the data from the database.

how do i retrieve the "brand name" (e.g. Sony) item of typeCombo if i select e.g. TV? In other words, how do i retrieve the related items of the combo box.

function createfrmObject($frmObjName){
//Purpose: Create the Javascript Dynamically
$this->CurrectForm= $frmObjName;
$DocElement = "document.".$this->CurrectForm.".region";
echo "<script type=\"text/javascript\" language=\"javascript\">";
echo "<!--\n";
echo "\rfunction changeRegion(obj){\r";
for($i=0;$i<count($this->typeArray);$i++){
echo "\rif(obj.value == '".$this->typeArray[$i]."'){\r";
//echo "\r\t/* Value for the Region Drop Down */\r";
echo "\tdocument.$this->CurrectForm.region.options.length = 0;\r";
echo "\t$DocElement.options[$DocElement.options.length] = new Option('--Select--','--Select--');\r";
for($j=0;$j<Count($this->brandArray);$j++){
if ( $this->cityArray[$i] == $this->regionArray[$j]){
echo "\t$DocElement.options[$DocElement.options.length] = new Option('".$this->regionArray[$j][2]."','".$this->regionArray[$j][0]."');\r";
}
}
echo "\tdocument.$this->CurrectForm.region.disabled = false;\n\r";
echo "\treturn true;\r";
echo "}\r";

}
echo "}\r";
echo "-->\r";
echo "</script>";
$this->CreateSelect();
}

function CreateSelect(){
echo "<select name=\"typeCombo\" size=\"1\" onChange='changeRegion(this)'>\r";
echo "<option value='--Select--'>--Select--</option>\r";
for($i=0; $i < count($this->typeArray);$i++) {
echo "<option value='".$this->typeArray[$i]."'>".$this->typeArray[$i]."</option>";

}
echo "</select>";
echo "<select name=\"brandCombo\" disabled>";
echo "<option value='--Select--'>--Select--</option>";
echo "</select>";
}



function DataFetch() {
//Purpose: Fetch the Data From 2 Different Table and store it in array

// Suggestions: If you have changed the Table Names Please Change it in the sql;
$typeSql="select distinct type from ims_asset order by type";
$typeResult=mysql_query($typeSql);
$this->typeArray=array();
while ($typeRow = mysql_fetch_array($typeResult)){
$this->typeArray[] = $typeRow['type'];

$brandSql="select distinct brand from ims_asset";

$brandResult=mysql_query($brandSql);
$IDz=mysql_num_rows($brandResult);
//echo $IDz;
while($brandRow = mysql_fetch_array($brandResult)){
for($i=0;$i<mysql_num_fields($brandResult);$i++){
$this->brandArray[$IDy][$i] = $brandRow['brand'];
}
$this->brandArray[$IDy][$i]=$typeRow[1];
echo $this->brandArray[$IDy][$i];
$IDy+=1;
}// brand While Loop Ends here;


} // Asset Type Loop End
}// DataFetch Function Ends here