<?php

	/***
	 * query insert class
	 * return alert on success
	 * package  query builder
	 
	 * author codewriter
	 ***/
include_once("_config_.php");
$conf = new config();
class queryinput
{
	const SELECT = "select ";
	const WHERE = " where ";
	const INSERT = "INSERT INTO ";
	const UPDATE = "update ";
	protected $_tablename;
	
	protected $_content = array();
	
	private function connt(){
		$conf = new config();
	 	return $conf->_conni();
	}
	
	public function queryhandler($table, $column = array())
	{
		
			$q =  self::INSERT;
			$q .= $this->settable($table);
			$q .= " (";
			$q .= $this->setColumn($column);
			$q .= ") VALUES";
			$q .= " (";
			$q .= $this->setValues($column);
			$q .= ")";
		
		//$check = "select * from '".$table."' where '".$this->setColumn($column)."' = '".$this->setValues($column)."'";
		//$dup = mysql_query($check);
	
		// update tabel_name SET title = '".$value."' where id = '".$id."'
		
		$query = mysqli_query($this->connt(), $q);
		return $query;

	}
	
	
	public function queryUpdater($table, $values, $where)
	{
		$up = self::UPDATE;
		$up .= $this->settable($table);
		$up .= " set ";
		$up .= $this->setUpdateData($values);
		//$up .= self::WHERE;
		$up .= $this->setWhere($where);
		//echo $up;
		$submitquery = mysqli_query($this->connt(), $up) or die(mysqli_error($this->connt()));
		return $submitquery;
	
	}
	
	
	private function setTable($table)
	{
		if($table != '' && is_string($table)){
			$this->_tablename = strtolower($table);
		}else{
			throw new exception("Table Name Must Be Delcare");
		}// end if
		return $this->_tablename;
	}
	
	private function setContent($content)
	{
		if(!is_array($content)) {
			throw new exception("Content Must Be An Array");
		}else{
		$this->_content = $content;
		} // end if
		return $content;
	}
	
	private function setColumn($column)
	{
			$keys = array();	
			foreach($this->setContent(array_keys($column)) as $key){
				$keys[] = $key;
			}
			$final = implode(",", $keys);
			return $final;
	}
	
	private function setValues($value)
	{
			$values = array();	
			foreach($this->setContent(array_values($value)) as $key => $value){
				$values[] = "'".$value."'";
			}
			$final = implode(", ", $values);
			return $final;
	}
	
	private function setNeedle($value)
	{
			$values = array();	
			foreach($this->setContent(array_values($value)) as $key => $value){
				$values[] = "".$value."";
			}
			$final = implode(", ", $values);
			return $final;
	}
	
	private function setWhere($value)
	{
		if($value != '' && is_array($value)) {
			$x = array();
			foreach($value as $k => $v){
			$x[]= "" .$k. " = '" .$v. "'";
			}
			$col = self::WHERE;
			$col .= implode(" AND ", $x);
		}else{
			$col = '';
		}
			return $col;
				
	}
	
	private function setUpdateData($value)
	{
		if($value != '' && is_array($value)) {
			$x = array();
			foreach($value as $k => $v){
			$x[]= "" .$k. " = '" .$v. "'";
			}
			//$col = self::WHERE;
			$col = implode(", ", $x);
		}else{
			$col = '';
		}
			return $col;
				
	}
	

	public function queryBuilder($needle = array(), $table, $where)
	{
	
	$sql = self::SELECT;
	$sql .= $this->setNeedle($needle);
	$sql .= " FROM ";
	$sql .= $table;
			
	$sql .= $this->setWhere($where);
	//echo $sql;
	$sqlQuery = mysqli_query($this->connt(), $sql) or die(mysql_error($this->connt()));
	//echo $sqlQuery;
	return $sqlQuery;
	
	}
	
	/* public function setLogout($session, $id)
	{
		if(isset($session) && isset($id)) {
		
	
	}
	*/
public function queryBuilderBySort($needle = array(), $table, $where, $sortfield)
	{
	
	$sql = self::SELECT;
	$sql .= $this->setNeedle($needle);
	$sql .= " FROM ";
	$sql .= $table;
			
	$sql .= $this->setWhere($where);
	
	$sql .= " ORDER BY ".$sortfield." ASC";
	//echo $sql;
	$sqlQuery = mysqli_query($this->connt(), $sql) or die(mysql_error($this->connt()));
	//echo $sqlQuery;
	return $sqlQuery;
	
	}
		
} // end class


?>