<?php
include('db.php');

function get_all_data($conn,$table)
{
	
	
	$q=mysqli_query($conn,"select * from $table order by id desc");
	
	$array=array();
	while($data=mysqli_fetch_assoc($q))
	{
		
		$array[]=$data;
		
	}
	
	return $array;
	
}


function get_all_data_order_by($conn,$table,$column,$order)
{
	
	
	$q=mysqli_query($conn,"select * from $table order by `$column` $order");
	
	$array=array();
	while($data=mysqli_fetch_assoc($q))
	{
		
		$array[]=$data;
		
	}
	
	return $array;
	
}


function get_all_data_filter_order_by($conn,$table,$column, $value , $order_column,$order)
{
	
	
	$q=mysqli_query($conn,"select * from $table  where `$column` = '$value'  order by `$order_column` $order");
	
	$array=array();
	while($data=mysqli_fetch_assoc($q))
	{
		 
		$array[]=$data;
		
	}
	
	return $array;
	
}





function get_all_data_by_equal($conn,$table,$column,$val)
{
	
	$q=mysqli_query($conn,"select * from $table where `$column`='$val' order by id desc");
	
	$array=array();
	while($data=mysqli_fetch_assoc($q))
	{
		
		$array[]=$data;
		
	}
	
	return $array;
	
	
}



//print_r(get_all_data($conn,'state'));


  function insertTable($conn,$table,$records,$submit_label)
  {
	 
      $name_field_index = 0; 
      $index = 0;	  
	  
	  $columns="(";
	  foreach(array_keys($records) as $field)
	  {
		  $index++;
		  
		  if($field == 'name')
		  {
			   $name_field_index  = $index;
			  
		  }
		  
		  
		  if($field!=$submit_label) {
		  $columns.="`$field` , ";
		  }
	  }
	  
	  $columns.=" `date_time` ) ";
	  
	  
	  
	 /* 
	   if($table == 'leads' && $field == 'name')
	   {
		   
		   $records['name'] = ucwords(strtolower($records['name']));
		   
	   }
	  
	  */
	  
	  $index = 0;
	    $values="( ";
		 foreach($records as $val)
	  {
		  $index++;
		  
		  if($val != 'submit_button')
		  {
			  
			  
			  
			  if($table == 'leads' && $name_field_index == $index)
	          {
		   
		     $val = ucwords(strtolower($val));
		   
	          }
			  
			  
			  
		  $val= mysqli_real_escape_string($conn,$val);
		  $values.=" '$val' , ";
		  }
	  }
	  
	  $now_time = date('Y-m-d H:i:s');

	  
	    $values.=" '$now_time' );";
		
		
	  $q=mysqli_query($conn,"insert into `$table` $columns VALUES $values ");
	  
	   if($q)
      {
		//echo "Data Inserted Sucessfully"; 
	}
	   
	   else {
	   echo mysqli_error($conn);
	   }
	  
	  
	  
	  
	  
  }



 
		 /*************  UPDATE TABLE *********/
		 
		 
		 
  function updateTable($conn,$table,$records,$submit_label,$id)
  {
	  
	  
	   $qry_str="";
	  foreach(array_keys($records) as $field)
	  {
		  if($field!=$submit_label && $field!='row_id' ) {
			  
			  
	         if($table == 'leads' && $field == 'name')
	          {
		   
		   $records[$field] = ucwords(strtolower($records[$field]));
		   
	        }
			  
			  
			  $val= mysqli_real_escape_string($conn,$records[$field]);
			 
			  
		  $qry_str.="`$field` = '".$val."' , ";
		  
		  }
	  }
	   $qry_str.=" `date_time`=now() ";
	  
	  $q=mysqli_query($conn,"update `$table` set $qry_str  where id=$id ");
	  
	 // echo "update `$table` set $qry_str  where id=$id ";
	  
	  if($q)
	  {
		//  echo "Data Updated Sucessfully";
	  }
	  else
	  {
		  echo mysqli_error($conn);
		  
	  }
	  
	  
	  
  }

		 
		 
		 
		 
		 
		 /************   DELETE QUERY ***************/
		 
function deleteTable($conn,$table,$id)
{
	
	
	$q=mysqli_query($conn,"delete from `$table` where id=$id ");
	
}
		 
/***************  get column table name********************//////////

function get_all_column($conn,$table)
{
	
	
	$q=mysqli_query($conn,"DESCRIBE $table");
	
	$array=array();
	
	while($data=mysqli_fetch_assoc($q))
	{
		
		$array[]=$data['Field'];
		
	}
	
	return $array;
	
}




?>