<?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";
	  }
     	
     	$connexion = mysql_connect($db_host,$db_user,$db_pass); 
       	mysql_select_db($db_name, $connexion);
  
  	$table = "washer"; 
  
  	$requete = "SELECT filtre FROM categorie WHERE table_article='$table'";	

    	$result = mysql_query($requete);
    	$datas = mysql_fetch_array($result);
    	    	
    	// decoupe la chaine
    	$champs_filtre = explode(",", $datas[0]);
	
	// declaration tableau à 2 dimensions
    	$tab2dim = array(array()) ;
	
	for($i=0; $i<=count($champs_filtre)-1; $i++) 
	    {   		
			    	$index = $champs_filtre[$i];
    				//echo $index;
    	
    				$connexion = mysql_connect($db_host,$db_user,$db_pass); 
       				mysql_select_db($db_name, $connexion);
    				$sql = "SELECT $index FROM $table "; 
    				//echo $sql;
    				$result = mysql_query($sql);
  	
  				// déclarer un tableau vide
  				$tab = array();
  	
  				while($data=mysql_fetch_row($result))  
             			{
             	   			if(in_array($data[0], $tab))
             	    			  { // existe
             	    				$nada = True;
		    			  }   
		    		     else { // nouveau : on le stocke
		    	   		    $tab[] = $data[0];
		    	                  }
			    		  // echo $data[0];
    		  			  // echo "<br>"; 
	    		
	     			}
        			// sauve dans le tableau multidimensionnel
        			$tab2dim[] = $tab;
        			 
         			//echo count($tab); 
    	    			// print_r($tab);
			   	// echo "<br>"; 
			   	 
		}	   	 

	//echo "<br>"; 			   	 
	//var_dump($tab2dim);
	//echo "<br>"; 
	//var_dump($tab2dim[1]);
	//echo "<br>"; 
	//echo count($tab2dim[1]);
     	
     	if(isset($_POST['soumettre']) && $_POST['soumettre'] == "Filtre")
	  { 
	    $decompose = array();
	       
	    foreach($_POST['choix'] as $val)
	           {  //$decompose[] = substr($val, 6, 3);
	
		     	for($i=0; $i<=count($champs_filtre)-1; $i++) 
	                   { 
	                   	if(ereg($champs_filtre[$i] ,$val))
	                   	  { $longueur = strlen($champs_filtre[$i]);
	                   	  	$position = substr($val,$longueur ,2);
	                   	  	$decompose[] = $champs_filtre[$i] ."=" .$tab2dim[$i+1][$position];
	                   	  }	
	                   }	  
		}

	    
	    echo "<br>";   
	    print_r($champs_filtre);
	    echo "<br>";
	    
	    echo "<br>";   
	    print_r($decompose);
	    echo "<br>";
	    
	    // groupement
	    $champs = array();
	    $stocke_sql = array();
	    
	    for($i=0; $i<count($champs_filtre); $i++) 
	       { $ptr = 0; 
	         $chaine = "";
	         $final = "";
	         
	         unset($champs);
	       	 for($j=0; $j<count($decompose); $j++) 
	    	     {  $decoupe = explode("=", $decompose[$j]);
	    	     	if($champs_filtre[$i] == $decoupe[0])
	    	         { $ptr = $ptr + 1;
	    	           $champs[] = $decoupe[1];
	    	     	 }
	    	     }	 
	    	     
	    	     if($ptr == 0)
	    	       { echo "---> " . $ptr  ;
	    	         echo "<br>";
	    	       }	
		     else { echo "---> " . $ptr  ."  " .print_r($champs) ;
	    	            echo "<br>";
			    for($k=0; $k<=count($champs)-1; $k++) 
	    	               { 
	    	               	  if($k<count($champs)-1)
				    {$chaine = $chaine ."'$champs[$k]'" .", ";
			            }
			         if($k == count($champs)-1)
				   {$chaine = $chaine ."'$champs[$k]'";
			           }  
			       }	
				//  marque IN ('Brandt', 'Bosh' )
	    	               	$final = $champs_filtre[$i] ." IN (" .$chaine .")";
	    	               	$stocke_sql[] = $champs_filtre[$i] ." IN (" .$chaine .")";
				echo $final;
	    	                echo "<br>";

	    	          }     
	    }
	    
	    
	    // construire le requete
	    
	    $portion ="";
	    $chaine = "";
	    
	    for($k=0; $k<=count($stocke_sql)-1; $k++) 
  	       { 
  	       	  if($k<count($stocke_sql)-1)
		    {$chaine = $chaine .$stocke_sql[$k] ." and ";
	            }
	         if($k == count($stocke_sql)-1)
		   {$chaine = $chaine .$stocke_sql[$k];
	           }  
	       }
	       
	        $req = " SELECT * FROM $table WHERE " .$chaine;
	    	
	    	
	    
	    //// ancienne mouture	
	    for($i=0; $i<=count($decompose)-1; $i++) 
	    {   //echo $tab[$decompose[$i]];
	    	$decoupe = explode("=", $decompose[$i]);
	    	if($i != count($decompose)-1)
	    	  { $portion = $portion ."$decoupe[0]" ." = " ."'$decoupe[1]'" ." and ";  // test and à la place de or
	          }
	        if($i == count($decompose)-1)
	    	  { $portion = $portion ."$decoupe[0]" ." = " ."'$decoupe[1]'";
	          }  
	    }
	    //////
	    
	    //$req = " SELECT * FROM $table WHERE " .$portion;
	    
	    //$req = "SELECT * FROM washer WHERE marque IN ('Brandt', 'Bosh') and classe_efficacite_essorage IN ('A+++') ";
	    //$req = "SELECT * FROM cruchot WHERE chargement ='Hublot' or chargement ='deux'";
	    
	    echo $req;
	    
	    $connexion = mysql_connect($db_host,$db_user,$db_pass); 
       	    mysql_select_db($db_name, $connexion);
	    
	    //$requete = " SELECT * FROM cruchot WHERE chargement = 'Hublot' or chargement = '2' ";
	    $result = mysql_query($req);
	    
	    echo "<br>"; 
	    echo "Nb de resultat:" .mysql_num_rows($result); 
	    echo "<br>"; 
	    
	    
	    $nb_result = mysql_num_rows($result);
    	
    	    if($nb_result != 0)
	      { 	    	
    	    	while($datas = mysql_fetch_assoc($result))
                     {
                        echo $datas['id_catalogue'];
                        echo "<br>"; 
                     }
              }
          else { echo "Aucune réponse";} 
	    
	    echo "<br>"; 
	    echo "<br>"; 
     	
     	}

?>
	 <form method='post' action=''>

<?php
	 
	
	 
         // attention le double tableau commence en 1 et pas en 0
	 for($i=1; $i<=count($champs_filtre); $i++) 
	    {   $taille = count($tab2dim[$i]);
	    	for($j=0; $j<$taille; $j++) 	
	 	   { $nom =  $tab2dim[$i][$j];
	    	     $val_ligne = $champs_filtre[$i-1] .$j;
       		     
       		     if(in_array($val_ligne, $_SESSION['checkbox'] ))
          	       { $nada = True;
       		       }
       		     else { array_push($_SESSION['checkbox'],$val_ligne);
       	   	          }
			  
                               for($k=0; $k <count($_POST['choix']); $k++) 	
  				   {                           	
                             	      if($val_ligne == $_POST['choix'][$k])
                                        { $flag = true;
                                          //echo "ok";
                                          //echo "<br>"; 
                                          break;
	                                }	
                                   else { $flag = false; } 	
                                  }
			     
       		     
       		     if($flag == true)
       		       {  echo " <input type=\"checkbox\" name=\"choix[]\" checked=\"checked\" value=\"$val_ligne\" >$nom";
       		       }
       		     if($flag == false)
       		       {  echo " <input type=\"checkbox\" name=\"choix[]\" value=\"$val_ligne\" >$nom";
       		       }         		       
       		       
    		   }
    		   echo "<br>"; 
	   } 	
	   	   
?>

	 <input type = 'submit' name = "soumettre" value ='Filtre'>
	 </form> 	


<?php    	 

	 echo "<br>";
         print_r($_SESSION['checkbox']);
	 echo "<br>";

?>