<?php

class db {
//database class
//maak verbinding
	static $result;
	//$server = database server
	//$user = mysql username
	//$pass = mysql password
	//$db = database name
	//function wil return a link can be used for multiple links to 2 or more databases
	public function connect($server,$user,$pass,$db){
		//maak verbinding met database 
		$link = @mysqli_connect($server,$user,$pass) or trigger_error('No valid user name or password or server is not running',E_USER_WARNING);
		if($c = @mysqli_select_db($link,$db) or trigger_error('No valid database name',E_USER_WARNING)){
		  	return $link;
		}else{
		    return false;
		}
		
	}
	
	//afhandeling van de database rotzooi
	//private not able to request form out side
	//$sql = sql code 
	//$link = link identifer
		private function query_exec($sql,$link){
		//voer query uit
		
			$out = mysqli_query($link,$sql) or trigger_error(mysqli_errno($link) .':'.mysqli_error($link),E_USER_WARNING);
		return $out;
	}
	//recive a row or a muli dimen array
	//publik
	//$sql = sql code
	//$link = link identifer
	//$array = return a arry if true (NO required) 
    //$values = array with all values that has to be protected by mysqli_real_escape_string()
	public function get_row($sql,$link,$array = false,$values = ''){
		//voer query uit
		if(!empty($values)){
		$new = explode('?',$sql);
		$sql = '';
		$i = 0;
		foreach($values as $value){
			$sql .= $new[$i] . mysqli_real_escape_string($link,$values[$i]);
			$i++;
			}
		$sql .= $new[$i];
		}
	    db::$result = db::query_exec($sql,$link);
		if(!db::$result){
			return false;
		}else{
		   if($array != true){
		    	return mysqli_fetch_assoc(db::$result);
		    }else{
		      $rows = array();
		      while($row = mysqli_fetch_assoc(db::$result)){
				$rows[] = $row;
		    }
		    return $rows;
		    }
		}	
	}
	//count rows 
	//public
	//$sql = Sql code not required if empty it will use the last restored restult for query
	//$link = link identifer
    //$values = array with all values that has to be protected by mysqli_real_escape_string()
	public	function num_row($sql = '',$link,$values = ''){
	if(!empty($sql)){

		//voer query uit
		if(!empty($values)){
		$new = explode('?',$sql);
		$sql = '';
		$i = 0;
		foreach($values as $value){
			$sql .= $new[$i] . mysqli_real_escape_string($link,$values[$i]);
			$i++;
			}
		$sql .= $new[$i];
		}
	  	db::$result = db::query_exec($sql,$link);
	}
	return mysqli_num_rows(db::$result);
	}
	
	//insert a row
	//$table = table name
	//$value = value has to be a array for easy data handling
	//$link = link identifer
	//$date = INT if you need to insert the date use a number if 0 it will be not used. use the key for the array because it runs on a loop...

	public	function insert_row($table,$value,$link,$date = 0)
        {       $sql = 'INSERT INTO '.$table.' SET';
                $i = 1;
        		foreach($value as $label => $text){
	            if($date == $i && $date > 0){    
                $sql.= ' '.$label .'= NOW( ),';
            	}else{
		        $sql.= ' '.$label.'="'.mysqli_real_escape_string($link,$text).'",';
            	}
            	$i++;
		        }
                $sql = substr($sql, 0, -1);
                $res = db::query_exec($sql,$link);
                if($res == false){
                  return false;
                }else{
				    return mysqli_insert_id($link);
				}  
        }
	//update a row
	//public
	//$table = table name
	//$key = key name of the update key (Normaly a primary key)
	//$id = value that has to be selected. EG UPDATE table set a = 1 WHERE key = id
	//if $id you need to udate multiple rows you can enter the id as array 
	//$value = value has to be a array for easy data handling
	//$link = link identifer
	//$date = INT if you need to insert the date use a number if 0 it will be not used. use the key for the array because it runs on a loop...

	public	function update_row($table,$key,$id,$value,$link,$date = 0){
	   			$sql = 'UPDATE '.$table.' SET';
                $i = 1;
        		foreach($value as $label => $text){
	            if($date == $i){    
                $sql.= ' '.$label .'= NOW( ),';
            	}else{
		        $sql.= ' '.$label.'="'.mysqli_real_escape_string($link,$text).'",';
            	}
            	$i++;
		        }
                $sql = substr($sql, 0, -1);
                if(is_array($id)){
                  $sql.= ' WHERE '.$key.' IN ('.implode(',',$id).')';
                  }else{
                $sql.= ' WHERE '.$key.' = '.$id;
              }
        
                $res = db::query_exec($sql,$link);
                if($res == false){
                  return false;
                }else{
	                
				    return mysqli_affected_rows($link);
				}
		}
		//verwijder een rij
			//update a row
	//public
	//$table = table name
	//$key = key name of the update key (Normaly a primary key)
	//$id = value that has to be selected. EG UPDATE table set a = 1 WHERE key = id
	//if $id you need to udate multiple rows you can enter the id as array 
	//$link = link identifer
	public		function delete_row($table,$key,$id,$link){
		if(is_array($id)){
          $sql = 'DELETE FROM '.$table.' WHERE '.$key.' IN ('.implode(',',$id).')';
        }else{
          $sql = 'DELETE FROM '.$table.' WHERE '.$key.' = "'.$id.'"';
        }
		//voer query uit
		$out = db::query_exec($sql,$link);
		if($out == false){
		  return false;
		}else{
				    return mysqli_affected_rows($link);
		}
		}	
		//get_result
		//alleen voor 1 waarde geschikt omdat het zeer on economisch is...
		//public 
		//$sql = sql code	
	 	//$link = link identifer
		//$field = field ( default 0) no name has to be int 
        //$values = array with all protected values for mysql_real_escape_string
 	public function get_result($sql,$link,$values = '',$field = 0){
		//voeg de waardes toe... 
		if(!empty($values)){
		$new = explode('?',$sql);
		$sql = '';
		$i = 0;
		foreach($values as $value){
			$sql .= $new[$i] . mysqli_real_escape_string($link,$values[$i]);
			$i++;
			}
		$sql .= $new[$i];
		}
            $res = db::query_exec($sql,$link);
            $out = mysqli_fetch_array($res);
            return $out[$field];
        }
	//exec as query exec only now public some casses use full
	//sql code 
	//$link = link identifer
	public function exec($sql,$link){
		//voer query uit
		  $res = db::query_exec($sql,$link);
		if(!$res){
		    return false;
		}else{
		      return true;
		}
		
	}
	}
	
?>