<?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";
	 }
    	
	$table = $_GET['table'];
	
	if(isset($_POST['valide']))
	      {
	 	  if($_POST['valide'] == "ok")
	     	    {
	     	    	$connexion = mysql_connect($db_host,$db_user,$db_pass); 
	 		mysql_select_db($db_name, $connexion);
			$sql = "SELECT * FROM categorie WHERE table_article = '".$table."' ";
 			$requete = mysql_query( $sql, $connexion ) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 
			$result = mysql_fetch_assoc($requete);
			$infos_descriptif = $result['descriptif'];
			
			$morceau = explode(",", $infos_descriptif);
			
			$motif_filtre = '(*)';
			$remplacement = "";
			
			$tableau_champ = array();
			$tableau_saisie = array();
		
		
			for($i=0; $i<=count($morceau)-1; $i++)
		  	   {   // il s'agit d'un titre
		  		
		  	       if(preg_match('`\*`', $morceau[$i]))
			  	 {  $champ = str_replace($motif_filtre, $remplacement, $morceau[$i]); 
			            $tableau_champ[]= $champ; 
			            
          		       	    $id = "description" .$i;
		       	     	    $saisie = $_POST[$id]; 
		       	     	    if($saisie == "")
		       	     	      { $saisie = "vide";
		       	     	      } 
		       	     	    $tableau_saisie[] = $saisie;  	
			            
			        }	
		  		
		  	  }	
		  		
		  		//print_r($tableau_champ);
		  		//echo "<br>";
		  		//echo count($tableau_champ);
		  		//echo "<br>";
		  		//print_r($tableau_saisie);
		  		
	     	    	
	     	    	$sql = "SELECT * FROM $table";
			$result = mysql_query($sql);
			
			$total = mysql_num_rows($result);
			$colonnes = mysql_num_fields($result);
			
			//echo $colonnes;
			
			$chaine = "";
			
			for($i=0;$i<$colonnes;$i++)
			   {
			       if($i == 0)
			         {	
		                    $chaine = mysql_field_name($result,$i);
			         }
			       if($i != 0)
			         {	
		                    $chaine = $chaine .", " .mysql_field_name($result,$i);
			         }  
			         
			   }
	     	    	
	 	
		$p1 = "INSERT INTO $table (id, id_catalogue, designation, reference, marque, fournisseur, garantie,dispo, prix_ttc, prix_ht,
		 remise, tva, descriptif, caracteristiques, image ,stock, status, remarques, liens, heure, date,";
	 		
		for($i=0; $i<=count($tableau_champ)-1; $i++)
	           {  
	           	if($i == 0)
			  { $p2 = $tableau_champ[$i];
			    $p3 = "'" .$tableau_saisie[$i] ."'";
			  }
			if($i != 0)
			  { $p2 = $p2 .", " .$tableau_champ[$i];
			    $p3 = $p3 .", " ."'" .$tableau_saisie[$i] ."'";
			  }  
	           }	 		
	 		
	 		//echo $p3;
	 		
	 		//echo "<br>";
	 		//echo "<br>";
	 		
	 		$p4 = $p1 .$p2 .")";
	 		
	 		$i1 = $_SESSION['id_catalogue'];
	 		$i2 = $_SESSION['designation'];
	 		$i3 = $_SESSION['reference'];
	 		$i4 = $_SESSION['marque'];
	 		$i5 = $_SESSION['fournisseur'];
	 		
	 		$i6 = $_SESSION['garantie'];
	 		
	 		$i7 = $_SESSION['dispo'];
	 		$i8 = $_SESSION['prix_ttc'];
	 		$i9 = $_SESSION['prix_ht'];
	 		$i10 = $_SESSION['remise'];
	 		$i11 = $_SESSION['tva'];
			$i12 = $_SESSION['descriptif'];
			$i12 = addslashes($i12);
			
			$i13 = ""; // caracteristiques
			
			$i14 = ""; // image
			
	 		$i15 = $_SESSION['stock'];
	 		$i16 = $_SESSION['status'];

	 		$i17 = $_SESSION['remarques']; 
	 		$i17 = addslashes($i17);
	 		
	 		$i18 = $_SESSION['liens']; 
	 		$i18 = addslashes($i18);

	 		$i19 = date("H:i"); 
	 		$i20 = date("d-m-Y"); 
	 		
	 		//$p5 = " VALUES ('', '$i1', '$i2', '$i3', '$i4', '$i5', '$i6', '$i7', '$i8', '$i9', '$i10', '$i11', '$i12', '$i13', '$i14', '$i15', '1', '2', '3', '4', '5' )";
			$p5 = " VALUES ('', '$i1', '$i2', '$i3', '$i4', '$i5', '$i6', '$i7', '$i8', '$i9', '$i10', '$i11', '$i12', '$i13', '$i14', '$i15', '$i16', '$i17', '$i18', '$i19', '$i20',";
			
			$p5 = $p5 .$p3 .")";	 	
	 	        
	 		$p6 = $p4 .$p5;
	 		
	 		//echo $p6;

			$connexion = mysql_connect($db_host,$db_user,$db_pass); 
			mysql_select_db($db_name, $connexion);
			mysql_query($p6);


////////////////////// sauver le descriptif 


			$connexion = mysql_connect($db_host,$db_user,$db_pass); 
	 		mysql_select_db($db_name, $connexion);
			$sql = "SELECT * FROM categorie WHERE table_article = '".$table."' ";
 			$requete = mysql_query( $sql, $connexion ) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 
			$result = mysql_fetch_assoc($requete);
			$infos_descriptif = $result['descriptif'];
			
			$morceau = explode(",", $infos_descriptif);
			
			$motif_titre = 'TITRE';
			$remplacement = "";
 	
 			for($i=0; $i<=count($morceau)-1; $i++)
		  	   {   // il s'agit d'un titre
		      		if( preg_match('/TITRE/',$morceau[$i]))
			  	  { $champs = str_replace($motif_titre, $remplacement, $morceau[$i]); 
			    	    //$champs = $morceau[$i];
			    	    $saisie = "TITRE"; //  "none";
			          }
		      
		       	     else{ // il s'agit d'une ligne de description
		       	     	    $nom = "description" .$i;
		       	     	    $champs = $morceau[$i];
		       	     	    $saisie = $_POST[$nom]; 
		                 }
			     
				if($i == 0)
				  { $chaine = $champs ." & " .$saisie ;
				  }	

				if($i != 0)
				  { $chaine = $chaine ."," .$champs ." & " .$saisie ;
				  }	
				               
		          }	
 		
 			//echo $chaine;
 			
		        $sql = "UPDATE $table SET caracteristiques = '$chaine' WHERE id_catalogue='$i1' ";
 		        $requete = mysql_query($sql);


			////////////////////// uploader image


	     	        header("Location: vignette.php?id=$i1&table=$table");


////////////////////// generer le page html


	     	        //header("Location: generation.php?id=$i1&table=$table");
	     	        
	     	}
	    } 	
	
	
	include('menu.php'); 
?>	

<!--
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html dir="ltr" lang="en">

<head>

<title>Administration - En cours de développement</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />

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

<meta http-equiv="Pragma" content="no-cache">
  
<link rel="stylesheet" href="default.css" type="text/css">
<script type="text/javascript" src="javascript.js"></script>
  
</head>
<body>

<div id="header"><h1>Administration</h1>
<div class="membre">Vous Ãªtes connectÃ© en tant que <b><?php echo $_SESSION['admin_identifiant']; ?> </b></div>
</div>
-->
 
 		<script type="text/javascript" src="javascript.js"></script>

 
		<div class="heading">Définitions des caractéristiques de l'article</div>
		<div id="list">

		<form action="" method="post" id="decrire" name="decrire" onsubmit="return toto();">
		  
		<table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5">
		
<?php			
		$connexion = mysql_connect($db_host,$db_user,$db_pass); 
	 	mysql_select_db($db_name, $connexion);
		$sql = "SELECT * FROM categorie WHERE table_article = '".$table."' ";
 		$requete = mysql_query( $sql, $connexion ) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 
		$result = mysql_fetch_assoc($requete);
		
		$infos_descriptif = $result['descriptif'];
		$infos_filtre = $result['filtre'];
		//echo($result['descriptif']) ;
		//echo "<br>";

		$morceau = explode(",", $infos_descriptif);
		
		$motif_titre = 'TITRE';
		$remplacement = "";
		
		for($i=0; $i<=count($morceau)-1; $i++)
		  {   // il s'agit d'un titre
		      if( preg_match('/TITRE/',$morceau[$i]))
			  {
			  	$str = str_replace($motif_titre, $remplacement, $morceau[$i]); 
			  	//$extraire = explode("-", $tab);
			    	//$str = strtoupper($extraire[0]);
			  	
			  	//$extraire = explode(" ", $morceau[$i]);
			    	//$str = strtoupper($extraire[1]);
			        
			     
			     echo " <tr bgcolor=\"#F1F3F5\">
			    	     <td colspan=\"2\" class=\"h1\" align=\"left\"> $str</td>
			             </tr> ";
			   }
		      
		       else{ // il s'agit d'une ligne de description
		       	     $nom = "description" .$i;
		       	     echo "   <tr bgcolor=\"#FFFFFF\">
			   	      <td align=\"right\" valign=\"top\">$morceau[$i] :</td>
			    	      <td align=\"left\" valign=\"middle\" colspan=\"1\">
			              <input type=\"text\" id=\"$nom\" name=\"$nom\" size=\"30\" maxlength=\"30\" value=\"\" id=\"\">&nbsp;</td>
		                      </tr> ";
		           }
		}	
			
?>
		
		<input type="hidden" name="valide" id="valide" value="">
		
		<tr bgcolor="#F1F3F5">
		<td colspan="2" align="center">
		<input type="submit" name="soumettre" value="Suite" />
		</td>
		</tr>
		
		</table>

		</form>
		</div>
	


</body></html>