<style type="text/css">
body {
background-color : #EEEEEE;

}

legend {
font: 11px Tahoma;
color : #2266AA;
padding : 0px 4px 0px 4px;
}

fieldset {
border : 1px solid #AAAAAA;
padding : 4px 4px 4px 4px;
}

/*
===========================
balise obligatoire
===========================
*/
.div_select {
border: 1px solid #00CCFF;
width : 150px;
background-color : #FFFFFF;
}

.div_select_header {
border-bottom: 1px solid #00CCFF;
background-color : #00CCFF;
}

.div_select_header td {
padding: 0px 6px 0px 6px;
font: 11px Tahoma;
font-weight: bold;
background-color : #00CCFF;
color: #FFFFFF;
}

.div_select_content td {
padding: 0px 6px 0px 6px;
font: 10px Tahoma;

}

.div_select_content {
width : 150px;
height : 80px ;
overflow : auto;
scrollbar-face-color: #DDDDDD;
scrollbar-shadow-color: #777777;
scrollbar-highlight-color: #ffffff;
scrollbar-3dlight-color: #eeeeee;
scrollbar-darkshadow-color: #000000;
scrollbar-track-color: #eeeeee;
scrollbar-arrow-color: #000000
}

.select_even {
cursor: hand;
background: #FFFFFF;
}

.select_odd {
cursor: hand;
background: #F2FAFF;
}

.select_over {
cursor: hand;
background-color : #CCEEFF;
color: #000000;
font-weight: bold;
}

.select_checked {
cursor: hand;
background-color : #CCFF99;
color: #000000;
font-weight: bold;
}

.div_cadre {
width: 600px;
text-align:center;
background-color: #FFFFFF;
border : 1px dashed #CCCCCC;
padding: 10px 20px 20px 20px;
}

</style>
<script language="javascript" type="text/javascript">

/**
* selectionne l'ensemble des elements d'une liste déroulante avec checkbox
* @param :
* - obj : le nom du pseudo-composant
* - nb : nombre d'elements que contient le composant
*/
function selectAll(obj, nb) {
for (i=0; i< nb; i++) {
document.getElementById(obj.id + i).checked = obj.checked;

if(obj.checked)
document.getElementById("tr_"+obj.id+i).className = "select_checked";
else {
if(i % 2)
css = "select_odd"
else
css ="select_even";

document.getElementById("tr_"+obj.id+i).className = css;
}
}
}

/**
* selectionne l'ensemble des elements d'une liste déroulante avec checkbox
* @param :
* - obj : le nom du pseudo-composant
* - el_id : id de l'element (position dans le composant)
* - ev : le type d'evenement
* - css_defaut : le style de la ligne par defaut
* - nb : le nombre d'elements
*/
function setEvenement(obj, el_id, ev, css_defaut, nb, provenance) {

//--- recuperation des balises
comp = document.getElementById(obj);
tr = document.getElementById("tr_" + obj + el_id);
ck = document.getElementById(obj + el_id);

//--- si il s'agit d'un clique
if(ev == "click") {
//--- alors on coche ou decoche la checkbox
if(provenance = "td") ck.checked = !ck.checked;


//--- si on decoche, alors on decoche aussi la checkbox du composant
if(!ck.checked) {
if(comp.checked) comp.checked = false;
}
else {

//---sinon on vérifie que tous les elements sont cochés
var absent = false;
var i = 0;

while(i < nb && absent == false) {
if(i != el_id) {
if(!document.getElementById(obj + i).checked) absent = true;
}
i++;
}

if(comp.checked == absent) comp.checked = !absent;
}
}

//--- autres evenements
switch(ev) {
case "over":
tr.className = "select_over";
break;

case "out":
if(ck.checked)
tr.className = "select_checked";
else
tr.className = css_defaut;
break;
}
}

</script>
</head>
<body>
<?php

/**
* permet de créer le pseudo-composant
* @param :
* - nom : le nom du composant
* - structure : les colonnes, les dimensions, les alignements (tableau)
* - contenu : les elements : valeur checkbox + autres colonnes (tableau)
* - selection : tableau de valeur a selectionner (correspond a la valeur de la checkbox)
*/



function checkedSelect( $nom, $structure, $contenu, $selection = null) {

//////////////// entete du composant \\\\\\\\\\\\\\\\\\\\\\\\\
if(sizeof($selection) == sizeof($contenu))
$checked = "checked";
else
$checked = "";


$str = '<div class="div_select">'
.'<div class="div_select_header">'
.'<table width="100%">'
.'<tr>'
.'<td width="20" align="center"><input type="checkbox" id="'.$nom.'" onClick="javascript:selectAll(this, '.sizeof($contenu).');" '.$checked.'/></td>';

//--- colonnes
for($i = 0, $max = sizeof($structure); $i < $max; $i++) {
$str .= '<td ';
if(isset($structure[$i]["align"])) $str .= 'align="'.$structure[$i]["align"].'" ';
if(isset($structure[$i]["width"])) $str .= 'width="'.$structure[$i]["width"].'" ';
$str .= '>'.$structure[$i]["name"].'</td>';
}

$str .= '<td width="1"></td>'
.'</tr>'
.'</table>'
.'</div>';

//////////////// contenu du composant \\\\\\\\\\\\\\\\\\\\\\\\\
$str .= '<div class="div_select_content" >'
.'<table width="100%">';

//--- pour chaque ligne
for($i = 0, $max = sizeof($contenu); $i < $max; $i++) {

//--- style css (pair / impair)
if($i%2)
$css_defaut = 'select_odd';
else
$css_defaut = 'select_even';

//--- element selectionne ?
if($selection != null && in_array($contenu[$i][0],$selection)) {
$checked = ' checked';
$css = 'select_checked';
}
else {
$checked = '';
$css = $css_defaut;
}

//--- la checkbox
$str .= '<tr id="tr_'.$nom.$i.'" class="'.$css.'" onMouseOver=\'javascript:setEvenement("'.$nom.'","'.$i.'","over","'.$css.'",'.sizeof($contenu).',"td");\' onMouseOut=\'javascript:setEvenement("'.$nom.'","'.$i.'","out","'.$css_defaut.'",'.sizeof($contenu).',"td");\' '
.' onClick=\'javascript:setEvenement("'.$nom.'","'.$i.'","click", "'.$css_defaut.'",'.sizeof($contenu).',"td");\' >'
.'<td width="20" ><input type="checkbox" id="'.$nom.$i.'" name="'.$nom.'[]" value="'.$contenu[$i][0].'" onClick=\'javascript:setEvenement("'.$nom.'","'.$i.'","click","'.$css_defaut.'",'.sizeof($contenu).',"ck");\' '.$checked.' /></td>';

//--- les colonnes
for($j = 1; $j < sizeof($contenu[$i]); $j++) {
$str .= '<td ';
if(isset($structure[$j-1]["align"])) $str .= 'align="'.$structure[$j-1]["align"].'" ';
if(isset($structure[$j-1]["width"])) $str .= 'width="'.$structure[$j-1]["width"].'" ';
$str .= '>'.$contenu[$i][$j].'</td>';
}

$str .= '</tr>';
}

//--- fermeture des balises
$str .= '</table>'
.'</div>'
.'</div>';

return $str;
}




	$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]);
    	
    	//var_dump($champs_filtre);
	
	$flag = false;
	   

	$critere01 = array(array());  
	$critere02 = array(array());  
	$critere03 = array(array());  
	$critere04 = array(array());  
	$critere05 = array(array());  
	$critere06 = array(array());  
	$critere07 = array(array());  
	$critere08 = array(array());  
	$critere09 = array(array());  
	$critere10 = array(array());  
	
	$item01 = "item01";
	$struct01 = array ( array("name" => "$champs_filtre[0]", "align" => "center", "width" => "40px"));
	
	$item02 = "item02";
	$struct02 = array ( array("name" => "$champs_filtre[1]", "align" => "center", "width" => "40px"));
	
	$item03 = "item03";
	$struct03 = array ( array("name" => "vitesse", "align" => "center", "width" => "40px"));
	
	$item04 = "item04";
	$struct04 = array ( array("name" => "$champs_filtre[3]", "align" => "center", "width" => "40px"));
	
	$item05 = "item05";
	$struct05 = array ( array("name" => "$champs_filtre[4]", "align" => "center", "width" => "40px"));
	
	$item06 = "item06";
	$struct06 = array ( array("name" => "$champs_filtre[5]", "align" => "center", "width" => "40px"));
	
	for($i=0; $i<count($champs_filtre); $i++) 
	   { 
		$connexion = mysql_connect($db_host,$db_user,$db_pass); 
       		mysql_select_db($db_name, $connexion);
  
  		$critere = $champs_filtre[$i];
  			
  		$requete = "SELECT $critere FROM $table";	
    		$result = mysql_query($requete);
    			
    		$p = 0;
    		$tempo = array();
    		
    		if($i == 0)
    		  { $tab = "critere01";
    		    $cpt = 0;
    		    while($data=mysql_fetch_row($result))  
             		{ 	if($cpt == 0)
             			  { $tempo[] = $data[0];
             			    $critere01[$p][0] = $data[0];
  				    $critere01[$p][1] = $data[0];
             	   		    $p = $p+1;
             			  } 
             			if($cpt > 0)
             			  {    $var = $data[0];
             			       if(in_array($var, $tempo))
             			         { $test = false;
				         }
             	   		    else {  $tempo[] = $var;
             	   			    $critere01[$p][0] = $data[0];
  					    $critere01[$p][1] = $data[0];
             	   			    $p = $p+1;
             	   		          }
             	   		   }  
             	   		     $cpt = $cpt +1;   	
		    	}
    		  }	
		if($i == 1)
    		  { $tab = "critere02";
    		    $cpt = 0;
    		    while($data=mysql_fetch_row($result))  
             		{	if($cpt == 0)
             			  { $tempo[] = $data[0];
             			    $critere02[$p][0] = $data[0];
  				    $critere02[$p][1] = $data[0];
             	   		    $p = $p+1;
             			  } 
             			if($cpt > 0)  
             			  {    $var = $data[0];
             			       if(in_array($var, $tempo))
             			         { $test = false;
				         }
             	   		    else {  $tempo[] = $var;  
             			            $critere02[$p][0] = $data[0];
  				            $critere02[$p][1] = $data[0];
             	   		            $p = $p+1;
			    	         }
			    	  }  
             	   		     $cpt = $cpt +1;   	
                      }
    		  }	
    		if($i == 2)
    		  { $tab = "critere03";
    		    $cpt = 0;
    		    while($data=mysql_fetch_row($result))  
             		{	if($cpt == 0)
             			  { $tempo[] = $data[0];
             			    $critere03[$p][0] = $data[0];
  				    $critere03[$p][1] = $data[0];
  				    $p = $p+1;
             			  } 
             			if($cpt > 0)
             			 {     $var = $data[0];
             			       if(in_array($var, $tempo))
             			         { $test = false;
				         }
				        else {  $tempo[] = $var;  
             	   				$critere03[$p][0] = $data[0];
  						$critere03[$p][1] = $data[0];
             	   				$p = $p+1;
             	   			     }
             	   		 }	      	
             	   		    $cpt = $cpt +1;   	
		    	}
    		  }	
    		if($i == 3)
    		  { $tab = "critere04";
    		    $cpt = 0;
		    while($data=mysql_fetch_row($result))  
             		{       if($cpt == 0)
             			  { $tempo[] = $data[0];
             			    $critere04[$p][0] = $data[0];
 	                            $critere04[$p][1] = $data[0];
             	   		    $p = $p+1;
             			  } 
             			if($cpt > 0)
             			 {     $var = $data[0];
             			       if(in_array($var, $tempo))
             			         { $test = false;
				         }
				       else {  $tempo[] = $var;  
             	   			       $critere04[$p][0] = $data[0];
  					       $critere04[$p][1] = $data[0];
             	   		               $p = $p+1;
             	   		            }
             	   		  }	      	
             	   		     $cpt = $cpt +1;        
		    	}	
    		  }	
    		if($i == 4)
    		  { $tab = "critere05";
    		    $cpt = 0;
		    while($data=mysql_fetch_row($result))  
             		{	if($cpt == 0)
             			  { $tempo[] = $data[0];
             			    $critere05[$p][0] = $data[0];
  				    $critere05[$p][1] = $data[0];
             	   		    $p = $p+1;
             			  } 
             			if($cpt > 0)
             			 {     $var = $data[0];
             			       if(in_array($var, $tempo))
             			         { $test = false;
				         }
				        else {  $tempo[] = $var;  
             	   				$critere05[$p][0] = $data[0];
  						$critere05[$p][1] = $data[0];
             	   				$p = $p+1;
		    		             }
				 }	      	
             	   		      $cpt = $cpt +1;		    		             	
		        }
    		  }	      
		if($i == 5)
    		  { $tab = "critere06";
    		    $cpt = 0;
		    while($data=mysql_fetch_row($result))  
             		{	if($cpt == 0)
             			  { $tempo[] = $data[0];
             			    $critere06[$p][0] = $data[0];
   		                    $critere06[$p][1] = $data[0];
             	   		    $p = $p+1;
             			  } 
             			if($cpt > 0)
             			 {     $var = $data[0];
             			       if(in_array($var, $tempo))
             			         { $test = false;
				         }
				        else {  $tempo[] = $var;  
             	   				$critere06[$p][0] = $data[0];
  						$critere06[$p][1] = $data[0];
             	   				$p = $p+1;
             	   			     }
		    	           }
             	   		      $cpt = $cpt +1;
    		        }	
	          }
	      
	     }     


	

	$debug = false;

	if($debug == true)
          {
	     echo "****************** Affichage pour le debug **************************"; 
    			echo "<br>"; 
			echo "<pre>"; 
			print_r($critere01);
			echo "</pre>";
             		echo "<br>"; 
             		
             		
             		echo "<br>"; 
			echo "<pre>"; 
			print_r($aacontenu2);
			echo "</pre>";
             		echo "<br>"; 
             		
    			
    			echo "<br>"; 
			echo "<pre>"; 
			print_r($critere02);
			echo "</pre>";
             		echo "<br>"; 
             		
             		echo "<br>"; 
			echo "<pre>"; 
			print_r($critere03);
			echo "</pre>";
             		echo "<br>"; 

			echo "<br>"; 
			echo "<pre>"; 
			print_r($critere04);
			echo "</pre>";
             		echo "<br>"; 

			echo "<br>"; 
			echo "<pre>"; 
			print_r($critere05);
			echo "</pre>";
             		echo "<br>"; 
             		
             		echo "<br>"; 
			echo "<pre>"; 
			print_r($critere06);
			echo "</pre>";
             		echo "<br>"; 

	  }

//======================================================
// VERFICATION DU POST
//======================================================

if($_POST["submit"])
  {
	
	//header('Location: aargrggrafraichir_filtre.php');
  	
        echo '<b>Formulaire validé avec les éléments : </b><br>';
	
        $portion_sql = array();
	
	if(count($critere01) != 0)
	  {
	    echo '<b>- '.$item01.' :</b> ';
	    $chaine01 ="";	
	
	    if(count($_POST[$item01]) != 0)	
	      {
    		  for($i = 0; $i < sizeof($_POST[$item01]); $i++)
	             {
			if($i > 0) echo ', ';
			echo $_POST[$item01][$i];

			$champ = $_POST[$item01][$i];

			if($i<count($_POST[$item01])-1)
		  	  {$chaine01 = $chaine01 ."'$champ'" .", ";
		  	  }
			if($i == count($_POST[$item01])-1)
		     	  {$chaine01 = $chaine01 ."'$champ'";
			  }  
	            }
		  echo "++++++++ " .$chaine01;
		
		  $final01 = $champs_filtre[0] ." IN (" .$chaine01 .")";
	    	  $portion_sql[] = $champs_filtre[0] ." IN (" .$chaine01 .")";
	    }	

	 }      
         
	echo '<br /><br />';
          
        if(count($critere02) != 0)
	  {
	    echo '<b>- '.$item02.' :</b> ';
	    $chaine02 ="";
	
	    if(count($_POST[$item02]) != 0)	
	      {	
	    	 for($i = 0; $i < sizeof($_POST[$item02]); $i++)
	            {
			if($i > 0) echo ', ';
			echo $_POST[$item02][$i];
			
			$champ = $_POST[$item02][$i];

			if($i<count($_POST[$item02])-1)
		  	  {$chaine02 = $chaine02 ."'$champ'" .", ";
		  	  }
			if($i == count($_POST[$item02])-1)
		     	  {$chaine02 = $chaine02 ."'$champ'";
			  }  
	            }
	         echo "++++++++ " .$chaine02;
	       
	         $final02 = $champs_filtre[1] ." IN (" .$chaine02 .")";
	         $portion_sql[] = $champs_filtre[1] ." IN (" .$chaine02 .")";
             }

          }  
	
	echo '<br /><br />';

	if(count($critere03) != 0)
	  {
	    echo '<b>- '.$item03.' :</b> ';
	    $chaine03 ="";	
	
	    if(count($_POST[$item03]) != 0)	
	      {
		 for($i = 0; $i < sizeof($_POST[$item03]); $i++)
	            {
			if($i > 0) echo ', ';
			echo $_POST[$item03][$i];
			
			$champ = $_POST[$item03][$i];

			if($i<count($_POST[$item03])-1)
		  	  {$chaine03 = $chaine03 ."'$champ'" .", ";
		  	  }
			if($i == count($_POST[$item03])-1)
		     	  {$chaine03 = $chaine03 ."'$champ'";
			  }  
	            }
		 echo "++++++++ " .$chaine03;
		
	         $final03 = $champs_filtre[2] ." IN (" .$chaine03 .")";
	         $portion_sql[] = $champs_filtre[2] ." IN (" .$chaine03 .")";
	     }	

          }  
	echo '<br /><br />';
          
        if(count($critere04) != 0)
	  {
	    echo '<b>- '.$item04.' :</b> ';
	    $chaine04 ="";
	
	    if(count($_POST[$item04]) != 0)	
	      {
	         for($i = 0; $i < sizeof($_POST[$item04]); $i++)
	            {
			if($i > 0) echo ', ';
			echo $_POST[$item04][$i];

			$champ = $_POST[$item04][$i];

			if($i<count($_POST[$item04])-1)
		  	  {$chaine04 = $chaine04 ."'$champ'" .", ";
		  	  }
			if($i == count($_POST[$item04])-1)
		     	  {$chaine04 = $chaine04 ."'$champ'";
			  }  
	           }
		 echo "++++++++ " .$chaine04;
		
	         $final04 = $champs_filtre[3] ." IN (" .$chaine04 .")";
	         $portion_sql[] = $champs_filtre[3] ." IN (" .$chaine04 .")";
             }

          }    
          
	echo '<br /><br />';

	if(count($critere05) != 0)
	  {
	    echo '<b>- '.$item05.' :</b> ';
	    $chaine05 ="";
	
	    if(count($_POST[$item05]) != 0)	
	      {
	         for($i = 0; $i < sizeof($_POST[$item05]); $i++)
	            {
			if($i > 0) echo ', ';
			echo $_POST[$item05][$i];
		
			$champ = $_POST[$item05][$i];

			if($i<count($_POST[$item05])-1)
		  	  {$chaine05 = $chaine05 ."'$champ'" .", ";
		  	  }
			if($i == count($_POST[$item05])-1)
		     	  {$chaine05 = $chaine05 ."'$champ'";
			  }  
	            }
	          echo "++++++++ " .$chaine05;
	       
       	          $final05 = $champs_filtre[4] ." IN (" .$chaine05 .")";
	          $portion_sql[] = $champs_filtre[4] ." IN (" .$chaine05 .")";
	     }	

          }  
          
	echo '<br /><br />';
         
        if(count($critere06) != 0)
	  {
	    echo '<b>- '.$item06.' :</b> ';
	    $chaine06 ="";
	
	    if(count($_POST[$item06]) != 0)	
	      {	
	
	         for($i = 0; $i < sizeof($_POST[$item06]); $i++)
	            {
			if($i > 0) echo ', ';
			echo $_POST[$item06][$i];
		
			$champ = $_POST[$item06][$i];

			if($i<count($_POST[$item06])-1)
		  	  {$chaine06 = $chaine06 ."'$champ'" .", ";
		  	  }
			if($i == count($_POST[$item06])-1)
		     	  {$chaine06 = $chaine06 ."'$champ'";
			  }  
	            }
	         echo "++++++++ " .$chaine06;
	       
	         $final06 = $champs_filtre[5] ." IN (" .$chaine06 .")";
	         $portion_sql[] = $champs_filtre[5] ." IN (" .$chaine06 .")";
              }

          }    
          
	echo '<br/><br/>';
	
	echo "<br>"; 
	echo "<pre>"; 
	var_dump($portion_sql);
	echo "</pre>";
        echo "<br>"; 
        
        $nb_options = count($portion_sql);
        echo "Options: " .$nb_options;
        echo "<br>"; 
        
        $_SESSION['nb_options'] = $nb_options ;
        	
         // construire le requete
	    
	    $portion ="";
	    $chaine = "";
	    
	    for($k=0; $k<=count($portion_sql)-1; $k++) 
  	       { 
  	       	 	$champ = $portion_sql[$k];
  	       		
  	       		if(champ !="")
  	       		  {
  	       	  		if($k<count($portion_sql)-1)
		    		  {$chaine = $chaine .$portion_sql[$k] ." and ";
	            		  }
	          		if($k == count($portion_sql)-1)
		    		  {$chaine = $chaine .$portion_sql[$k];
	            		  }  
	       		  }
	       }		  
	       
	       $flag = "filtre";
	       
	       
	       if($chaine == "")
	         {  $req = " SELECT * FROM $table ";
	         }	
	       else {
	             $req = " SELECT * FROM $table WHERE " .$chaine;
		     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_resultats_filtre = mysql_num_rows($result);
    	
	    	if($nb_resultats_filtre != 0)
	          { 	    	
    	    	   while($datas = mysql_fetch_assoc($result))
                        {
                          echo $datas['reference'];
                          echo "<br>"; 
                        }
                  }
          	else { echo "Aucune réponse";} 
	    
	    	echo "<br>"; 
	    	echo "<br>"; 
	
}

?>	