<?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'];


		
		if(isset($_POST['submit']) && $_POST['submit'] == "Enregistrer")
		   {
	   		//header("Location: traite.php");
		        
		        $chaine = "";
			
			// recuperer toutes les infos et traitement
			for($i=0; $i<$_POST['nb_champs']; $i++)
		   	  {
		      		$champs = "champs_" .$i;	
		      		$saisie = "saisie_" .$i;	
		      
		      		//echo $_POST[$champs] ."    " .$_POST[$saisie];
		      		//echo "<br>";
		      		
				// detection d'un titre 
		      		if($_POST[$saisie] == "")
		       	     	  { $_POST[$saisie] = "vide";
		       	     	    $_POST[$champs] = strtoupper($_POST[$champs]);      
		      
		       	     	  } 

		      		if($i == 0)
				  { $chaine = $_POST[$champs] ." & " .$_POST[$saisie] ;
				  }	

				if($i != 0)
				  { $chaine = $chaine ."," .$_POST[$champs] ." & " .$_POST[$saisie] ;
				  }	

		      
		          } 
		          
			echo $chaine;

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

			
			$sql = "SELECT * FROM $table WHERE id_catalogue='$id' ";
			$result = mysql_query($sql);
			$data = mysql_fetch_array($result);
			$id_catalogue = $data['id_catalogue'];



			// enregistrement
			
 		    $sql = "UPDATE $table SET image = '$name_file' WHERE id_catalogue='$id' ";
 		           $requete = mysql_query($sql);




		   }

		 include('menu.php'); 

			// 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']++;
			    // exite déjà 
			    if(in_array($_POST['boite'], $_SESSION['checkbox']) )
			      { array_push($_SESSION['checkbox'],"vide");
			      }
			    else {  // nouveau	
			    	    array_push($_SESSION['checkbox'],$_POST['boite']);
			         }
			  }
			
			// si la personne clic sur "un champs en -", on enléve un champs
			if(isset($_POST['submit']) && $_POST['submit'] == "Un champs en -")
			  {
			    array_splice($_SESSION['checkbox'], $_POST['nb_champs'], 1); 	
			    $_POST['nb_champs']--;
			  }  
			  
			  var_dump($_SESSION);
			 
?>

		<div class="heading">Insertion des nouveaux champs </div>
		<form action="" method="post" id="ajouter_champs" name="ajouter_champs" >
		<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>
		<td colspan="3">Titre</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=0; $i<$_POST['nb_champs']; $i++)
		   {
		      $champs = "champs_" .$i;	
		      $saisie = "saisie_" .$i;	
		      $val_titre = "titre_" .$i;	
		      
		      
		      if(isset($_POST[$champs]) && !empty($_POST[$champs]) ) 
	                {		      
		       	      $val_champs = $_POST[$champs];
		   	}
		      else { $val_champs = ""; }	
		   	
		      if(isset($_POST[$saisie]) && !empty($_POST[$saisie]) ) 
	                {		      
		       	      $val_saisie = $_POST[$saisie];
		       	      
		   	}	
		      else { $val_saisie = ""; }	
		      
		   	
		      echo "<tr bgcolor=\"#FFFFFF\">
		      
			    <td align=\"left\" valign=\"middle\" colspan=\"1\"> 
			    <input type=\"text\" name=$champs size=\"30\" maxlength=\"20\" value=\"$val_champs\" id=\"\" >&nbsp;
			    </td> 
			    
			    <td align=\"left\" valign=\"middle\" colspan=\"2\">
			    <input type=\"text\" name=$saisie size=\"30\" maxlength=\"20\" value=\"$val_saisie\" id=\"\" >&nbsp;
			    </td> ";

			///// premier	 
			if($_POST['nb_champs'] == 1)
		          {   echo " <td align=\"left\" valign=\"middle\" colspan=\"3\"> 
			             <input type='checkbox' name=\"boite\" value=\"$val_titre\">init&nbsp;
			    	     </td> 
				     </tr> ";	
			  }
			
			if($_POST['nb_champs'] > 1)
			   { 
			     ///// dernier	 
			     if($_POST['nb_champs']-1 == $i)
		               {   echo " <td align=\"left\" valign=\"middle\" colspan=\"3\"> 
			                  <input type='checkbox' name=\"boite\" value=\"$val_titre\">dernier&nbsp;
			    	          </td> 
				          </tr> ";	
			       }
			       
			     /// intermediaire
			     else { if($_POST['boite'] != "")
			              { if($_SESSION['checkbox'][$i] != "vide")
			                  {   echo " <td align=\"left\" valign=\"middle\" colspan=\"3\"> 
			                      <input type='checkbox' name=\"boite\" value=\"$val_titre\" checked=\"checked\">$i &nbsp;
			                      </td> 
			                      </tr> ";	
				          }
				        else { 
			    	       		echo " <td align=\"left\" valign=\"middle\" colspan=\"3\"> 
			    	              	       <input type='checkbox' name=\"boite\" value=\"$val_titre\">$i &nbsp;
			    	                       </td> 
				                       </tr> ";	
				             }      
			     	
			     	      }				     
			     	}
			       
			   }

				  
		   	
		    }
		    
		    
		echo " <tr bgcolor=\"#F1F3F5\">
		       <td colspan=\"2\" align=\"center\">
		       <input type=\"submit\" name=\"submit\" value=\"Enregistrer\" />
		       <input type=\"submit\" name=\"submit\" value=\"Un champs en +\" />
       		       <input type=\"submit\" name=\"submit\" value=\"Un champs en -\" />
		       </td>

		       </tr>
		       </form> ";

?>


	