<?php

	session_start();

/******************************************************************/
	
	$site = $_SERVER["SERVER_NAME"];

	$host = "";  
	$user = "";  
	$pass = ""; 
	$name = "";  

	if( ($site == "localhost") || ($site == "127.0.0.1") ) 
	{
		//echo "Connexion mode local";
		//echo "<br>";
		
		$_SESSION['hostdb']= "localhost"; 
		$_SESSION['userdb']= "root"; 
		$_SESSION['passdb']= "";
		$_SESSION['namedb']= "task_manager";   
	}
	else
	{	
		//echo "Connexion hebergeur";
		//echo "<br>";
				
		$_SESSION['hostdb']= "analogdepat.mysql.db"; 
		$_SESSION['userdb']= "analogdepat"; 
		$_SESSION['passdb']= "Un92pac007";
		$_SESSION['namedb']= "analogdepat";   
	}

/******************************************************************/
function db_connexion()
{
	
	// connexion
	$conn = new mysqli($_SESSION['hostdb'] , $_SESSION['userdb'], $_SESSION['passdb'], $_SESSION['namedb']);
	// gestion erreur de connexion
	if ($conn->connect_error) 
	{
		die("Connection failed: " . $conn->connect_error);
	}
	//else
	//{
	//	echo "DB connexion : OK";
	//	echo "<br>";
		
		/*$allTables = array();
	
		$showtables= mysqli_query($conn, "SHOW TABLES FROM $namedb");
		while($table = mysqli_fetch_array($showtables)) 
		{ 
			echo($table[0] . "<br>");  
			$allTables[] = $table[0];
		}*/
	//}
	return($conn);
}

/******************************************************************/
function InsertRecord($conn, $table, $json_data)
{
	for ($i = 0; $i < count($json_data); $i++) 
	{
		//echo $json_data[$i][0] ." ****** " .$json_data[$i][1] ;
		//echo "<br>";
		
		$string = $json_data[$i][0];
		$string = explode(')', (explode('(', $string)[1]))[0];
		
		$str_less_spacing = str_replace(' ', '', $string); 
		echo $str_less_spacing . "  --  "  .strlen($str_less_spacing);
		echo "<br>";
		
		$plage = $str_less_spacing;
		
		$valeur = $json_data[$i][1];
		echo $valeur; 
		echo "<br>";
		
		$sql = "INSERT INTO " .$table ." (plage, valeur) VALUES ('$plage', '$valeur' )";
		//echo $sql;
	
		if ($conn->query($sql) === TRUE) 
		{
			echo "New record created successfully";
			echo "<br>";
		} 
		else 
		{
			echo "Error: " . $sql . "<br>" . $conn->error;
			echo "<br>";
		}
	}
	
	echo "------------------------------------";
	echo "<br>";
	echo "<br>";
}
/******************************************************************/
function Insert($conn, $table)
{

	//$sql = "INSERT INTO MyGuests (firstname, lastname, email)
	//VALUES ('John', 'Doe', 'john@example.com')";

	$sql = "INSERT INTO " .$table ." (firstname, lastname, email)
	VALUES ('John', 'Doe', 'john@example.com')";

	echo $sql;
	
	if ($conn->query($sql) === TRUE) 
	{
		echo "New record created successfully";
	} 
	else 
	{
		echo "Error: " . $sql . "<br>" . $conn->error;
	}
	
}
/******************************************************************/
function RecordExists($conn, $table, $champ)
{
	//$query = "SELECT * FROM products WHERE code = '$code'";

	$query = "SELECT * FROM " .$table . " WHERE firstname = '$champ'";
	//echo $query;
	$result = $conn->query($query);
	if ($result) 
	{
		if (mysqli_num_rows($result) > 0) 
		{
			echo 'found!';
		} 
		else 
		{
			echo 'not found';
		}
	} 
	
	else 
	{
		echo 'Error: ' . mysqli_error();
	}
}
/******************************************************************/
function CreateTable($conn)
{

	// sql to create table
	$sql = "CREATE TABLE aaaMyGuests (
	id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
	firstname VARCHAR(30) NOT NULL,
	lastname VARCHAR(30) NOT NULL,
	email VARCHAR(50)
	)";
	
	//echo $sql;

	if ($conn->query($sql) === TRUE) 
	{
		echo "Table MyGuests created successfully";
		echo "<br>";
	} 
	else 
	{
			echo "Error creating table: " . $conn->error;
			echo "<br>";
	}
}
/******************************************************************/
function AddTable($conn, $table)
{
		$status = "";

		if( table_exists($conn, $table) == false )
		{
			$sql = "CREATE TABLE " .$table ." (
			id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
			plage VARCHAR(30) NOT NULL,
			valeur VARCHAR(30) NOT NULL )";
			
			echo $sql;
			echo "<br>";
			
			if ($conn->query($sql) === TRUE) 
			{
				echo "---->>> La table a été ajoutée à la DB";
				echo "<br>";
				$status = "CREATION";
			} 
			else 
			{
				echo "Error creating table: " . $conn->error;
				echo "<br>";
				$status = "ERREUR";
			}
		}
		else
		{
			echo "---->>> Impossible de créer une table en doublon !";
			echo "<br>";
			$status = "DOUBLON";
		}
		
		return($status);
		
}
/*****************************************************************/
function ViderTable($conn, $table)
{
	$query = "TRUNCATE TABLE " .$table;
	$conn->query($query);
}

/****************************************************************/
function DBGetAllTables($conn)
{
	/*if( ($site == "localhost") || ($site == "127.0.0.1") ) 
	{
		echo "Connexion mode local";
		echo "<br>";
		$hostdb = "localhost";  
		$userdb = "root";  
		$passdb = ""; 
		$namedb = "task_manager";  
	}
	else
	{	
		echo "Connexion hebergeur";
		echo "<br>";
		$hostdb = "analogdepat.mysql.db";
		$userdb = "analogdepat";
		$passdb = "Un92pac007";
		$namedb = "analogdepat";
	}*/
	
	if ($conn->connect_error) 
	{
		die("Connection failed: " . $conn->connect_error);
	}
	else
	{
		///echo "Connection Active";
		echo "<br>";
		echo "<br>";
		
		$allTables = array();
		//$vvv = $_SESSION['namedb'];
		$query = "SHOW TABLES FROM " .$_SESSION['namedb'];
		//echo $lili;
		//$showtables= mysqli_query($conn, "SHOW TABLES FROM $vvv");
		$showtables= mysqli_query($conn, $query);
		while($table = mysqli_fetch_array($showtables)) 
		{ 
			echo($table[0] . "<br>");  
			$allTables[] = $table[0];
		}		
	}
}
/******************************************************************/
function table_exists($conn, $table)
{
	$query = "SHOW TABLES LIKE '$table'";
	//$result = $conn->query("SHOW TABLES LIKE '$table'");
	$result = $conn->query($query);
	
	$flag = false;
	
	if($result->num_rows == 1)
	{
		//echo "Table exists";
		//echo "<br>";
		$flag = true;	
	} 
	else 
	{
		//echo "Table does not exists";
		//echo "<br>";
		$flag = false;
	}
	
	return($flag);
}
/******************************************************************/

?>