Location: PHPKode > scripts > Cascade Drop Down > cascade-drop-down/enhanced/JSSelect.class.php
/* JSSelect.class.php                                                   */
/* =====================================================================*/
/* Copyright (c) 2004 by Gobinath  (gobinathm at gmail dot com)         */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/* JSDropDown Class
* Explanation: purpose of this class is to Fetch values from the database and display in 2 Drop Down
* Reason : The second Drop Down WIll be based on the Selection of First
class JSDropDown{

	 //Member varible Declaration
	 var $db_con;
	 var $rs_row;
 	 var $ERR_MSG; 
	 var $regionArrayResult;
	 // Declare Array Variable;
	 var $cityArray;
	 var $regionArray;

     // Declare a variable for Assigning the Current Form Name
	 var $CurrectForm;
	function JSDropDown($dbConfig){
     //Purpose: Constructer to Handle Database And Value initialize 
              $this->db_con=mysql_connect($dbConfig['server'],$dbConfig['username'],$dbConfig['password']) or die(mysql_error());
		      mysql_select_db($dbConfig['database'],$this->db_con) or die(mysql_error($this->db_con));	  
			  // Declare the variables as Array
	          $this->cityArray = array();			  
	function close_JSDropDown_database(){
	//Purpose: Close the Database Connectivity
	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;
	    $citySql="select * from citymaster";   
		while ($cityRow = mysql_fetch_array($cityResult,MYSQL_BOTH)){
					$this->cityArray[$IDx][$i] = $cityRow[$i];
			$regionSql="select * from region WHERE city_id='".$cityRow["city_id"]."'";		   
			while($regionRow = mysql_fetch_array($regionResult)){
					    $this->regionArray[$IDy][$i] = $regionRow[$i];
			}// Region While Loop Ends here;
		} // City Region Loop End 
	}// DataFetch Function Ends here
	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";
         echo "\rif(obj.value == '".$this->cityArray[$i][0]."'){\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";
             if ( $this->cityArray[$i][0] == $this->regionArray[$j][1]){
                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>";	
   function CreateSelect(){
        echo "<select name=\"city\" size=\"1\" onChange='changeRegion(this)'>\r";
		    echo "<option value='--Select--' size="5">--Select--</option>\r";
		         echo "<option value='".$this->cityArray[$i][0]."'>".$this->cityArray[$i][1]."</option>\r";
		    echo "</select> &nbsp; Area&nbsp;";
        echo "<select name=\"region\" disabled>\r";
        echo "<option value='--Select--' size="5">--Select--</option>\r";		 
	      echo "</select>\r";
} // Class JSDropDown Ends Here
Return current item: Cascade Drop Down