<?php
class database{
	function fetch($table) {
		$sel="select * from $table";
		$res=executeQuery($sel);
	 return $res;
	}	
	function fetch1($table,$selector,$condition) {
		$sel="select $selector from $table where $condition";
			//$this->showError($sel);	
		return $res=$this->executeQuery($sel);	
	
	 // $this->getAssoc($res);
	}	
	function insert($table,$elements){
		$keys=array_keys($elements);
		$values=array_values($elements);
		$num=count($keys);
		$key=implode(',', $keys);
		//echo $key."<br />";
		$value=implode("','", $values);
		$val="'".$value."'";
		//echo $val."<br />";
		
	
			$in = "INSERT INTO $table($key) VALUES($val)";
		if($this->executeQuery($in)){
		  //echo "registration success full";
		  
		  }else{
		$this->showError($in);
		    }	
	}
	function update($table,$elements,$condition) {
		$keys=array_keys($elements);
		$values=array_values($elements);
		$num=count($keys);
		$str="";
		for ($i = 0; $i < $num; $i++) {
			$str.="`$keys[$i]`='".$values[$i]."'";
			if($i<$num-1)
			{
				$str.=",";};
			
		}
		//echo $str;
		$up="UPDATE  `$table` SET  $str where $condition";
		if($this->executeQuery($up)){
		  //echo "registration success full";
		  
		  }else{
		$this->showError($up);
		    }	
		
	}
	function delete($table,$condition){
		
		$del="DELETE FROM $table WHERE $condition";
		if(executeQuery($del)){
		 // echo "Record Deleted";
		  
		  }else{
		showError($del);
		    }	
	}
	
	
function executeQuery($query){
    return mysqli_query($_SESSION['db_link'],$query);   
}

function totalRecords($result){
   return mysqli_num_rows($result);
}

function getRecordId(){
   return mysqli_insert_id($_SESSION['db_link']);
}

function getAssoc($result){
   return mysqli_fetch_assoc($result);
}
function getRow($result){
   return mysqli_fetch_row($result);
}
function showError($query){
   die(mysqli_error()."<br /> Query: ".$query);
}

function dbClose(){
   mysqli_close();
}
function getRecord($key,$table,$value,$att){
  $sel="select * from $table where $key='$value'";
  $res=executeQuery($sel);
  $rec= getAssoc($res);
  return $rec[$att];
}

function getRecordTotal($key,$table,$value,$att){
  $sel="select * from $table where $key='$value'";
  $res=executeQuery($sel);
  $tot=0;
  while($rec= getAssoc($res)){
  $tot+= $rec[$att];
  }
  return $tot;
}

function getRecordTotalCon($key,$table,$value,$att,$key1,$value1){
  $sel="select * from $table where $key='$value' and $key1='$value1'";
  $res=executeQuery($sel);
  $tot=0;
  while($rec= getAssoc($res)){
  $tot+= $rec[$att];
  }
  return $tot;
}

function getRecordCount($key,$table,$value){
  $sel="select * from $table where $key='$value'";
  $res=executeQuery($sel);
    return mysqli_num_rows($res);
}
function getOptions($key,$table,$value,$action,$parent,$i,$par){
  $cnt=$this->getRecordCount('parent','ven_products',$value);
   if($cnt!=0){
  ?>
   <optgroup label="<?php 
	$j=$i;
	while($j>0){echo " ->";$j--;}echo $this->getRecord('id','ven_categories',$value,'name') ?>"></optgroup>
     <?php }else{?>

	<option <?php if($action=='edit'){if($par==$value){echo 'selected';}}?> value="<?php echo $value;?>"><span style="font-size:<?php echo 26;?>px;"><?php 
	$j=$i;
	while($j>0){echo " ->";$j--;}echo $this->getRecord('id','ven_categories',$value,'name') ?></span></option>
    
    <?php  }
		$pid=$value;
									$s="select * from ven_categories where parent='$pid'";
									$su=$this->executeQuery($s);
									$cnt=$this->totalRecords($su);
									if($cnt!=0){
										$i++;
									}else{?>
										
										<?php }
									while($sub=$this->getAssoc($su)){
										
									
										$this->getOptions('id','ven_categories',$sub['id'],$action,$sub['parent'],$i,$par);
										}
											
	 }
	 function getOptionsprod($key,$table,$value,$action,$parent,$i,$par){
		  $cnt=$this->getRecordCount('parent','ven_categories',$value);
		  if($cnt!=0){
		 ?>
     <optgroup label="<?php 
	$j=$i;
	while($j>0){echo " ->";$j--;}echo $this->getRecord('id','ven_categories',$value,'name') ?>"></optgroup>
    <?php }else{?>

	<option <?php if($action=='edit'){if($par==$value){echo 'selected';}}?> value="<?php echo $value;?>"><span style="font-size:<?php echo 26;?>px;"><?php 
	$j=$i;
	while($j>0){echo " ->";$j--;}echo $this->getRecord('id','ven_categories',$value,'name') ?></span></option>
    
    <?php }
		$pid=$value;
									$s="select * from ven_categories where parent='$pid'";
									$su=$this->executeQuery($s);
									$cnt=$this->totalRecords($su);
									if($cnt!=0){
										$i++;
									}else{?>
										
										<?php }
									while($sub=$this->getAssoc($su)){
										
									
										$this->getOptionsprod('id','ven_categories',$sub['id'],$action,$sub['parent'],$i,$par);
										}
											
	 }
	
}
function get_file_extension($file_name) 
{
	return substr(strrchr($file_name,'.'),1);
}

?>