Comparer les dates de deux tableaux avec while

msi79 Messages postés 509 Date d'inscription lundi 24 août 2009 Statut Membre Dernière intervention 2 mai 2023 - 21 juil. 2019 à 10:28
NHenry Messages postés 15113 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 22 avril 2024 - 25 juil. 2019 à 13:40
je voudrai afficher les approvisionnements et les dépenses effectuées dans le même tableau avec while.
mais il se trouve que pour chaque dépense qui s'affiche l’approvisionnement s'affiche. or chaque approvisionnement doit s'afficher une seule fois dans le tableau.
voici la capture d'ecran de mon tableau.


voici mes tables :
--
-- Structure de la table `apros`
--

CREATE TABLE IF NOT EXISTS `apros` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `date` datetime NOT NULL,
  `montant` int(30) NOT NULL,
  `type` int(11) NOT NULL,
  `ref` varchar(80) NOT NULL,
  `code` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

--
-- Contenu de la table `apros`
--

INSERT INTO `apros` (`id`, `date`, `montant`, `type`, `ref`, `code`) VALUES
(2, '2019-06-01 00:00:00', 1000000, 0, '375625B', 0),
(4, '2019-06-05 00:00:00', 500000, 0, '', 0),
(5, '2019-07-15 00:00:00', 1500000, 0, '', 0);

-- --------------------------------------------------------

--
-- Structure de la table `tbdepense_tmp`
--

CREATE TABLE IF NOT EXISTS `tbdepense_tmp` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `date` datetime NOT NULL,
  `qte` int(11) NOT NULL,
  `montant` int(30) NOT NULL,
  `idDp` int(11) NOT NULL,
  `promo` varchar(30) NOT NULL,
  `mois` int(11) NOT NULL,
  `clef` varchar(30) NOT NULL,
  `accord` enum('0','1') NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Contenu de la table `tbdepense_tmp`
--

INSERT INTO `tbdepense_tmp` (`id`, `date`, `qte`, `montant`, `idDp`, `promo`, `mois`, `clef`, `accord`) VALUES
(1, '2019-06-20 00:29:29', 1, 55000, 2, '2019', 7, 'xKZL2459', '0'),
(2, '2019-06-20 08:16:52', 5, 4500, 16, '2019', 7, 'axK06789', '0'),
(3, '2019-07-21 07:12:52', 10, 80000, 45, '2019', 7, 'KZTL0134', '0'),
(4, '2019-07-21 07:20:07', 15, 80000, 48, '2019', 7, 'xKLV1268', '0');



voici mon code php
<table class="table">
  <tr>
    <th style="width:10%">DATE</th>
    <th style="width:10%">APROS</th>
    <th style="width:25%">DESIGNATION</th>
    <th style="width:25%">BENEFICIAIRES</th>
    <th style="width:10%">LES DEPENSES</th>
    <th style="width:10%">LE SOLDE</th>
  </tr>
<?php
if($nbr_date_dernier ==1):
try{
$req_repo = $pdo->query($sql_date_dernier);
}catch(PDOException  $e){
     echo "ERREUR DE REQUETE : " . $sql_date_dernier . '  error : '.$e->getMessage();
}
$nbr_repo = $req_repo->rowCount();
$resul_repo = $req_repo->fetch(PDO::FETCH_ASSOC);
$daterepots = explode(" ",$resul_repo["date"]);
$datesrepo = explode("-",$daterepots[0]);       
  
?>
  <tr style="; font-weight:bold;">
    <th style="width:2%"><?php echo  $datesrepo[2].'/'.$datesrepo[1].'/'.$datesrepo[0];; ?></th>
    <th style="width:20%; background-color:red"><?php echo number_format($resul_repo['montant'], 0, ',', ' ') ; ?></th>
    <th colspan="3" style="width:5%; background-color:#FF0">REPORT</th>
    <th style="width:7%"><?php echo number_format($resul_repo['montant'], 0, ',', ' '); ?></th>
  </tr>

 <?php 
 endif;
   	 $sql_1 = "SELECT *
			  FROM tbdepense_tmp D
			       ,objet_demandes O
			  WHERE D.clef = O.clef
			  AND MONTH(D.date) = '".$_GET['mois']."'
			  AND YEAR(D.date) = '".$_GET['annee']."'
			  ORDER BY D.date
	         ";

// on envoie la requête
try{
     $req_1 = $pdo->prepare($sql_1);
     $req_1->execute();
     $resulsMDP = $req_1->fetchAll();
}catch(PDOException  $e){
     echo "ERREUR DE REQUETE : " . $sql_1 . '  error : '.$e->getMessage();
}

//echo $sql;
$nbr = !empty($resulsMDP) ? count($resulsMDP) : 0;
if($nbr > 0):
		  $rang=0;
           foreach($resulsMDP as $resulMDP):
		   $rang++;
$dateInits = explode(" ",$resulMDP["date"]);
$dates = explode("-",$dateInits[0]);          		   

	    $sql_resd = "SELECT *
        FROM depenses 
		WHERE id = '".$resulMDP['idDp']."'
    ";
try{
$req_resd = $pdo->query($sql_resd);
}catch(PDOException  $e){
     echo "ERREUR DE REQUETE : " . $sql_resd . '  error : '.$e->getMessage();
}
$nbr_resd = $req_resd->rowCount();
$liste = $req_resd->fetch(PDO::FETCH_ASSOC);

$sql_destinataire = "SELECT *
        FROM employers 
		WHERE id = '".$resulMDP['destinataire']."'
    ";
try{
$req_destinataire = $pdo->query($sql_destinataire);
}catch(PDOException  $e){
     echo "ERREUR DE REQUETE : " . $sql_destinataire . '  error : '.$e->getMessage();
}
$nbr_destinataire = $req_destinataire->rowCount();
$liste_destinataire = $req_destinataire->fetch(PDO::FETCH_ASSOC);
//RECUPERATION DES APPROS
//VERIF L'EXISTENCE DU TELEPHONE
	 $sql_apros = "SELECT * 
			  FROM apros
			  WHERE MONTH(date) = '".$_GET['mois']."'
			  AND YEAR(date) = '".$_GET['annee']."'
	         ";

// on envoie la requête
try{
     $req_apros = $pdo->prepare($sql_apros);
     $req_apros->execute();
     $resulsAP = $req_apros->fetchAll();
}catch(PDOException  $e){
     echo "ERREUR DE REQUETE : " . $sql_apros . '  error : '.$e->getMessage();
}

//echo $sql;
$nbrAP = !empty($resulsAP) ? count($resulsAP) : 0;
?>
<?php
if($nbrAP > 0):
foreach($resulsAP as $resulAP):
$dateapros = explode(" ",$resulAP["date"]);
$datesAPRO = explode("-",$dateapros[0]);
 if($resulAP["date"]<$resulMDP["date"]):
?>
  <tr style="; font-weight:bold;">
    <th style="width:2%"> </th>
    <th style="width:20%"> </th>
    <th colspan="3" style="width:5%"> </th>
    <th style="width:7%"> </th>
  </tr>
  <tr style="; font-weight:bold;">
    <th style="width:2%"><?php echo  $datesAPRO[2].'/'.$datesAPRO[1].'/'.$datesAPRO[0];; ?></th>
    <th style="width:20%; background-color:red;"> </th>
    <th colspan="3" style="width:5%; background-color:#FF0">APROS</th>
    <th style="width:7%"><?php echo number_format($resulAP['montant'], 0, ',', ' '); ?></th>
  </tr>
<?php
 endif;
endforeach;
endif;
 ?>
  <tr>
    <td align="center">
	<?php echo  $dates[2].'/'.$dates[1].'/'.$dates[0]; ?> 
    </td>
    <td><?php echo $resulMDP['civilite'].' '. $resulMDP['nom'].' '. $resulMDP['prenom']; ?></td>
    <td align="center"><?php echo $liste['depense']; ?></td>
    <td align="right"><?php echo $liste_destinataire['nom'].'  '.$liste_destinataire['prenom']; ?></td>
    <td align="right"><?php  echo number_format($resulMDP['montant']*$resulMDP['qte'], 0, ',', ' ');  ?></td>
    <td align="right"> </td>
  </tr>
  <?php 
    endforeach;
   endif; 
  

?>

  <tr style="background-color:#F5F5F5">
    <td> </td>
    <td> </td>
    <td> </td>
    <td align="right"><strong><?php  echo number_format($resul_FRAIS_DOSSIER['TOTAUX'], 0, ',', ' ');  ?></strong></td>
    <td align="right"><strong><?php  echo number_format($resul_TOTAUX['TOTAUX'], 0, ',', ' ');  ?></strong></td>
    <td align="right"> </td>
  </tr>
  
  <?php
if($nbrAP > 0):
foreach($resulsAP as $resulAP):
$dateapros = explode(" ",$resulAP["date"]);
$datesAPRO = explode("-",$dateapros[0]);
 if($resulAP["date"]>=$resulMDP["date"]):
?>
  <tr style="; font-weight:bold;">
    <th style="width:2%"> </th>
    <th style="width:20%"> </th>
    <th colspan="3" style="width:5%"> </th>
    <th style="width:7%"> </th>
  </tr>
  <tr style="; font-weight:bold;">
    <th style="width:2%"><?php echo  $datesAPRO[2].'/'.$datesAPRO[1].'/'.$datesAPRO[0];; ?></th>
    <th style="width:20%; background-color:red;"> </th>
    <th colspan="3" style="width:5%; background-color:#FF0">APROS</th>
    <th style="width:7%"><?php echo number_format($resulAP['montant'], 0, ',', ' '); ?></th>
  </tr>
<?php
 endif;
endforeach;
endif;
 ?>
</table>

1 réponse

NHenry Messages postés 15113 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 22 avril 2024 159
21 juil. 2019 à 11:44
Il te faut mémoriser le dernier approvisionnement utilisé et uniquement si ça change, alors afficher une nouvelle ligne d'entête.

Concernant la mise en forme, il est recommander de séparer le PHP du HTML, pour cela Twig pourrait t'aider.
Et en plus, en gérant bien, tu pourra mettre dans le contexte ton arborescence de ton tableau.
$Context['Approvisionnements']['Apros']['Items'][]=array('Date'=>"", ...)

0
msi79 Messages postés 509 Date d'inscription lundi 24 août 2009 Statut Membre Dernière intervention 2 mai 2023 1
21 juil. 2019 à 19:33
serieusement je comprends rien dans cette reponse ou bien j'ai pas bien expliqué ma préoccupation ?
0
msi79 Messages postés 509 Date d'inscription lundi 24 août 2009 Statut Membre Dernière intervention 2 mai 2023 1
22 juil. 2019 à 15:05
Personne pour m'aider ?
0
NHenry Messages postés 15113 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 22 avril 2024 159
22 juil. 2019 à 20:38
En gros, il faut que tu :
- Mémorise le dernier approvisionnement utilisé
- Lire une nouvelle ligne
- Si le numéro d'approvisionnement est différent du précédent, mettre l'entête
- Repartir pour un tour.
0
msi79 Messages postés 509 Date d'inscription lundi 24 août 2009 Statut Membre Dernière intervention 2 mai 2023 1
23 juil. 2019 à 22:44
je ne comprends toujours pas comment m'y prendre avec ta solution.
mais j'ai une idée et je sais pas comment l'ecrire en php.
voici mon idée pour résoudre.

il me faut comparer les dates d'approvisionnement à celles des dépenses .
au cours de l'affichage des dépenses, si un approvisionnement est fait on l'affiche si sa date est inférieur a celle de la dépense.

le problème que je rencontre est que la date d'approvisionnement s'affiche plusieurs fois
0
NHenry Messages postés 15113 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 22 avril 2024 159
25 juil. 2019 à 13:40
Désolé pour les délais de réponse.

Pour simplifier ton code, commence par préparer tes données (mettre dans un tableau, ...)
Puis ensuite, génère le code HTML associé.
0
Rejoignez-nous