<?php

	session_start();
	
	include('config_inc.php'); 
	
	
	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";
	 }

	
	$id = $_GET['id'];
	$table= $_GET['table'];
	
	$connexion = mysql_connect($db_host,$db_user,$db_pass); 
	mysql_select_db($db_name, $connexion);
	
	$requete = "SELECT * FROM $table WHERE id_catalogue='$id' ";
	$result = mysql_query($requete);
	$data = mysql_fetch_array($result);
	
	$caracteristiques = $data['caracteristiques'];  
	
	if(isset($_POST['mise_a_jour']) && $_POST['mise_a_jour'] == "Rafraichir")
	  { 	
	       //echo $_POST['boite'];
	       
	       //print_r($_POST['choix']);
	       
	       $decompose = array();
	       
	       foreach($_POST['choix'] as $val)
	              {  $decompose[] = substr($val, 6, 3);
	              }
	       
	       print_r($decompose);
	      
	       for($i=0; $i<=count($decompose)-1; $i++)
		  {  // parser le tableau 
		  	echo "lulu"; 
	       		for($j=0; $j<=count($_SESSION['position_titre'])-1; $j++)
		           {  echo $decompose[$i] .$_SESSION['position_titre'][$j];
		              echo "<br>";
		             if($_SESSION['position_titre'][$j] == $decompose[$i] )
		               { echo "--> " .$decompose[$i];
		  	         echo "<br>";
		               }	
	       		   }
	       	  }	   
	  }	
	
	include('menu.php'); 
	
	if(isset($_POST['soumettre']) && $_POST['soumettre'] == "Enregistrer")
	  { 
	  	$caracteristiques = $_POST['caracteristiques'];
	  	 
	  	$caracteristiques = rtrim($caracteristiques, " \t."); // tabulation
	  	$caracteristiques = rtrim($caracteristiques, " \n."); // nouvelle ligne
	  	$caracteristiques = rtrim($caracteristiques, " \r."); // retour chariot
	  	// supprime les balises html
	  	//$caracteristiques = strip_tags($caracteristiques);
	  	
	  	$lignes  =  explode("\n",$caracteristiques);
	  	
	  	//print_r($lignes); 
	  	/*echo "++++++++++";
	  	echo "<br>";
	  	print_r($lignes); 
	  	echo "<br>";
	  	echo "++++++++++";
	  	echo "<br>";
	  	echo "++++Filtre++++++";
	  	echo "<br>";
	  	print_r(array_filter($lignes));
	  	echo "<br>";*/
	  	
	  	for($i=0; $i<=count($lignes)-1; $i++)
		  {   
		      if(!empty($lignes[$i]))
		      //if($lignes[$i] != "")	
		        {   if($i == 0)
		              {$chaine = $lignes[$i] ;
                  	      }
                	    if($i != 0)
		              {
			       $chaine = $chaine ."," .$lignes[$i];
			      }
			}       	
		  }	
	  	
	  	
	  	echo "<br>";
	  	echo "<br>";
	  	
	  	echo $chaine;
	  	
 		//$caracteristiques = addslashes($caracteristiques);
	  	
	  	$connexion = mysql_connect($db_host,$db_user,$db_pass); 
	 	mysql_select_db($db_name, $connexion);
	 	
	 	$sql = "UPDATE $table SET caracteristiques = '$chaine' WHERE id_catalogue='$id' ";
 		$requete = mysql_query($sql);
 		
 	  }	

////////// formatage

		/*echo "+++++++";
		echo "<br>";
		echo $caracteristiques;
		echo "<br>";
		echo "+++++++";*/

		$motif_titre = 'TITRE';
		$remplacement = "";

		// decoupe en sous element
		$morceau = explode(",", $caracteristiques);

		for($i=0; $i<=count($morceau)-1; $i++)
		  {   
		       $portion = explode("&", $morceau[$i]);
		       array_push($_SESSION['format'], $morceau[$i]);
		       //echo $morceau[$i];
		       //echo "<br>";
		       
		  }	
		  
		  //print_r($_SESSION['format']);

		  $chaine = "";

		  for($i=0; $i<=count($_SESSION['format'])-1; $i++)
		  { 
				if($i<count($_SESSION['format'])-1)
				  {$chaine = $chaine .$_SESSION['format'][$i] ."\n";
				   //echo "normal " .$_SESSION['format'][$i];
				   //echo "<br>";
			          }
			        if($i==count($_SESSION['format'])-1)
				  {$chaine = $chaine .$_SESSION['format'][$i];
				    // echo "final " .$_SESSION['format'][$i];
				   //echo "<br>";

			          }  
		  }

////////// formatage

?>
		<div class="heading">Caracteristiques de l'article</div><br>
		<form action="" method="post" id="ajouter_produit" name="ajouter_produit" >
		<table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="800">

		<tr bgcolor="#F1F3F5">
		<td colspan="2">&nbsp;</td>
		</tr>
		</tr>
		
		<tr bgcolor="#FFFFFF">
		<td align="right" valign="top">Zone de saisie :</td>
		<td align="left" valign="middle" colspan="1">
		<textarea name="caracteristiques" rows="12" cols="90" id=""><?php echo $chaine; ?>

		</textarea>&nbsp;</td>
		</tr>
		
		<tr bgcolor="#F1F3F5">
		<td colspan="4" align="center">
		<input type="submit" name="soumettre" value="Enregistrer" />
		<input type="submit" name="mise_a_jour" value="Rafraichir" />
		</td>
		</tr>
		
		</table>

		</form>
		</div>

		</body>
		</html>

<?php

//$Licenses = str_replace("<br />", "\r\n", $Licences);

// rtrim
 /*   *    " " (ASCII 32  (0x20)), un espace ordinaire.
    * "\t" (ASCII 9 (0x09)), une tabulation.
    * "\n" (ASCII 10 (0x0A)), une nouvelle ligne (line feed).
    * "\r" (ASCII 13 (0x0D)), un retour chariot (carriage return).
    * "\0" (ASCII 0 (0x00)), le caractère NUL.
    * "\x0B" (ASCII 11 (0x0B)), une tabulation verticale.*/




?>	