<?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 = "orange";
  
  	$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]);

	print_r($champs_filtre);
	
	echo "<br>";
	echo "Liste des critéres de filtrage";
	echo "<br>";
	
	for($i=0; $i<=count($champs_filtre)-1; $i++) 
	    {   echo $champs_filtre[$i];
		echo "<br>";
	    }   	
    	    	
    	$index = $champs_filtre[0];
    	//echo $index;
    	
    	
    	// declaration tableau à 2 dimensions
    	$tab2dim = array(array()) ;
    	
    	
	$table = "cruchot";
    	
    	$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>"; 
	    		
	     }
    		
    		
         // 
         echo count($tab); 
    	    	
   	 print_r($tab);
     	
     	if(isset($_POST['soumettre']) && $_POST['soumettre'] == "Filtre")
	  { 
    	    //header("Location: filtre_resultat.php?tab=$tab");
	
	    /*echo "Bingo";
	  
	    echo "<br>";
	    print_r($_POST['choix']);
	    echo "<br>";*/
	    
	    $decompose = array();
	       
	    foreach($_POST['choix'] as $val)
	           {  $decompose[] = substr($val, 6, 3);
	           }

	    echo "<br>";   
	    print_r($decompose);
	    echo "<br>";
	    
	    // construire le requete
	    
	    $portion ="";
	    
	    for($i=0; $i<=count($decompose)-1; $i++) 
	    {   //echo $tab[$decompose[$i]];
	    	$st = $tab[$decompose[$i]];
	    	if($i != count($decompose)-1)
	    	  { $portion = $portion ."chargement =" ."'$st'" ." or ";
	          }
	        if($i == count($decompose)-1)
	    	  { $portion = $portion ."chargement =" ."'$st'";
	          }  
	    }
	    
	    
	    $req = " SELECT * FROM cruchot WHERE " .$portion;
	    //$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>"; 
	    
	    while( $list = mysql_fetch_array( $result) ) 
	    {   echo "<br>"; 
  		echo $list['id_catalogue'];
  		echo "<br>"; 
	    }	  
	    
	    echo "<br>"; 
	    echo "<br>"; 
	    echo "<br>"; 
	    echo "<br>"; 
	    
     	
     	}
     	
     	
	


?>

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

<?php
	 for($i=0; $i<count($tab); $i++) 
	    {   $val_ligne = "ligne_" .$i;
	    	echo " <input type=\"checkbox\" name=\"choix[]\" value=\"$val_ligne\" >$tab[$i] &nbsp ";
	    }
?>

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


<?php    	 
    	 
    	 /*<form method='post' action='process.php'>
<input type = 'checkbox' name = 'vehicle[]' value = 'car' checked= 'checked'  />Car<br />
<input type = 'checkbox' name = 'vehicle[]' value = 'pickup' />Pickup Truck<br />
<input type = 'checkbox' name = 'vehicle[]' value = 'van' />Full Size Van<br />
<input type = 'checkbox' name = 'vehicle[]' value = 'minivan' />Mini Van<br />
<input type = 'checkbox' name = 'vehicle[]' value = 'suv' />SUV<br />
<input type = 'submit' value ='Enter Data'>
</form>*/
   	
    	    	
    	    	
	//$requete = "SELECT * FROM categorie "; 

    	//$result = mysql_query($requete);
    	//$datas = mysql_fetch_array($result);
    	//$datas = mysql_fetch_assoc($result);
    	
    	//print_r($datas['$index']);
    	//print_r($datas);
    	    	
    	
    	// on fait une boucle qui va faire un tour pour chaque enregistrement
	/*while($data = mysql_fetch_array($req))
    	{
    		
    	}*/	
    	
    	
    	
    	
    	//print_r($champs_filtre);
    	




    	//echo $champs_filtre[0];
    	
    	

    	
//	$designation =  $infos_produit[2];
	
//	echo $designation;



	//caracteristiques


       	//$sql = 'SELECT marque,modele FROM lave_linge'; 
  
	//$result = mysql_query($sql);
	
	//print_r($result);

	//var_dump($result);


	
        
        
  	/*    	$infos_produit = mysql_fetch_array($result, MYSQL_NUM);
  	    	$designation =  $infos_produit[2];
		$reference =  $infos_produit[3];
		$marque =  $infos_produit[4];
		
		$garantie =  $infos_produit[6];*/




?>


<?php

/*<form method='post' action='process.php'>
<input type = 'checkbox' name = 'vehicle[]' value = 'car' checked= 'checked'  />Car<br />
<input type = 'checkbox' name = 'vehicle[]' value = 'pickup' />Pickup Truck<br />
<input type = 'checkbox' name = 'vehicle[]' value = 'van' />Full Size Van<br />
<input type = 'checkbox' name = 'vehicle[]' value = 'minivan' />Mini Van<br />
<input type = 'checkbox' name = 'vehicle[]' value = 'suv' />SUV<br />
<input type = 'submit' value ='Enter Data'>
</form>*/





?>