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->regionArray=array();   
	          $this->cityArray = array();			  
	}
	
	function close_JSDropDown_database(){
	//Purpose: Close the Database Connectivity
	    mysql_close($this->db_con);
	}
	
	
	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";   
		$cityResult=mysql_query($citySql);
		$IDx=0;$IDy=0;$IDz=0;
		$this->regionArrayResult=array();
		while ($cityRow = mysql_fetch_array($cityResult,MYSQL_BOTH)){
			for($i=0;$i<mysql_num_fields($cityResult);$i++){
					$this->cityArray[$IDx][$i] = $cityRow[$i];
			}	
			$regionSql="select * from region WHERE city_id='".$cityRow["city_id"]."'";		   
			$regionResult=mysql_query($regionSql);
			$IDz=mysql_num_rows($regionResult);
			while($regionRow = mysql_fetch_array($regionResult)){
			        for($i=0;$i<mysql_num_fields($regionResult);$i++){
					    $this->regionArray[$IDy][$i] = $regionRow[$i];
					}
					$this->regionArray[$IDy][$i]=$cityRow[1]; 		
					$IDy+=1;         
			}// Region While Loop Ends here;
		   $IDx+=1; 
		} // 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";
      for($i=0;$i<count($this->cityArray);$i++){
         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";
         for($j=0;$j<Count($this->regionArray);$j++){
             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>";	
	    $this->CreateSelect();
   }
    
   function CreateSelect(){
        echo "<select name=\"city\" size=\"1\" onChange='changeRegion(this)'>\r";
		    echo "<option value='--Select--' size="5">--Select--</option>\r";
   		  for($i=0;$i<count($this->cityArray);$i++){
		         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