<?php

	 session_start();
	 
	 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";
	 }


?>

		<!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">
  
		<link rel="stylesheet" href="default.css" type="text/css">


	</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>		

		<?php include('menu.php'); ?>

<?php
		// si l'utilisateur soumet le formulaire on affiche la valeur de tous les champs duformulaire
		
		if(isset($_POST['submit']) && $_POST['submit'] == "Suite")
		   {
			$chaine = "";

			for ($i=1; $i<=$_POST['nb_champs']; $i++)
			{
			  $ptr_champs = 'champs_'.$i;
			  $value1 = $_POST[$ptr_champs];
  			  $ptr_saisie = 'saisie_'.$i;
			  $value2 = $_POST[$ptr_saisie];
			  echo 'Valeur du champ '.$i.' : '.$value1.' '.$value2.'  <br />';
			  
			  if($i==1)
	                    { $chaine = $value1 ." " .$value2 ;
	                    }	
	               else {
		              $chaine = $chaine .", " .$value1 ." " .$value2 ;	
			    }
			  
			}  
			  
			  //$connexion = mysql_connect("localhost", "root", "");
			$connexion = mysql_connect($db_host,$db_user,$db_pass); 
			mysql_select_db($db_name, $connexion);
			  
		  	$identifiant="zorro";
		  
			$sql = "INSERT INTO descriptif( id, template, datas)  VALUES('', '$identifiant', '$chaine')"; 
 
		 	$requete = mysql_query($sql);
			
			// pour le debug
			//$sql = "SELECT * FROM descriptif WHERE template = '".$identifiant."' ";
 			//$requete = mysql_query( $sql, $connexion ) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 
			//$result = mysql_fetch_assoc($requete);
			//echo($result['datas']) ;
			  
			  
			
		}

		// sinon on affiche le formulaire avec la possibilité d'ajout des champs au formulaire
	   else{

			// on défini le nombre initial de champs
			if (!isset($_POST['nb_champs']))
			{
			   $_POST['nb_champs'] = 1;
			}
			// si la personne clic sur "un champs en +", on ajoute un champs
			if(isset($_POST['submit']) && $_POST['submit'] == "Un champs en +")
			  {
			    $_POST['nb_champs']++;
			    
			     $selection_categorie = "rafraichir";
			  }
			
			// si la personne clic sur "un champs en +", on ajoute un champs
			if(isset($_POST['submit']) && $_POST['submit'] == "Un champs en -")
			  {
			    $_POST['nb_champs']--;
			  }  
			  
			  
?>



		<div class="heading">Descriptif de la catégorie</div>

		<form method="post">
		
		<table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="800">
		<tr bgcolor="#F1F3F5">
		<td colspan="1">Elements</td>
		<td colspan="2">Propriétés</td>
		
		</tr>
		</tr>
		</td>
		</tr>
		
		
<?php		
		// on place un champ caché contenant un entier ayant comme valeur le nombre dechamps du formulaire
		echo '<INPUT TYPE="hidden" NAME="nb_champs" VALUE="'.$_POST['nb_champs'].'">';
		
		// on affiche tous les champs du formulaire

		for($i=1; $i<=$_POST['nb_champs']; $i++)
		   {
		      $champs = "champs_" .$i;	
		      $saisie = "saisie_" .$i;	
		      
		      if(isset($_POST['champs']) && !empty($_POST['champs']) ) 
	                {		      
		       	      $val = $_POST['champs'];
		   	}
		   	else { $val ="644";}
		   	
		      echo "<tr bgcolor=\"#FFFFFF\">
			    <td align=\"left\" valign=\"middle\" colspan=\"1\"> ";
?>			    
			    
			    <input type="text" name=$champs size="30" maxlength="20" value=<?php echo $val; ?> id="" >&nbsp;</td> 

<?php			    
		echo "	    <td align=\"left\" valign=\"middle\" colspan=\"1\">
			    <input type=\"text\" name=$saisie size=\"30\" maxlength=\"20\" value=\"\" id=\"\" >&nbsp;</td>
			    </tr> ";	
		   	
		      //echo '<INPUT TYPE="text" name="champs_'.$i.'"><br />';
		    }
		    
		    
		echo " <tr bgcolor=\"#F1F3F5\">
		       <td colspan=\"2\" align=\"center\">
		       <input type=\"submit\" name=\"submit\" value=\"Suite\" />
		       <input type=\"submit\" name=\"submit\" value=\"Un champs en +\" />
       		       <input type=\"submit\" name=\"submit\" value=\"Un champs en -\" />

		       
		       </td>
		       </tr> ";
		    
		// on place un bouton permettant de rajouter un champs
		//echo '<INPUT TYPE="submit" NAME="submit" VALUE="Un champs en +"><br />';
		// on place un bouton permettant de soumettre le formulaire
		//echo '<INPUT TYPE="submit" NAME="submit" VALUE="Envoyer">';
		echo '</form>';
		
		
}
?>