<?php
/**
* Author(s): Gobinath Modified by: Brandon Crabtree to be included in the program Shift Log
* DropMenu 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 DropMenu{
//Member varible Declaration
var $db_con;
var $rs_row;
var $ERR_MSG;
var $subcatArrayResult;
// Declare Array Variable;
var $catArray;
var $subcatArray;
// Declare a variable for Assigning the Current Form Name
var $CurrectForm;
function DropMenu($database){
//Purpose: Constructer to Handle Database And Value initialize
$this->db_con=mysql_connect($database['server'],$database['user'],$database['password']) or die(mysql_error());
mysql_select_db($database['database'],$this->db_con) or die(mysql_error($this->db_con));
// Declare the variables as Array
$this->subcatArray=array();
$this->catArray = array();
}
function close_DropMenu_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;
$catSql="SELECT * FROM category ORDER BY o_id";
$catResult=mysql_query($catSql);
$IDx=0;$IDy=0;$IDz=0;
$this->subcatArrayResult=array();
while ($catRow = mysql_fetch_array($catResult,MYSQL_BOTH)){
for($i=0;$i<mysql_num_fields($catResult);$i++){
$this->catArray[$IDx][$i] = $catRow[$i];
}
$subcatSql="select * from subcat WHERE cat_id='".$catRow["cat_id"]."' ORDER BY o_id";
$subcatResult=mysql_query($subcatSql);
$IDz=mysql_num_rows($subcatResult);
while($subcatRow = mysql_fetch_array($subcatResult)){
for($i=0;$i<mysql_num_fields($subcatResult);$i++){
$this->subcatArray[$IDy][$i] = $subcatRow[$i];
}
$this->subcatArray[$IDy][$i]=$catRow[1];
$IDy+=1;
}// subcat While Loop Ends here;
$IDx+=1;
} // cat subcat Loop End
}// DataFetch Function Ends here
function createfrmObject($frmObjName){
//Purpose: Create the Javascript Dynamically
$this->CurrectForm= $frmObjName;
$DocElement = "document.".$this->CurrectForm.".subcat";
echo "<script type=\"text/javascript\" language=\"javascript\">";
echo "<!--\n";
echo "\rfunction changesubcat(obj){\r";
for($i=0;$i<count($this->catArray);$i++){
echo "\rif(obj.value == '".$this->catArray[$i][0]."'){\r";
echo "\r\t/* Value for the subcat Drop Down */\r";
echo "\tdocument.$this->CurrectForm.subcat.options.length = 0;\r";
echo "\t$DocElement.options[$DocElement.options.length] = new Option('','');\r";
for($j=0;$j<Count($this->subcatArray);$j++){
if ( $this->catArray[$i][0] == $this->subcatArray[$j][1]){
echo "\t$DocElement.options[$DocElement.options.length] = new Option('".$this->subcatArray[$j][2]."','".$this->subcatArray[$j][0]."');\r";
}
}
echo "\tdocument.$this->CurrectForm.subcat.disabled = false;\n\r";
echo "\treturn true;\r";
echo "}\r";
}
echo "}\r";
echo "-->\r";
echo "</script>";
$this->CreateSelect();
}
function CreateSelect(){
echo "Category <select name=\"cat\" size=\"1\" onChange='changesubcat(this)'>\r";
echo "<option value='0'>Select One</option>\r";
for($i=0;$i<count($this->catArray);$i++){
echo "<option value='".$this->catArray[$i][0]."'>".$this->catArray[$i][1]."</option>\r";
}
echo "</select> Subcategory ";
echo "<select name=\"subcat\" disabled>\r";
echo "<option value=''></option>\r";
echo "</select>\r";
}
} // Class DropMenu Ends Here
?>