<?php

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

if($_POST["submit"])
  {
  	echo '<body onLoad="alert(\' 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 ;
        
        if($nb_options != 0 )
          { $_SESSION['mode'] = "filtre";
          }
        else { $_SESSION['mode'] = "defaut"; 
             }  

	$_SESSION['page'] == 1;             
          	
        	
         // 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];
	            		  }  
	       		  }
	       }		  
	       
	       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>"; 
	
}

?>	