Tableau croisé dynamique

Eléphanteau du PHP | 14 Messages

29 août 2012, 13:52

Salut,

Je ne cherche pas qu'à afficher des colonnes ou pas, en fait c une solution brouillon de dépannage.

En fait, j'ai pas bien compris les idées proposées, alors j'ai fait cette altervative pour faire taire les gueuls des chefs.

Je voudrais sérieusement construire un tableau dynamique, mais dans vos réponses, j'ai déduit que mon besoin n'est pas claire alors j'ai bidouyé ce code.

Dans quel jointure y a erreur :?:
Pour le code pas claire je fais un post avec les balises php y a pas de soucis :!:
<?php
	try	{
		$bdd = new PDO('mysql:host=localhost;dbname=dbvente', 'root', 'mysql');
	}
	catch(Exception $e){
		die('Erreur : '.$e->getMessage());
	}
	
	$idf = isset($_POST['famille'])      ? $_POST['famille']      :null; //identifiant famille
	$idp = isset($_POST['produits'])     ? $_POST['produits']     :null; //identifiant produit
	$ids = isset($_POST['sousproduits']) ? $_POST['sousproduits'] :null; //identifiant sous produit	
	$idd = isset($_POST['spdetail'])     ? $_POST['spdetail']     :null; //identifiant sous produit	détail
	
	$idb = isset($_POST['du'])           ? $_POST['du']           :null; //identifiant date début (begin)
	$ide = isset($_POST['au'])           ? $_POST['au']           :null; //identifiant date fin (end)
	
	$vl = (isset($_POST['vl']))          ?$_POST['vl']            :null; //case à cocher pour la colonne des volumes
	$pm = (isset($_POST['pm']))          ?$_POST['pm']            :null; //case à cocher pour la colonne des prix moyens
	$cd = (isset($_POST['cd']))          ?$_POST['cd']            :null; //case à cocher pour la colonne des chiffres des affaires en devise
	$cl = (isset($_POST['cl']))          ?$_POST['cl']            :null; //case à cocher pour la colonne des chiffres des affaires en monnaie locale
?>

<form id="chgdetail" action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post">
  <fieldset id="t0">
 
  <fieldset id="t2">
    <legend> Famille de produit </legend><br/>
	
    <label for="famille">Famille : </label> 
    <select name="famille" id="famille" tabindex="1" onchange="document.forms['chgdetail'].submit();"> <br/>
		<option value="-1">Toutes les familles</option>
		<?php	
			
			/*-----------------------------Famille de produit--------------------------------*/
		
			$reponse = $bdd->query('SELECT famille_produit.ID_Famille, famille_produit.Nom_Famille FROM famille_produit');
			
			$code_famille=array();
			$famille=array();
			
			$nb_famille = 0;
			
			if($reponse != false){
			
				while($ligne = $reponse->fetch(PDO::FETCH_ASSOC)){
					array_push($code_famille, $ligne['ID_Famille']);
					array_push($famille, $ligne['Nom_Famille']);
					$nb_famille++;
				}
			}
			
			for($i=0; $i<$nb_famille; $i++){
			?>
			
				<option value="<?php echo($code_famille[$i]); ?>"<?php echo((isset($idf) && $idf == $code_famille[$i])?" selected=\"selected\"":null); ?>><?php echo($famille[$i]); ?></option>
				
			<?php
			
			//$reponse->closeCursor();	
		}
		?>
	</select>    
    <p tabindex="2"/>
  </fieldset>
  
  <fieldset id="t3">
    <legend> Produits et Détails </legend><br/>
	
    <label for="produits">Produits : </label>
    <select name="produits" id="produits" tabindex="20" onchange="document.forms['chgdetail'].submit();"><br/>
		<option value="-1">Tous les produits</option>
		<?php
			
			/*-----------------------------------------Les produits -----------------------------------------*/
			
			$reponse = $bdd->query('SELECT produit.ID_Produit, produit.Nom_Produit
								FROM
								  produit
								WHERE
								  produit.ID_Famille = '.$idf.'');			
			
			$code_produit=array();
			$produit=array();
			
			$nb_produits = 0;
		
			if($reponse != false){
			
				while($ligne = $reponse->fetch(PDO::FETCH_ASSOC)){
					array_push($code_produit, $ligne['ID_Produit']);
					array_push($produit, $ligne['Nom_Produit']);
					$nb_produits++;
				}
			}			
			
			for($i=0; $i<$nb_produits; $i++){
			?>
			
				<option value="<?php echo($code_produit[$i]); ?>"<?php echo((isset($idp) && $idp == $code_produit[$i])?" selected=\"selected\"":null); ?>><?php echo($produit[$i]); ?></option>
				
			<?php
			
			$reponse->closeCursor();	
			}
		?>
	</select></br>
	
	<br/><label for="sousproduits">Sous produits : </label> 
		<select name="sousproduits" id="sousproduits" tabindex="21" onchange="document.forms['chgdetail'].submit();"><br/>
			<option value="-1">Tous les sous-produits</option>
			<?php
					
				/*-------------------------------------------------- Les sous produits --------------------------------*/
				
				if(isset($idp) && $idp!=-1){
					$reponse = $bdd->query('SELECT ID_Sous_Produit, Nom_Sous_Produit FROM sous_produit WHERE ID_Produit = ' . $idp .' '. 'ORDER BY ID_Sous_Produit');
						
					$nsp = 0;
							
					$code_sousproduit = array();
					$nom_sousproduit = array();
							
					while($ligne_sousproduit = $reponse->fetch(PDO::FETCH_ASSOC)){
						array_push($code_sousproduit, $ligne_sousproduit['ID_Sous_Produit']);
						array_push($nom_sousproduit, $ligne_sousproduit['Nom_Sous_Produit']);
						$nsp++;
					}				
					
					for($d=0; $d<$nsp; $d++){
					?>
				
						<option value="<?php echo($code_sousproduit[$d]); ?>"<?php echo((isset($ids) && $ids == $code_sousproduit[$d])?" selected=\"selected\"":null); ?>><?php echo($nom_sousproduit[$d]); ?></option>
					
					<?php
				
					$reponse->closeCursor();	
					}
				}	
			
			?>
		</select><br/>			
    
	<br/><label for="spdetail">Détail Sous produits : </label> 
		<select name="spdetail" id="spdetail" tabindex="22" onchange="document.forms['chgdetail'].submit();"><br/>
			<option value="-1">Détail des sous-produits</option>
			
			<?php
			
				/*----------------------------------------- Détail des sous produits ---------------------------------------*/
			
				if(isset($ids) && $ids!=-1){
					$reponse = $bdd->query('SELECT sous_produit_detail.ID_Sous_Produit_Detail
								   , sous_produit_detail.Nom_Sous_Produit_Detail
								FROM
								  sous_produit_detail
								WHERE
								  sous_produit_detail.ID_Sous_Produit = '. $ids .' '.'
								ORDER BY
								  sous_produit_detail.ID_Sous_Produit_Detail');
					
					$nspd = 0;
						
					$code_sousproduitdetail = array();
					$nom_sousproduitdetail = array();
						
					while($ligne_sousproduitdetail = $reponse->fetch(PDO::FETCH_ASSOC)){
						array_push($code_sousproduitdetail, $ligne_sousproduitdetail['ID_Sous_Produit_Detail']);
						array_push($nom_sousproduitdetail, $ligne_sousproduitdetail['Nom_Sous_Produit_Detail']);
						$nspd++;
					}
					
					if($nspd !=0) {		
							
						for($j=0; $j<$nspd; $j++){
					?>
							<option value="<?php echo($code_sousproduitdetail[$j]); ?>"<?php echo((isset($idd) && $idd == $code_sousproduitdetail[$j])?" selected=\"selected\"":null); ?>><?php echo($nom_sousproduitdetail[$j]); ?></option>
								
					<?php
						}
						$reponse->closeCursor();				
					}
			}
			?>
			
		</select>
	<p tabindex="23"/>	
  </fieldset>
  
  <!--------------------------------------- Critères de selection des périodes ----------------------------------------------->
  
  <fieldset id="t4">
    <legend>Période</legend><br/>
	
	<label for="du">du : </label>
    <select name="du" id="du" tabindex="35">
		<option value="-1">----</option>
		<?php

			$reponse = $bdd->query('SELECT DISTINCT periode.ID_Periode
								   , periode.An
								FROM
								  periode
								GROUP BY
								  periode.An
								ORDER BY
								  periode.An');
			
			$npd = 0;
						
			$code_periodedebut = array();
			$nom_periodedebut = array();
			
			while($ligne_periodedebut = $reponse->fetch(PDO::FETCH_ASSOC)){
				array_push($code_periodedebut, $ligne_periodedebut['ID_Periode']);
				array_push($nom_periodedebut, $ligne_periodedebut['An']);
				$npd++;
			}
					
					if($npd !=0) {		
							
						for($j=0; $j<$npd; $j++){
					?>
							<option value="<?php echo($code_periodedebut[$j]); ?>"<?php echo((isset($idb) && $idb == $code_periodedebut[$j])?" selected=\"selected\"":null); ?>><?php echo($nom_periodedebut[$j]); ?></option>
								
					<?php
						}
						$reponse->closeCursor();				
					}
		?>
    </select>
	
	<label for="au">au : </label>
	<select name="au" id="au" tabindex="36"><br/> 
		<option value="-1">----</option>
		<?php

			$reponse = $bdd->query('SELECT DISTINCT periode.ID_Periode
								   , periode.An
								FROM
								  periode
								GROUP BY
								  periode.An
								ORDER BY
								  periode.An');
			
			$npf = 0;
						
			$code_periodefin = array();
			$nom_periodefin = array();
			
			while($ligne_periodefin = $reponse->fetch(PDO::FETCH_ASSOC)){
				array_push($code_periodefin, $ligne_periodefin['ID_Periode']);
				array_push($nom_periodefin, $ligne_periodefin['An']);
				$npf++;
			}
					
					if($npf !=0) {		
							
						for($j=0; $j<$npf; $j++){
					?>
							<option value="<?php echo($code_periodefin[$j]); ?>"<?php echo((isset($ide) && $ide == $code_periodefin[$j])?" selected=\"selected\"":null); ?>><?php echo($nom_periodedebut[$j]); ?></option>
								
					<?php
						}
						$reponse->closeCursor();				
					}
		?>
    </select>
	<p tabindex="23"/>
  </fieldset>
  
  <!--------------------------------------- Critères de selection des unités de mesures -------------------------------------->
  
  <fieldset id="t5">
    <legend>Unités</legend><br/>
    
	<label for="unite">unités: </label>
	<select name="unite" id="unite" tabindex="45"><br/>
		<option value="-1">----</option>
		<?php

			$reponse = $bdd->query('SELECT DISTINCT unites.Nom_Unite FROM unites');

			while ($donnees = $reponse->fetch())
			{
				echo '<option value="choix">'.$donnees['Nom_Unite'].'</option>';
			}

			$reponse->closeCursor();
		?>
	</select>
	<p tabindex="23"/>
  </fieldset>
  
  <!--------------------------------------- Choix des colonnes à afficher ---------------------------------------------------->
  
  <p></p>
  <fieldset id="t6">
		<legend>Choix des informations à afficher</legend><br/>	
		
		<input type="checkbox" name="vl" value="on" checked="checked" /> Volume<br/><br/>
		<input type="checkbox" name="pm" value="on" checked="checked" /> Prix Moyen<br/><br/>
		<input type="checkbox" name="cd" value="on" checked="checked" /> Chiffre d'affaire en US$<br/><br/>
		<input type="checkbox" name="cl" value="on" checked="checked" /> Chiffre d'affaire en DA<br/><br/>

  </fieldset>
  
  <!--------------------------------------- Mise en forme finale du formulaire ----------------------------------------------->
  
  <p>
  <input id="recherche" name="recherche" type="submit" value="Rechercher" /> 
  <input id="reinitialiser" name="reinitialiser" type="reset" />
  </p>
  </fieldset>
  
  <fieldset id="t6">
    <legend>Résultat</legend><br/>
    <p>Affichage tabulaire du résultat de la recherche</p>
  </fieldset>
</form>


Eléphanteau du PHP | 14 Messages

29 août 2012, 13:53

suite:

<!----------------------------------------- Recherche des données --------------------------------------------->
<?php

$annee = 1900;

//Vérification de la période choisie
if($idb>0 && $ide>0)
{
	//Vérifier que les périodes sont cohérentes
	if($ide < $idb)
	{
		/*Afficher un message d'erreur
		Maintenir le focus sur le fieldset période*/
	}
	//SINON
	else
	{
		//La période choisite est comprise entre date début et une date fin
		if($idd>0)
		{
			$reponse = $bdd->query('SELECT
								  dbvente.periode.An,
								  dbvente.sous_produit_detail.Nom_Sous_Produit_Detail,
								  dbvente.unites.Nom_Unite,
								  dbvente.vente_sous_detail.Volume,
								  dbvente.vente_sous_detail.Prix,
								  dbvente.vente_sous_detail.CA,
								  dbvente.vente_sous_detail.CA_Dinars
								FROM
								  dbvente.vente_sous_detail,
								  dbvente.sous_produit_detail,
								  dbvente.periode,
								  dbvente.produit,
								  dbvente.sous_produit,
								  dbvente.unites,
								  dbvente.famille_produit
								WHERE
								  dbvente.famille_produit.ID_Famille = ' . $idf . ' AND
								  dbvente.produit.ID_Produit = ' . $idp . ' AND
								  dbvente.sous_produit.ID_Sous_Produit = ' . $ids . ' AND
								  dbvente.vente_sous_detail.ID_Marche = 2 AND
								  dbvente.vente_sous_detail.ID_Periode BETWEEN ' .$idb . ' AND ' . $ide . ' AND
								  dbvente.sous_produit_detail.ID_Sous_Produit_Detail = ' . $idd . ' AND
								  dbvente.vente_sous_detail.ID_Periode = dbvente.periode.ID_Periode AND
								  dbvente.vente_sous_detail.ID_Sous_Produit_Detail = dbvente.sous_produit_detail.ID_Sous_Produit_Detail AND
								  dbvente.sous_produit_detail.ID_Sous_Produit = dbvente.sous_produit.ID_Sous_Produit AND
								  dbvente.sous_produit.ID_Produit = dbvente.produit.ID_Produit AND
								  dbvente.vente_sous_detail.ID_Unite = dbvente.unites.ID_Unite AND
								  dbvente.famille_produit.ID_Famille = dbvente.produit.ID_Famille
								ORDER BY
								  dbvente.periode.An DESC');
								  
			?>
			<table border="2" width="100%" cellpadding="3">
				<?php
					//if(!empty($vl) || !empty($pm) || !empty($cd) || !empty($cl)){		
					while ($donnees = $reponse->fetch())
					{
						if($annee != $donnees['An']){
							echo '<tr>';
								echo '<th colspan="6">' . $donnees['An']. '</th>';
								echo '<tr>';
									echo '<th> Sous-Produits </th>';
									echo '<th> Unité </th>';
									//if(!empty($vl))
									{echo '<th>Volume</th>';}
									//if(!empty($pm))
									{echo '<th>Prix Moyen</th>';} 
									if(!empty($cd))
									//{echo '<th>Chiffre affaire devise</th>';} 
									//if(!empty($cl))
									{echo '<th>Chiffre affaire dinars</th>';} 
								echo '</tr>';	
							echo '</tr>';
						}
						
						echo '<tr><td>' . htmlspecialchars($donnees['Nom_Sous_Produit_Detail']) . '</td>';
							echo '<td>' . $donnees['Nom_Unite']. '</td>';	
							echo '<td>' . $donnees['Volume']. '</td>';
							if ($donnees['Prix']!=0){
								echo '<td>' . $donnees['Prix']. '</td>';}else{echo '<td>ND</td>';}
							if ($donnees['CA']!=0){
								echo '<td>' . $donnees['CA']. '</td>';}else{echo '<td>ND</td>';}
							if ($donnees['CA_Dinars']!=0){
								echo '<td>' . $donnees['CA_Dinars']. '</td>';}else{echo '<td>ND</td>';}
							$annee = $donnees['An'];
					}//}
					
				?>
			</table>					
			<?php
		}
		//SINON
		else
		{
			
			if($ids>0)
			{
				/*afficher toutes les ventes réalisées du sous produit
				je manipule à ce niveau la table vente de sous produit
				et la table détail*/
				
				$reponse = $bdd->query('SELECT
									  dbvente.periode.An,
									  dbvente.sous_produit_detail.Nom_Sous_Produit_Detail,
									  dbvente.unites.Nom_Unite,
									  dbvente.vente_sous_detail.Volume,										  
									  dbvente.vente_sous_detail.Prix,
									  dbvente.vente_sous_detail.CA,
									  dbvente.vente_sous_detail.CA_Dinars
									FROM
									  dbvente.vente_sous_detail,
									  dbvente.sous_produit_detail,
									  dbvente.periode,
									  dbvente.produit,
									  dbvente.sous_produit,
									  dbvente.unites,
									  dbvente.famille_produit
									WHERE
									  dbvente.famille_produit.ID_Famille = ' . $idf . ' AND
									  dbvente.produit.ID_Produit = ' . $idp . ' AND
									  dbvente.sous_produit.ID_Sous_Produit = ' . $ids . ' AND
									  dbvente.vente_sous_detail.ID_Periode BETWEEN ' . $idb . ' AND ' . $ide . ' AND										  
									  dbvente.vente_sous_detail.ID_Marche = 2 AND
									  dbvente.vente_sous_detail.ID_Periode = dbvente.periode.ID_Periode AND
									  dbvente.vente_sous_detail.ID_Sous_Produit_Detail = dbvente.sous_produit_detail.ID_Sous_Produit_Detail AND
									  dbvente.sous_produit_detail.ID_Sous_Produit = dbvente.sous_produit.ID_Sous_Produit AND
									  dbvente.sous_produit.ID_Produit = dbvente.produit.ID_Produit AND
									  dbvente.vente_sous_detail.ID_Unite = dbvente.unites.ID_Unite AND
									  dbvente.famille_produit.ID_Famille = dbvente.produit.ID_Famille
									ORDER BY
									  dbvente.periode.An DESC');
									  
				?>
				<table border="2" width="100%" cellpadding="3">
					<?php
								
						while ($donnees = $reponse->fetch())
						{
							if($annee != $donnees['An']){
								echo '<tr>';
									echo '<th colspan="6">' . $donnees['An']. '</th>';
									echo '<tr>';
										echo '<th> Détail Sous-Produits </th>';
										echo '<th> Unité </th>';
										echo '<th>Volume</th>';
										echo '<th>Prix Moyen</th>';
										echo '<th>Chiffre affaire devise</th>';
										echo '<th>Chiffre affaire dinars</th>';	
									echo '</tr>';	
								echo '</tr>';
							}
							
							echo '<tr><td>' . htmlspecialchars($donnees['Nom_Sous_Produit_Detail']) . '</td>';
								echo '<td>' . $donnees['Nom_Unite']. '</td>';	
								echo '<td>' . $donnees['Volume']. '</td>';
								if ($donnees['Prix']!=0){
									echo '<td>' . $donnees['Prix']. '</td>';}else{echo '<td>ND</td>';}
								if ($donnees['CA']!=0){
									echo '<td>' . $donnees['CA']. '</td>';}else{echo '<td>ND</td>';}
								if ($donnees['CA_Dinars']!=0){
									echo '<td>' . $donnees['CA_Dinars']. '</td>';}else{echo '<td>ND</td>';}
								$annee = $donnees['An'];
						}
						
					?>
				</table>				
				<?php
			}
			//SINON
			else
			{
				
				if($idp>0)
				{
					/*afficher toutes les ventes réalisées du produit
					je manipile à ce niveau la table vente globale
					et la table de vente de sous produit*/
					
					$reponse = $bdd->query('SELECT
										  dbvente.periode.An,
										  dbvente.sous_produit.Nom_Sous_Produit,
										  dbvente.unites.Nom_Unite,
										  dbvente.vente_detail.Volume,
										  dbvente.vente_detail.Prix,
										  dbvente.vente_detail.CA,
										  dbvente.vente_detail.CA_Dinars
										FROM
										  dbvente.periode,
										  dbvente.sous_produit,
										  dbvente.vente_detail,
										  dbvente.produit,
										  dbvente.famille_produit,
										  dbvente.unites
										WHERE
										  dbvente.vente_detail.ID_Periode = dbvente.periode.ID_Periode AND
										  dbvente.vente_detail.ID_Sous_Produit = dbvente.sous_produit.ID_Sous_Produit AND
										  dbvente.sous_produit.ID_Produit = dbvente.produit.ID_Produit AND
										  dbvente.produit.ID_Famille = dbvente.famille_produit.ID_Famille AND
										  dbvente.vente_detail.ID_Unite = dbvente.unites.ID_Unite AND
										  dbvente.vente_detail.ID_Marche = 2 AND
										  dbvente.famille_produit.ID_Famille = ' . $idf . ' AND
										  dbvente.sous_produit.ID_Produit = ' . $idp . ' AND
										  dbvente.vente_detail.ID_Periode BETWEEN ' . $idb . ' AND ' . $ide . ' '.'
										ORDER BY
										  dbvente.periode.An DESC');
										  
					?>
					<table border="2" width="100%" cellpadding="3">
						<?php
									
							while ($donnees = $reponse->fetch())
							{
								if($annee != $donnees['An']){
									echo '<tr>';
										echo '<th colspan="6">' . $donnees['An']. '</th>';
										echo '<tr>';
											echo '<th> Sous-Produits </th>';
											echo '<th> Unité </th>';
											echo '<th>Volume</th>';
											echo '<th>Prix Moyen</th>';
											echo '<th>Chiffre affaire devise</th>';
											echo '<th>Chiffre affaire dinars</th>';	
										echo '</tr>';	
									echo '</tr>';
								}
								
								echo '<tr><td>' . htmlspecialchars($donnees['Nom_Sous_Produit']) . '</td>';
									echo '<td>' . $donnees['Nom_Unite']. '</td>';	
									echo '<td>' . $donnees['Volume']. '</td>';
									if ($donnees['Prix']!=0){
										echo '<td>' . $donnees['Prix']. '</td>';}else{echo '<td>ND</td>';}
									if ($donnees['CA']!=0){
										echo '<td>' . $donnees['CA']. '</td>';}else{echo '<td>ND</td>';}
									if ($donnees['CA_Dinars']!=0){
										echo '<td>' . $donnees['CA_Dinars']. '</td>';}else{echo '<td>ND</td>';}
									$annee = $donnees['An'];
							}
							
						?>
					</table>
				
					<?php
				}
				else
				{
					if($idf>0)
					{
						/*afficher toutes les ventes réalisées de la famille 
						je manipile à ce niveau la table vente globale*/
						
						$reponse = $bdd->query('SELECT
											  dbvente.periode.An,
											  dbvente.produit.Nom_Produit,
											  dbvente.unites.Nom_Unite,
											  dbvente.vente.Volume,
											  dbvente.vente.Prix,
											  dbvente.vente.CA,
											  dbvente.vente.CA_Dinars
											FROM
											  dbvente.periode,
											  dbvente.produit,
											  dbvente.vente,
											  dbvente.unites
											WHERE
											  dbvente.vente.ID_Periode = dbvente.periode.ID_Periode AND
											  dbvente.vente.ID_Produit = dbvente.produit.ID_Produit AND
											  dbvente.vente.ID_Unite = dbvente.unites.ID_Unite AND
											  dbvente.vente.ID_Marche = 2 AND
											  dbvente.vente.ID_Famille = ' . $idf . ' ' . 'AND
											  dbvente.vente.ID_Periode BETWEEN ' . $idb . ' AND '. $ide .' '.'
											ORDER BY
											  dbvente.periode.An DESC');
						?>
						
						<table border="2" width="100%" cellpadding="3">
							<?php
										
								while ($donnees = $reponse->fetch())
								{
									if($annee != $donnees['An']){
										echo '<tr>';
											echo '<th colspan="6">' . $donnees['An']. '</th>';
											echo '<tr>';
												echo '<th> Produit </th>';
												echo '<th> Unité </th>';
												echo '<th>Volume</th>';
												echo '<th>Prix Moyen</th>';
												echo '<th>Chiffre affaire devise</th>';
												echo '<th>Chiffre affaire dinars</th>';	
											echo '</tr>';	
										echo '</tr>';
									}
									
									echo '<tr><td>' . htmlspecialchars($donnees['Nom_Produit']) . '</td>';
										echo '<td>' . $donnees['Nom_Unite']. '</td>';	
										echo '<td>' . $donnees['Volume']. '</td>';
										if ($donnees['Prix']!=0){
											echo '<td>' . $donnees['Prix']. '</td>';}else{echo '<td>ND</td>';}
										if ($donnees['CA']!=0){
											echo '<td>' . $donnees['CA']. '</td>';}else{echo '<td>ND</td>';}
										if ($donnees['CA_Dinars']!=0){
											echo '<td>' . $donnees['CA_Dinars']. '</td>';}else{echo '<td>ND</td>';}
										$annee = $donnees['An'];
								}
								
							?>
						</table>
						
						<?php
					}
				}
			}
		}
	}
}
/*Sinon
 Vérifier les intervales choisis*/
else
{
	//La période choisie correspond à depuis une date début jusqu'à présent
	if($idb>0 && $ide<=0)
	{
		if($idd>0)
		{
			$reponse = $bdd->query('SELECT
								  dbvente.periode.An,
								  dbvente.sous_produit_detail.Nom_Sous_Produit_Detail,
								  dbvente.unites.Nom_Unite,
								  dbvente.vente_sous_detail.Volume,
								  dbvente.vente_sous_detail.Prix,
								  dbvente.vente_sous_detail.CA,
								  dbvente.vente_sous_detail.CA_Dinars
								FROM
								  dbvente.vente_sous_detail,
								  dbvente.sous_produit_detail,
								  dbvente.periode,
								  dbvente.produit,
								  dbvente.sous_produit,
								  dbvente.unites,
								  dbvente.famille_produit
								WHERE
								  dbvente.famille_produit.ID_Famille = ' . $idf . ' AND
								  dbvente.produit.ID_Produit = ' . $idp . ' AND
								  dbvente.sous_produit.ID_Sous_Produit = ' . $ids . ' AND
								  dbvente.vente_sous_detail.ID_Marche = 2 AND
								  dbvente.vente_sous_detail.ID_Periode >= ' .$idb . ' ' . '
								  dbvente.sous_produit_detail.ID_Sous_Produit_Detail = ' . $idd . ' AND
								  dbvente.vente_sous_detail.ID_Periode = dbvente.periode.ID_Periode AND
								  dbvente.vente_sous_detail.ID_Sous_Produit_Detail = dbvente.sous_produit_detail.ID_Sous_Produit_Detail AND
								  dbvente.sous_produit_detail.ID_Sous_Produit = dbvente.sous_produit.ID_Sous_Produit AND
								  dbvente.sous_produit.ID_Produit = dbvente.produit.ID_Produit AND
								  dbvente.vente_sous_detail.ID_Unite = dbvente.unites.ID_Unite AND
								  dbvente.famille_produit.ID_Famille = dbvente.produit.ID_Famille
								ORDER BY
								  dbvente.periode.An DESC');
								  
			?>
			<table border="2" width="100%" cellpadding="3">
				<?php
							
					while ($donnees = $reponse->fetch())
					{
						if($annee != $donnees['An']){
							echo '<tr>';
								echo '<th colspan="6">' . $donnees['An']. '</th>';
								echo '<tr>';
									echo '<th> Sous-Produits </th>';
									echo '<th> Unité </th>';
									echo '<th>Volume</th>';
									echo '<th>Prix Moyen</th>';
									echo '<th>Chiffre affaire devise</th>';
									echo '<th>Chiffre affaire dinars</th>';	
								echo '</tr>';	
							echo '</tr>';
						}
						
						echo '<tr><td>' . htmlspecialchars($donnees['Nom_Sous_Produit_Detail']) . '</td>';
							echo '<td>' . $donnees['Nom_Unite']. '</td>';	
							echo '<td>' . $donnees['Volume']. '</td>';
							if ($donnees['Prix']!=0){
								echo '<td>' . $donnees['Prix']. '</td>';}else{echo '<td>ND</td>';}
							if ($donnees['CA']!=0){
								echo '<td>' . $donnees['CA']. '</td>';}else{echo '<td>ND</td>';}
							if ($donnees['CA_Dinars']!=0){
								echo '<td>' . $donnees['CA_Dinars']. '</td>';}else{echo '<td>ND</td>';}
							$annee = $donnees['An'];
					}
					
				?>
			</table>					
			<?php
		}
		//SINON
		else
		{
			 //SI produit selectionné ALORS
			if($ids>0)
			{
				/*afficher toutes les ventes réalisées du sous produit
				je manipule à ce niveau la table vente de sous produit
				et la table détail*/
				
				$reponse = $bdd->query('SELECT
									  dbvente.periode.An,
									  dbvente.sous_produit_detail.Nom_Sous_Produit_Detail,
									  dbvente.unites.Nom_Unite,
									  dbvente.vente_sous_detail.Volume,										  
									  dbvente.vente_sous_detail.Prix,
									  dbvente.vente_sous_detail.CA,
									  dbvente.vente_sous_detail.CA_Dinars
									FROM
									  dbvente.vente_sous_detail,
									  dbvente.sous_produit_detail,
									  dbvente.periode,
									  dbvente.produit,
									  dbvente.sous_produit,
									  dbvente.unites,
									  dbvente.famille_produit
									WHERE
									  dbvente.famille_produit.ID_Famille = ' . $idf . ' AND
									  dbvente.produit.ID_Produit = ' . $idp . ' AND
									  dbvente.sous_produit.ID_Sous_Produit = ' . $ids . ' AND
									  dbvente.vente_sous_detail.ID_Periode >= ' . $idb . ' ' . '										  
									  dbvente.vente_sous_detail.ID_Marche = 2 AND
									  dbvente.vente_sous_detail.ID_Periode = dbvente.periode.ID_Periode AND
									  dbvente.vente_sous_detail.ID_Sous_Produit_Detail = dbvente.sous_produit_detail.ID_Sous_Produit_Detail AND
									  dbvente.sous_produit_detail.ID_Sous_Produit = dbvente.sous_produit.ID_Sous_Produit AND
									  dbvente.sous_produit.ID_Produit = dbvente.produit.ID_Produit AND
									  dbvente.vente_sous_detail.ID_Unite = dbvente.unites.ID_Unite AND
									  dbvente.famille_produit.ID_Famille = dbvente.produit.ID_Famille
									ORDER BY
									  dbvente.periode.An DESC');
				
				?>
				<table border="2" width="100%" cellpadding="3">
					<?php
								
						while ($donnees = $reponse->fetch())
						{
							if($annee != $donnees['An']){
								echo '<tr>';
									echo '<th colspan="6">' . $donnees['An']. '</th>';
									echo '<tr>';
										echo '<th> Sous-Produits </th>';
										echo '<th> Unité </th>';
										echo '<th>Volume</th>';
										echo '<th>Prix Moyen</th>';
										echo '<th>Chiffre affaire devise</th>';
										echo '<th>Chiffre affaire dinars</th>';	
									echo '</tr>';	
								echo '</tr>';
							}
							
							echo '<tr><td>' . htmlspecialchars($donnees['Nom_Sous_Produit_Detail']) . '</td>';
								echo '<td>' . $donnees['Nom_Unite']. '</td>';	
								echo '<td>' . $donnees['Volume']. '</td>';
								if ($donnees['Prix']!=0){
									echo '<td>' . $donnees['Prix']. '</td>';}else{echo '<td>ND</td>';}
								if ($donnees['CA']!=0){
									echo '<td>' . $donnees['CA']. '</td>';}else{echo '<td>ND</td>';}
								if ($donnees['CA_Dinars']!=0){
									echo '<td>' . $donnees['CA_Dinars']. '</td>';}else{echo '<td>ND</td>';}
								$annee = $donnees['An'];
						}
						
					?>
				</table>				
				<?php
			}
			//SINON
			else
			{
				//SI sous produit selectionné ALORS
				if($idp>0)
				{
					/*afficher toutes les ventes réalisées du produit
					je manipile à ce niveau la table vente globale
					et la table de vente de sous produit*/
					
					$reponse = $bdd->query('SELECT
										  dbvente.periode.An,
										  dbvente.sous_produit.Nom_Sous_Produit,
										  dbvente.unites.Nom_Unite,
										  dbvente.vente_detail.Volume,
										  dbvente.vente_detail.Prix,
										  dbvente.vente_detail.CA,
										  dbvente.vente_detail.CA_Dinars
										FROM
										  dbvente.periode,
										  dbvente.sous_produit,
										  dbvente.vente_detail,
										  dbvente.produit,
										  dbvente.famille_produit,
										  dbvente.unites
										WHERE
										  dbvente.vente_detail.ID_Periode = dbvente.periode.ID_Periode AND
										  dbvente.vente_detail.ID_Sous_Produit = dbvente.sous_produit.ID_Sous_Produit AND
										  dbvente.sous_produit.ID_Produit = dbvente.produit.ID_Produit AND
										  dbvente.produit.ID_Famille = dbvente.famille_produit.ID_Famille AND
										  dbvente.vente_detail.ID_Unite = dbvente.unites.ID_Unite AND
										  dbvente.vente_detail.ID_Marche = 2 AND
										  dbvente.famille_produit.ID_Famille = ' . $idf . ' AND
										  dbvente.sous_produit.ID_Produit = ' . $idp . ' AND
										  dbvente.vente_detail.ID_Periode >= ' . $idb . ' ' .'
										ORDER BY
										  dbvente.periode.An DESC');
										  
					
					?>
					<table border="2" width="100%" cellpadding="3">
						<?php
									
							while ($donnees = $reponse->fetch())
							{
								if($annee != $donnees['An']){
									echo '<tr>';
										echo '<th colspan="6">' . $donnees['An']. '</th>';
										echo '<tr>';
											echo '<th> Sous-Produits </th>';
											echo '<th> Unité </th>';
											echo '<th>Volume</th>';
											echo '<th>Prix Moyen</th>';
											echo '<th>Chiffre affaire devise</th>';
											echo '<th>Chiffre affaire dinars</th>';	
										echo '</tr>';	
									echo '</tr>';
								}
								
								echo '<tr><td>' . htmlspecialchars($donnees['Nom_Sous_Produit']) . '</td>';
									echo '<td>' . $donnees['Nom_Unite']. '</td>';	
									echo '<td>' . $donnees['Volume']. '</td>';
									if ($donnees['Prix']!=0){
										echo '<td>' . $donnees['Prix']. '</td>';}else{echo '<td>ND</td>';}
									if ($donnees['CA']!=0){
										echo '<td>' . $donnees['CA']. '</td>';}else{echo '<td>ND</td>';}
									if ($donnees['CA_Dinars']!=0){
										echo '<td>' . $donnees['CA_Dinars']. '</td>';}else{echo '<td>ND</td>';}
									$annee = $donnees['An'];
							}
							
						?>
					</table>				
					<?php
				}
				else
				{
					if($idf>0)
					{
						/*afficher toutes les ventes réalisées de la famille 
						je manipile à ce niveau la table vente globale*/
						
						$reponse = $bdd->query('SELECT
											  dbvente.periode.An,
											  dbvente.produit.Nom_Produit,
											  dbvente.unites.Nom_Unite,
											  dbvente.vente.Volume,
											  dbvente.vente.Prix,
											  dbvente.vente.CA,
											  dbvente.vente.CA_Dinars
											FROM
											  dbvente.periode,
											  dbvente.produit,
											  dbvente.vente,
											  dbvente.unites
											WHERE
											  dbvente.vente.ID_Periode = dbvente.periode.ID_Periode AND
											  dbvente.vente.ID_Produit = dbvente.produit.ID_Produit AND
											  dbvente.vente.ID_Unite = dbvente.unites.ID_Unite AND
											  dbvente.vente.ID_Marche = 2 AND
											  dbvente.vente.ID_Famille = ' . $idf . ' ' . 'AND
											  dbvente.vente.ID_Periode >= ' . $idb . ' '.'
											ORDER BY
											  dbvente.periode.An DESC');
											  
						?>
						
						<table border="2" width="100%" cellpadding="3">
							<?php
										
								while ($donnees = $reponse->fetch())
								{
									if($annee != $donnees['An']){
										echo '<tr>';
											echo '<th colspan="6">' . $donnees['An']. '</th>';
											echo '<tr>';
												echo '<th> Produit </th>';
												echo '<th> Unité </th>';
												echo '<th>Volume</th>';
												echo '<th>Prix Moyen</th>';
												echo '<th>Chiffre affaire devise</th>';
												echo '<th>Chiffre affaire dinars</th>';	
											echo '</tr>';	
										echo '</tr>';
									}
									
									echo '<tr><td>' . htmlspecialchars($donnees['Nom_Produit']) . '</td>';
										echo '<td>' . $donnees['Nom_Unite']. '</td>';	
										echo '<td>' . $donnees['Volume']. '</td>';
										if ($donnees['Prix']!=0){
											echo '<td>' . $donnees['Prix']. '</td>';}else{echo '<td>ND</td>';}
										if ($donnees['CA']!=0){
											echo '<td>' . $donnees['CA']. '</td>';}else{echo '<td>ND</td>';}
										if ($donnees['CA_Dinars']!=0){
											echo '<td>' . $donnees['CA_Dinars']. '</td>';}else{echo '<td>ND</td>';}
										$annee = $donnees['An'];
								}
								
							?>
						</table>
						
						<?php
					}
				}
			}
		}
	}
	else
	{
		if($idb<=0 && $ide>0)
		{
			//La période choisie correspond à toutes les dates jusqu'à une date précise
			if($idd>0)
			{
				$reponse = $bdd->query('SELECT
									  dbvente.periode.An,
									  dbvente.sous_produit_detail.Nom_Sous_Produit_Detail,
									  dbvente.unites.Nom_Unite,
									  dbvente.vente_sous_detail.Volume,
									  dbvente.vente_sous_detail.Prix,
									  dbvente.vente_sous_detail.CA,
									  dbvente.vente_sous_detail.CA_Dinars
									FROM
									  dbvente.vente_sous_detail,
									  dbvente.sous_produit_detail,
									  dbvente.periode,
									  dbvente.produit,
									  dbvente.sous_produit,
									  dbvente.unites,
									  dbvente.famille_produit
									WHERE
									  dbvente.famille_produit.ID_Famille = ' . $idf . ' AND
									  dbvente.produit.ID_Produit = ' . $idp . ' AND
									  dbvente.sous_produit.ID_Sous_Produit = ' . $ids . ' AND
									  dbvente.vente_sous_detail.ID_Marche = 2 AND
									  dbvente.vente_sous_detail.ID_Periode <= ' .$ide . ' ' . '
									  dbvente.sous_produit_detail.ID_Sous_Produit_Detail = ' . $idd . ' AND
									  dbvente.vente_sous_detail.ID_Periode = dbvente.periode.ID_Periode AND
									  dbvente.vente_sous_detail.ID_Sous_Produit_Detail = dbvente.sous_produit_detail.ID_Sous_Produit_Detail AND
									  dbvente.sous_produit_detail.ID_Sous_Produit = dbvente.sous_produit.ID_Sous_Produit AND
									  dbvente.sous_produit.ID_Produit = dbvente.produit.ID_Produit AND
									  dbvente.vente_sous_detail.ID_Unite = dbvente.unites.ID_Unite AND
									  dbvente.famille_produit.ID_Famille = dbvente.produit.ID_Famille
									ORDER BY
									  dbvente.periode.An DESC');
									  
				?>
				<table border="2" width="100%" cellpadding="3">
					<?php
								
						while ($donnees = $reponse->fetch())
						{
							if($annee != $donnees['An']){
								echo '<tr>';
									echo '<th colspan="6">' . $donnees['An']. '</th>';
									echo '<tr>';
										echo '<th> Sous-Produits </th>';
										echo '<th> Unité </th>';
										echo '<th>Volume</th>';
										echo '<th>Prix Moyen</th>';
										echo '<th>Chiffre affaire devise</th>';
										echo '<th>Chiffre affaire dinars</th>';	
									echo '</tr>';	
								echo '</tr>';
							}
							
							echo '<tr><td>' . htmlspecialchars($donnees['Nom_Sous_Produit_Detail']) . '</td>';
								echo '<td>' . $donnees['Nom_Unite']. '</td>';	
								echo '<td>' . $donnees['Volume']. '</td>';
								if ($donnees['Prix']!=0){
									echo '<td>' . $donnees['Prix']. '</td>';}else{echo '<td>ND</td>';}
								if ($donnees['CA']!=0){
									echo '<td>' . $donnees['CA']. '</td>';}else{echo '<td>ND</td>';}
								if ($donnees['CA_Dinars']!=0){
									echo '<td>' . $donnees['CA_Dinars']. '</td>';}else{echo '<td>ND</td>';}
								$annee = $donnees['An'];
						}
						
					?>
				</table>					
				<?php
			}
			//SINON
			else
			{
				 //SI produit selectionné ALORS
				if($ids>0)
				{
					/*afficher toutes les ventes réalisées du sous produit
					je manipule à ce niveau la table vente de sous produit
					et la table détail*/
					
					$reponse = $bdd->query('SELECT
									  dbvente.periode.An,
									  dbvente.sous_produit_detail.Nom_Sous_Produit_Detail,
									  dbvente.unites.Nom_Unite,
									  dbvente.vente_sous_detail.Volume,										  
									  dbvente.vente_sous_detail.Prix,
									  dbvente.vente_sous_detail.CA,
									  dbvente.vente_sous_detail.CA_Dinars
									FROM
									  dbvente.vente_sous_detail,
									  dbvente.sous_produit_detail,
									  dbvente.periode,
									  dbvente.produit,
									  dbvente.sous_produit,
									  dbvente.unites,
									  dbvente.famille_produit
									WHERE
									  dbvente.famille_produit.ID_Famille = ' . $idf . ' AND
									  dbvente.produit.ID_Produit = ' . $idp . ' AND
									  dbvente.sous_produit.ID_Sous_Produit = ' . $ids . ' AND
									  dbvente.vente_sous_detail.ID_Periode <= ' . $ide . ' ' . '										  
									  dbvente.vente_sous_detail.ID_Marche = 2 AND
									  dbvente.vente_sous_detail.ID_Periode = dbvente.periode.ID_Periode AND
									  dbvente.vente_sous_detail.ID_Sous_Produit_Detail = dbvente.sous_produit_detail.ID_Sous_Produit_Detail AND
									  dbvente.sous_produit_detail.ID_Sous_Produit = dbvente.sous_produit.ID_Sous_Produit AND
									  dbvente.sous_produit.ID_Produit = dbvente.produit.ID_Produit AND
									  dbvente.vente_sous_detail.ID_Unite = dbvente.unites.ID_Unite AND
									  dbvente.famille_produit.ID_Famille = dbvente.produit.ID_Famille
									ORDER BY
									  dbvente.periode.An DESC');
									  
					?>
					<table border="2" width="100%" cellpadding="3">
						<?php
									
							while ($donnees = $reponse->fetch())
							{
								if($annee != $donnees['An']){
									echo '<tr>';
										echo '<th colspan="6">' . $donnees['An']. '</th>';
										echo '<tr>';
											echo '<th> Sous-Produits </th>';
											echo '<th> Unité </th>';
											echo '<th>Volume</th>';
											echo '<th>Prix Moyen</th>';
											echo '<th>Chiffre affaire devise</th>';
											echo '<th>Chiffre affaire dinars</th>';	
										echo '</tr>';	
									echo '</tr>';
								}
								
								echo '<tr><td>' . htmlspecialchars($donnees['Nom_Sous_Produit_Detail']) . '</td>';
									echo '<td>' . $donnees['Nom_Unite']. '</td>';	
									echo '<td>' . $donnees['Volume']. '</td>';
									if ($donnees['Prix']!=0){
										echo '<td>' . $donnees['Prix']. '</td>';}else{echo '<td>ND</td>';}
									if ($donnees['CA']!=0){
										echo '<td>' . $donnees['CA']. '</td>';}else{echo '<td>ND</td>';}
									if ($donnees['CA_Dinars']!=0){
										echo '<td>' . $donnees['CA_Dinars']. '</td>';}else{echo '<td>ND</td>';}
									$annee = $donnees['An'];
							}
							
						?>
					</table>					
					<?php
				}
				//SINON
				else
				{
					//SI sous produit selectionné ALORS
					if($idp>0)
					{
						/*afficher toutes les ventes réalisées du produit
						je manipile à ce niveau la table vente globale
						et la table de vente de sous produit*/
						
						$reponse = $bdd->query('SELECT
										  dbvente.periode.An,
										  dbvente.sous_produit.Nom_Sous_Produit,
										  dbvente.unites.Nom_Unite,
										  dbvente.vente_detail.Volume,
										  dbvente.vente_detail.Prix,
										  dbvente.vente_detail.CA,
										  dbvente.vente_detail.CA_Dinars
										FROM
										  dbvente.periode,
										  dbvente.sous_produit,
										  dbvente.vente_detail,
										  dbvente.produit,
										  dbvente.famille_produit,
										  dbvente.unites
										WHERE
										  dbvente.vente_detail.ID_Periode = dbvente.periode.ID_Periode AND
										  dbvente.vente_detail.ID_Sous_Produit = dbvente.sous_produit.ID_Sous_Produit AND
										  dbvente.sous_produit.ID_Produit = dbvente.produit.ID_Produit AND
										  dbvente.produit.ID_Famille = dbvente.famille_produit.ID_Famille AND
										  dbvente.vente_detail.ID_Unite = dbvente.unites.ID_Unite AND
										  dbvente.vente_detail.ID_Marche = 2 AND
										  dbvente.famille_produit.ID_Famille = ' . $idf . ' AND
										  dbvente.sous_produit.ID_Produit = ' . $idp . ' AND
										  dbvente.vente_detail.ID_Periode <= ' . $ide . ' ' .'
										ORDER BY
										  dbvente.periode.An DESC');
										  
							?>
						<table border="2" width="100%" cellpadding="3">
							<?php
										
								while ($donnees = $reponse->fetch())
								{
									if($annee != $donnees['An']){
										echo '<tr>';
											echo '<th colspan="6">' . $donnees['An']. '</th>';
											echo '<tr>';
												echo '<th> Sous-Produits </th>';
												echo '<th> Unité </th>';
												echo '<th>Volume</th>';
												echo '<th>Prix Moyen</th>';
												echo '<th>Chiffre affaire devise</th>';
												echo '<th>Chiffre affaire dinars</th>';	
											echo '</tr>';	
										echo '</tr>';
									}
									
									echo '<tr><td>' . htmlspecialchars($donnees['Nom_Sous_Produit']) . '</td>';
										echo '<td>' . $donnees['Nom_Unite']. '</td>';	
										echo '<td>' . $donnees['Volume']. '</td>';
										if ($donnees['Prix']!=0){
											echo '<td>' . $donnees['Prix']. '</td>';}else{echo '<td>ND</td>';}
										if ($donnees['CA']!=0){
											echo '<td>' . $donnees['CA']. '</td>';}else{echo '<td>ND</td>';}
										if ($donnees['CA_Dinars']!=0){
											echo '<td>' . $donnees['CA_Dinars']. '</td>';}else{echo '<td>ND</td>';}
										$annee = $donnees['An'];
								}
								
							?>
						</table>					
						<?php
					}
					else
					{
						if($idf>0)
						{
							/*afficher toutes les ventes réalisées de la famille 
							je manipile à ce niveau la table vente globale*/
							
							$reponse = $bdd->query('SELECT
												  dbvente.periode.An,
												  dbvente.produit.Nom_Produit,
												  dbvente.unites.ID_Unite,
												  dbvente.vente.Volume,
												  dbvente.vente.Prix,
												  dbvente.vente.CA,
												  dbvente.vente.CA_Dinars
												FROM
												  dbvente.periode,
												  dbvente.produit,
												  dbvente.vente,
												  dbvente.unites
												WHERE
												  dbvente.vente.ID_Periode = dbvente.periode.ID_Periode AND
												  dbvente.vente.ID_Produit = dbvente.produit.ID_Produit AND
												  dbvente.vente.ID_unite = dbvente.unites.ID_Unite AND
												  dbvente.vente.ID_Marche = 2 AND
												  dbvente.vente.ID_Famille = ' . $idf . ' ' . 'AND
												  dbvente.vente.ID_Periode <= ' . $ide . ' '.'
												ORDER BY
												  dbvente.periode.An DESC');

							?>
						
							<table border="2" width="100%" cellpadding="3">
								<?php
											
									while ($donnees = $reponse->fetch())
									{
										if($annee != $donnees['An']){
											echo '<tr>';
												echo '<th colspan="6">' . $donnees['An']. '</th>';
												echo '<tr>';
													echo '<th> Produit </th>';
													echo '<th> Unité </th>';
													echo '<th>Volume</th>';
													echo '<th>Prix Moyen</th>';
													echo '<th>Chiffre affaire devise</th>';
													echo '<th>Chiffre affaire dinars</th>';	
												echo '</tr>';	
											echo '</tr>';
										}
										
										echo '<tr><td>' . htmlspecialchars($donnees['Nom_Produit']) . '</td>';
											echo '<td>' . $donnees['Nom_Unite']. '</td>';	
											echo '<td>' . $donnees['Volume']. '</td>';
											if ($donnees['Prix']!=0){
												echo '<td>' . $donnees['Prix']. '</td>';}else{echo '<td>ND</td>';}
											if ($donnees['CA']!=0){
												echo '<td>' . $donnees['CA']. '</td>';}else{echo '<td>ND</td>';}
											if ($donnees['CA_Dinars']!=0){
												echo '<td>' . $donnees['CA_Dinars']. '</td>';}else{echo '<td>ND</td>';}
											$annee = $donnees['An'];
									}
									
								?>
							</table>
							
							<?php
						}
					}
				}
			}
		}
		//SINON
		else{
			/*A ce niveau tout l'historique est à prendre en considération*/
			
			if($idd>0)
			{
				$reponse = $bdd->query('SELECT
									  dbvente.periode.An,
									  dbvente.sous_produit_detail.Nom_Sous_Produit_Detail,
									  dbvente.unites.Nom_Unite,
									  dbvente.vente_sous_detail.Volume,
									  dbvente.vente_sous_detail.Prix,
									  dbvente.vente_sous_detail.CA,
									  dbvente.vente_sous_detail.CA_Dinars
									FROM
									  dbvente.vente_sous_detail,
									  dbvente.sous_produit_detail,
									  dbvente.periode,
									  dbvente.produit,
									  dbvente.sous_produit,
									  dbvente.unites,
									  dbvente.famille_produit
									WHERE
									  dbvente.famille_produit.ID_Famille = ' . $idf . ' AND
									  dbvente.produit.ID_Produit = ' . $idp . ' AND
									  dbvente.sous_produit.ID_Sous_Produit = ' . $ids . ' AND
									  dbvente.vente_sous_detail.ID_Marche = 2 AND 
									  dbvente.sous_produit_detail.ID_Sous_Produit_Detail = ' . $idd . ' AND
									  dbvente.vente_sous_detail.ID_Periode = dbvente.periode.ID_Periode AND
									  dbvente.vente_sous_detail.ID_Sous_Produit_Detail = dbvente.sous_produit_detail.ID_Sous_Produit_Detail AND
									  dbvente.sous_produit_detail.ID_Sous_Produit = dbvente.sous_produit.ID_Sous_Produit AND
									  dbvente.sous_produit.ID_Produit = dbvente.produit.ID_Produit AND
									  dbvente.vente_sous_detail.ID_Unite = dbvente.unites.ID_Unite AND
									  dbvente.famille_produit.ID_Famille = dbvente.produit.ID_Famille
									ORDER BY
									  dbvente.periode.An DESC');
									  
				?>
				<table border="2" width="100%" cellpadding="3">
					<?php
								
						while ($donnees = $reponse->fetch())
						{
							if($annee != $donnees['An']){
								echo '<tr>';
									echo '<th colspan="6">' . $donnees['An']. '</th>';
									echo '<tr>';
										echo '<th> Sous-Produits </th>';
										echo '<th> Unité </th>';
										echo '<th>Volume</th>';
										echo '<th>Prix Moyen</th>';
										echo '<th>Chiffre affaire devise</th>';
										echo '<th>Chiffre affaire dinars</th>';	
									echo '</tr>';	
								echo '</tr>';
							}
							
							echo '<tr><td>' . htmlspecialchars($donnees['Nom_Sous_Produit_Detail']) . '</td>';
								echo '<td>' . $donnees['Nom_Unite']. '</td>';	
								echo '<td>' . $donnees['Volume']. '</td>';
								if ($donnees['Prix']!=0){
									echo '<td>' . $donnees['Prix']. '</td>';}else{echo '<td>ND</td>';}
								if ($donnees['CA']!=0){
									echo '<td>' . $donnees['CA']. '</td>';}else{echo '<td>ND</td>';}
								if ($donnees['CA_Dinars']!=0){
									echo '<td>' . $donnees['CA_Dinars']. '</td>';}else{echo '<td>ND</td>';}
								$annee = $donnees['An'];
						}
						
					?>
				</table>					
				<?php
			}
			//SINON
			else
			{
				 //SI produit selectionné ALORS
				if($ids>0)
				{
					/*afficher toutes les ventes réalisées du sous produit
					je manipule à ce niveau la table vente de sous produit
					et la table détail*/
					
					$reponse = $bdd->query('SELECT
									  dbvente.periode.An,
									  dbvente.sous_produit_detail.Nom_Sous_Produit_Detail,
									  dbvente.unites.Nom_Unite,
									  dbvente.vente_sous_detail.Volume,										  
									  dbvente.vente_sous_detail.Prix,
									  dbvente.vente_sous_detail.CA,
									  dbvente.vente_sous_detail.CA_Dinars
									FROM
									  dbvente.vente_sous_detail,
									  dbvente.sous_produit_detail,
									  dbvente.periode,
									  dbvente.produit,
									  dbvente.sous_produit,
									  dbvente.unites,
									  dbvente.famille_produit
									WHERE
									  dbvente.famille_produit.ID_Famille = ' . $idf . ' AND
									  dbvente.produit.ID_Produit = ' . $idp . ' AND
									  dbvente.sous_produit.ID_Sous_Produit = ' . $ids . ' AND										  
									  dbvente.vente_sous_detail.ID_Marche = 2 AND
									  dbvente.vente_sous_detail.ID_Periode = dbvente.periode.ID_Periode AND
									  dbvente.vente_sous_detail.ID_Sous_Produit_Detail = dbvente.sous_produit_detail.ID_Sous_Produit_Detail AND
									  dbvente.sous_produit_detail.ID_Sous_Produit = dbvente.sous_produit.ID_Sous_Produit AND
									  dbvente.sous_produit.ID_Produit = dbvente.produit.ID_Produit AND
									  dbvente.vente_sous_detail.ID_Unite = dbvente.unites.ID_Unite AND
									  dbvente.famille_produit.ID_Famille = dbvente.produit.ID_Famille
									ORDER BY
									  dbvente.periode.An DESC');
									  
					?>
					<table border="2" width="100%" cellpadding="3">
						<?php
									
							while ($donnees = $reponse->fetch())
							{
								if($annee != $donnees['An']){
									echo '<tr>';
										echo '<th colspan="6">' . $donnees['An']. '</th>';
										echo '<tr>';
											echo '<th> Sous-Produits </th>';
											echo '<th> Unité </th>';
											echo '<th>Volume</th>';
											echo '<th>Prix Moyen</th>';
											echo '<th>Chiffre affaire devise</th>';
											echo '<th>Chiffre affaire dinars</th>';	
										echo '</tr>';	
									echo '</tr>';
								}
								
								echo '<tr><td>' . htmlspecialchars($donnees['Nom_Sous_Produit_Detail']) . '</td>';
									echo '<td>' . $donnees['Nom_Unite']. '</td>';	
									echo '<td>' . $donnees['Volume']. '</td>';
									if ($donnees['Prix']!=0){
										echo '<td>' . $donnees['Prix']. '</td>';}else{echo '<td>ND</td>';}
									if ($donnees['CA']!=0){
										echo '<td>' . $donnees['CA']. '</td>';}else{echo '<td>ND</td>';}
									if ($donnees['CA_Dinars']!=0){
										echo '<td>' . $donnees['CA_Dinars']. '</td>';}else{echo '<td>ND</td>';}
									$annee = $donnees['An'];
							}
							
						?>
					</table>					
					<?php
				}
				//SINON
				else
				{
					//SI sous produit selectionné ALORS
					if($idp>0)
					{
						/*afficher toutes les ventes réalisées du produit
						je manipile à ce niveau la table vente globale
						et la table de vente de sous produit*/
						
						$reponse = $bdd->query('SELECT
										  dbvente.periode.An,
										  dbvente.sous_produit.Nom_Sous_Produit,
										  dbvente.unites.Nom_Unite,
										  dbvente.vente_detail.Volume,
										  dbvente.vente_detail.Prix,
										  dbvente.vente_detail.CA,
										  dbvente.vente_detail.CA_Dinars
										FROM
										  dbvente.periode,
										  dbvente.sous_produit,
										  dbvente.vente_detail,
										  dbvente.produit,
										  dbvente.famille_produit,
										  dbvente.unites
										WHERE
										  dbvente.vente_detail.ID_Periode = dbvente.periode.ID_Periode AND
										  dbvente.vente_detail.ID_Sous_Produit = dbvente.sous_produit.ID_Sous_Produit AND
										  dbvente.sous_produit.ID_Produit = dbvente.produit.ID_Produit AND
										  dbvente.produit.ID_Famille = dbvente.famille_produit.ID_Famille AND
										  dbvente.vente_detail.ID_Unite = dbvente.unites.ID_Unite AND
										  dbvente.vente_detail.ID_Marche = 2 AND
										  dbvente.famille_produit.ID_Famille = ' . $idf . ' AND
										  dbvente.sous_produit.ID_Produit = ' . $idp . ' '.'
										ORDER BY
										  dbvente.periode.An DESC');
										  
						?>
						<table border="2" width="100%" cellpadding="3">
							<?php
										
								while ($donnees = $reponse->fetch())
								{
									if($annee != $donnees['An']){
										echo '<tr>';
											echo '<th colspan="6">' . $donnees['An']. '</th>';
											echo '<tr>';
												echo '<th> Sous-Produits </th>';
												echo '<th> Unité </th>';
												echo '<th>Volume</th>';
												echo '<th>Prix Moyen</th>';
												echo '<th>Chiffre affaire devise</th>';
												echo '<th>Chiffre affaire dinars</th>';	
											echo '</tr>';	
										echo '</tr>';
									}
									
									echo '<tr><td>' . htmlspecialchars($donnees['Nom_Sous_Produit']) . '</td>';
										echo '<td>' . $donnees['Nom_Unite']. '</td>';	
										echo '<td>' . $donnees['Volume']. '</td>';
										if ($donnees['Prix']!=0){
											echo '<td>' . $donnees['Prix']. '</td>';}else{echo '<td>ND</td>';}
										if ($donnees['CA']!=0){
											echo '<td>' . $donnees['CA']. '</td>';}else{echo '<td>ND</td>';}
										if ($donnees['CA_Dinars']!=0){
											echo '<td>' . $donnees['CA_Dinars']. '</td>';}else{echo '<td>ND</td>';}
										$annee = $donnees['An'];
								}
								
							?>
						</table>
					
						<?php
					}
					else
					{
						if($idf>0)
						{
							/*afficher toutes les ventes réalisées de la famille 
							je manipile à ce niveau la table vente globale*/
							
							$reponse = $bdd->query('SELECT
												  dbvente.periode.An,
												  dbvente.produit.Nom_Produit,
												  dbvente.unites.Nom_Unite,
												  dbvente.vente.Volume,
												  dbvente.vente.Prix,
												  dbvente.vente.CA,
												  dbvente.vente.CA_Dinars
												FROM
												  dbvente.periode,
												  dbvente.produit,
												  dbvente.vente,
												  dbvente.unites
												WHERE
												  dbvente.vente.ID_Periode = dbvente.periode.ID_Periode AND
												  dbvente.vente.ID_Produit = dbvente.produit.ID_Produit AND
												  dbvente.vente.ID_Unite = dbvente.unites.ID_Unite AND
												  dbvente.vente.ID_Marche = 2 AND
												  dbvente.vente.ID_Famille = ' . $idf . ' ' . '
												ORDER BY
												  dbvente.periode.An DESC');
							
							?>
						
							<table border="2" width="100%" cellpadding="3">
								<?php
											
									while ($donnees = $reponse->fetch())
									{
										if($annee != $donnees['An']){
											echo '<tr>';
												echo '<th colspan="6">' . $donnees['An']. '</th>';
												echo '<tr>';
													echo '<th> Produit </th>';
													echo '<th> Unité </th>';
													echo '<th>Volume</th>';
													echo '<th>Prix Moyen</th>';
													echo '<th>Chiffre affaire devise</th>';
													echo '<th>Chiffre affaire dinars</th>';	
												echo '</tr>';	
											echo '</tr>';
										}
										
										echo '<tr><td>' . htmlspecialchars($donnees['Nom_Produit']) . '</td>';
											echo '<td>' . $donnees['Nom_Unite']. '</td>';	
											echo '<td>' . $donnees['Volume']. '</td>';
											if ($donnees['Prix']!=0){
												echo '<td>' . $donnees['Prix']. '</td>';}else{echo '<td>ND</td>';}
											if ($donnees['CA']!=0){
												echo '<td>' . $donnees['CA']. '</td>';}else{echo '<td>ND</td>';}
											if ($donnees['CA_Dinars']!=0){
												echo '<td>' . $donnees['CA_Dinars']. '</td>';}else{echo '<td>ND</td>';}
											$annee = $donnees['An'];
									}
									
								?>
							</table>
							
							<?php
						}
					}
				}
			}			
		}
	}
}
?>