<?php

	session_start();
	
	$_SESSION['database']=="public";
	
	if($_SESSION['database']=="local")
	  {
	     $db_host    = "localhost";
	     $db_user   = "root";
	     $db_pass   = "";
     	     $db_name   = "boutique";

	 }
	
	if($_SESSION['database']=="public")
	  {
  	     $db_host    = "sql2";
   	     $db_user   = "analogde";
 	     $db_pass   = "rainbow";
    	     //$db_type   = "mysql";
	     $db_name   = "analogde";
	 }

?>

<meta http-equiv=Content-Type content=text/html; charset=iso-8859-1> 

<?php

function supprimer_caracteres($chaine)
{
 	return( strtr( $chaine, "ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ","AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn" ) );
} 

function compacter($str)
{
	$str =supprimer_caracteres($str);
	
	// remplace les apostrophes par des underscore
	$result = str_replace ( "'" , "_", $str);
	
	// remplace les espace par des underscore
	$result = str_replace ( " " , "_", $result);
	
	return $result;
}

function decompacter($str)
{
	$morceaux = explode("_",$str);
	$nbr = count($morceaux);
	$tab = array();
	$ch = "";
	
	for($i=0; $i<$nbr; $i++) 
	   {  $len = strlen($morceaux[$i]);
	      if($len == 1)
	       { $tab[]=$morceaux[$i];
	         $tab[]="'";
               }
	   else{ $tab[]=$morceaux[$i];
	       }
	   }	
	
	//print_r($morceaux);
	
	for($i=0; $i<$nbr; $i++) 
	   { $len = strlen($morceaux[$i]);
	     if($len == 1)
	       { $ch = $ch .$morceaux[$i] ."'"; 
	         
	       }
	     else {  	
	     	    $ch = $ch .$morceaux[$i] ." ";  
		  }
	   }	
	
	return $ch;
}
	
	$association ="Consommation d'eau et production d'énergie par jour";
	$filtre = compacter($association);
	$association = addslashes($association);
	
	// prevoir une routine pour vérifier si l'enregistrement existe déjà
	$connexion = mysql_connect($db_host,$db_user,$db_pass); 
	mysql_select_db($db_name, $connexion);
	$sql = "SELECT * FROM correspondance WHERE filtre = 'Consommation_d_eau_et_production_d_energie_par_jour' ";
	$req = mysql_query($sql);

	if(mysql_num_rows($req) == 0)
	   {
     		$connexion = mysql_connect($db_host,$db_user,$db_pass); 
     		mysql_select_db($db_name, $connexion);
		$sql = "INSERT INTO correspondance( id, filtre, association) VALUES('', '$filtre', '$association')"; 
 		$requete = mysql_query($sql);
	
		// pour le debug
		$sql = "SELECT * FROM correspondance WHERE filtre = 'Consommation_d_eau_et_production_d_energie_par_jour' ";
		$requete = mysql_query($sql);
		$result = mysql_fetch_assoc($requete);	
	
		echo " >>> : " .$result['association'];
	  }
	else {
     		echo "L'enregistrement existe !.";
	    }

	
	
	
	/*$connexion = mysql_connect($db_host,$db_user,$db_pass); 
     	mysql_select_db($db_name, $connexion);
	$sql = "INSERT INTO correspondance( id, filtre, association) VALUES('', '$filtre', '$association')"; 
 	$requete = mysql_query($sql);
	
	// pour le debug
	$sql = "SELECT * FROM correspondance WHERE filtre = 'Consommation_d_eau_et_production_d_energie_par_jour' ";
	$requete = mysql_query($sql);
	$result = mysql_fetch_assoc($requete);	
	
	echo " >>> : " .$result['association'];*/
	
	
/*	CREATE TABLE correspondance (
	id int(11) NOT NULL AUTO_INCREMENT,
	table VARCHAR( 25 ) NOT NULL ,
	filtre VARCHAR( 255 ) NOT NULL ,
	association VARCHAR( 255 ) NOT NULL ,
	PRIMARY KEY ( id )
	);
*/	
	
	/*$str = "Niveau sonore en db";
	$val = compacter($str);
	echo $val;
	
	echo "<br>";

	$str = "Consommation d'eau et production d'énergie par jour";
	$val = compacter($str);
	echo $val;
	
	echo "<br>";
	
	//$str = "Consommation d'eau et d'energie";
	//$val = compacter($str);
	//echo $val;
	
	echo "<br>";
	
	$str = $val;
	$val = decompacter($str);
	echo $val;
	
	echo "<br>";
	
	$chaine = "L'équipe de Toulouse"; 
	$val = supprimer_caracteres($chaine);
	echo $val;*/
	
	//echo ">>>>>> :" .$ch;
	//echo "<br>";
	
	// reconstitution de la chaine
	//$result = implode(" ", $tab);
	
	// reconstitution de la chaine
	//$built = implode(" ", $tab);

	//echo $built;
	
	//$str = "Votre nom est-il O'reilly ?";

	// Affiche : Votre nom est-il O\'reilly ?
	//echo addslashes($str);
	
	//$str = "Consommation_d_eau";
	
	// remplace les apostrophes par des underscore
	//$result = str_replace ( "'" , "_", $str);
	
	// remplace les espace par des underscore
	//$result = str_replace ( " " , "_", $result);
	
	//return result;
	
	//echo $result; 


/*	CREATE TABLE IF NOT EXISTS correspondance (
	id int(11) NOT NULL AUTO_INCREMENT,
	filtre VARCHAR( 255 ) NOT NULL ,
	association VARCHAR( 255 ) NOT NULL ,
	PRIMARY KEY ( id )
	);
*/


?>


