Affichage sélectif requête sql

Répondre


Cette question est un moyen d’empêcher des soumissions automatisées de formulaires par des robots.
Smileys
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: =D> #-o =P~ :^o :non: :priere: 8-|
Voir plus de smileys
  Revue du sujet
 

  Étendre la vue Revue du sujet : Affichage sélectif requête sql

Re: Affichage sélectif requête sql

par orell1 » 29 août 2011, 12:31

Très bien je vais essayer ça alros.
Merci infiniment.

Re: Affichage sélectif requête sql

par Mazarini » 29 août 2011, 12:17

Je te conseille de faire des fonctions pour afficher les différentes parties.

Une fonction debut_tableau(), une fonction ligne_tableau() et une fonction fin_tableau().
Ensuite tu appelles les fonctions selon tes besoins en t'inspirant du code presque PHP que je t'ai indiqué.

Pour chaque fonctinon tu pourras mettre des paramètres selon les besoins.
- pour debut_tableau(), le mois
- pour ligne_tableau(), le resultat de la lecture
- pour fin_tableau(), a priori pas de paramètre

Re: Affichage sélectif requête sql

par orell1 » 29 août 2011, 12:08

Merci...

un truc de ce style?
<?php
while ($calendrier = mysql_fetch_array($result)) //(lecture d'une ligne de la table)
{
  $mois = "DECEMBRE";
   if ($mois_courant <> $mois)
   {
       if ($mois_courant <> "") //fermer le tableau;
       $mois_courant = $mois;
       //commencer un nouveau tableau;
   }
  // traiter la ligne 
  ?>
  <table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
  
    <td width="16%" align="center" valign="middle" bgcolor="#5bc210" style="padding-bottom:10px;"> <strong><center><?php echo $calendrier['mois']?></center></strong></td>

    
    <td width="32%">&nbsp;</td>
    <td width="22%">&nbsp;</td>
    <td width="15%">&nbsp;</td>
    <td width="15%">&nbsp;</td>
  </tr>
  <tr bgcolor="#5bc210">
    <td style="padding-bottom:10px; padding-left:5px;">Date</td>
    <td style="padding-bottom:10px; padding-left:5px;">Epreuve</td>
    <td style="padding-bottom:10px; padding-left:5px;">Formule</td>
    <td style="padding-bottom:10px; padding-left:5px;">Lieux</td>
    <td style="padding-bottom:10px; padding-left:5px;">Infos</td>
  </tr>
  
  <tr bgcolor="#252525">
    <td style="padding-bottom:10px; padding-left:5px; color:#9c9d9f;"><?php echo $calendrier['date'] ?></td>
    <td class="bordure" style="padding-bottom:10px; padding-left:5px; color:#9c9d9f;"><?php echo $calendrier['epreuve'] ?></td>
    <td style="padding-bottom:10px; padding-left:5px; color:#9c9d9f;"><?php echo $calendrier['formule'] ?></td>
    <td class="bordure" style="padding-bottom:10px; padding-left:5px; color:#9c9d9f;"><?php echo $calendrier['lieux'] ?></td>
    <td style="padding-bottom:10px; padding-left:5px; color:#9c9d9f;"><center><a href="<?php echo $calendrier['upload'] ?>"><img src="images/pdf.jpg" alt="Télécharger le PDF" /></a>
    </center></td>

  </tr>
</table>   

<?php
}
if ($mois_courant <> "") //fermer le tableau;
?>
<br />

Re: Affichage sélectif requête sql

par Mazarini » 29 août 2011, 11:57

Quelque chose du genre :
$mois_courant = "";
while (lecture d'une ligne de la table)
{
  $mois = le mois de la requete;
   if ($mois_courant <> $mois)
   {
       if ($mois_courant <> "") fermer le tableau;
       $mois_courant = $mois;
       commencer un nouveau tableau;
   }
   traiter la ligne    
}
if ($mois_courant <> "") fermer le tableau; 

Re: Affichage sélectif requête sql

par orell1 » 29 août 2011, 11:20

Je suis désolé mais je ne comprends pas trop comment faire... :(

Re: Affichage sélectif requête sql

par orell1 » 29 août 2011, 11:17

Peut être est- ce comme ça : $mois_courant = "";

Re: Affichage sélectif requête sql

par orell1 » 29 août 2011, 11:13

Bonjour,
Merci pour ta réponse.
Peux-tu me dire STP ce qu'est mettre une variable à blanc..?

Re: Affichage sélectif requête sql

par Mazarini » 29 août 2011, 11:07

Bonjour,

Il te faudrait gérer gérer une rupture sur le mois pour faire un tableau par mois présent dans la table.

Initialise une variable $mois_courant à blanc avant la boucle de lecture.
Dans la boucle, chaque fois qu'il y a un changement de mois, ferme le tableau en cours (sauf premier mois), commence un nouveau tableau et alimente la valeur de $mois_courant.
Après la boucle, ferme le tableau du dernier mois traité (sauf si pas de mois dans la table).

Affichage sélectif requête sql

par orell1 » 29 août 2011, 09:51

Bonjour,
j'essaie de retourner des résultats par le biais d'une requête sql mais l'affichage ne se fait pas comme je le souhaite...

Sont chargés dans ma base sql des événements pour l'année 2011 allant de JANVIER à DECEMBRE.
Ses informations sont affichées grace à un tableau en php.

Le problème c'est que toutes mes infos s'affichent dans le même tableau et non dans un tableau différent pour chaque mois.
Je souhaite que mes infos soient ordonnés par mois mais malgré les if tout reste dans le même tableau...

Voici la base sql:
--
-- Structure de la table `calendrier`
--

CREATE TABLE IF NOT EXISTS `calendrier` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `mois` varchar(20) COLLATE latin1_general_ci NOT NULL,
  `date` date NOT NULL,
  `epreuve` text COLLATE latin1_general_ci NOT NULL,
  `formule` text COLLATE latin1_general_ci NOT NULL,
  `lieux` text COLLATE latin1_general_ci NOT NULL,
  `upload` varchar(150) COLLATE latin1_general_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=10 ;

--
-- Contenu de la table `calendrier`
--

INSERT INTO `calendrier` (`id`, `mois`, `date`, `epreuve`, `formule`, `lieux`, `upload`) VALUES
(1, 'MARS', '2011-03-30', 'Pro''AM', '1+1', 'Bethemont', ''),
(2, 'JUILLET', '2011-07-24', 'Ceci est un test', 'deux joueurs', 'Villennes sur seine', 'docs_calendrier/'),
(3, 'AVRIL', '2011-04-19', 'test test test test', 'youplaboum', 'Paris 17', ''),
(4, 'DECEMBRE', '2011-12-29', 'Test', 'deux + deux', 'Chambourcy', 'upload/'),
(5, 'DECEMBRE', '2011-12-27', 'Test', '3 + 3', 'Chambourcy', 'upload/'),
(6, 'DECEMBRE', '2011-12-20', 'Test', '4+4', 'Chambourcy', 'upload/'),
(7, 'DECEMBRE', '2011-12-12', 'Test', '5+5', 'Chambourcy', 'upload/'),
(8, 'DECEMBRE', '2011-12-05', 'Test', '6+6', 'Chambourcy', 'upload/'),
(9, 'DECEMBRE', '2011-12-10', 'Test', '7+7', 'Chambourcy', 'upload/');
Et la mon PHP qui permet d'afficher les résultats suivant chaque mois (pour le moment j'ai fait le test sur le mois de mars et decembre...
<?php

//------------------------------ entete de la page -----------------------------------------
 include_once("haut.php");
//------------------------------------------------------------------------------------------

//------------------------------CONNEXION BASE CALENDRIER--------------------------//

        $conn = db_connect();
        $sql = "SELECT * FROM calendrier ORDER by date ASC";
        $result = mysql_query($sql, $conn);
		$resultat = mysql_query($sql, $conn);
		$mois = mysql_fetch_array($result);
?>

<fieldset class="fieldset">
<legend class="legend"><span class="titr2">Calendrier 2011</span></legend>


<?php 
if ($mois['mois'] == "MARS") { 
while ($calendrier = mysql_fetch_array($result)) { 
?>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
  
    <td width="16%" align="center" valign="middle" bgcolor="#5bc210" style="padding-bottom:10px;"> <strong><center><?php echo $calendrier['mois']?></center></strong></td>

    
    <td width="32%">&nbsp;</td>
    <td width="22%">&nbsp;</td>
    <td width="15%">&nbsp;</td>
    <td width="15%">&nbsp;</td>
  </tr>
  <tr bgcolor="#5bc210">
    <td style="padding-bottom:10px; padding-left:5px;">Date</td>
    <td style="padding-bottom:10px; padding-left:5px;">Epreuve</td>
    <td style="padding-bottom:10px; padding-left:5px;">Formule</td>
    <td style="padding-bottom:10px; padding-left:5px;">Lieux</td>
    <td style="padding-bottom:10px; padding-left:5px;">Infos</td>
  </tr>
  
  <tr bgcolor="#252525">
    <td style="padding-bottom:10px; padding-left:5px; color:#9c9d9f;"><?php echo $calendrier['date'] ?></td>
    <td class="bordure" style="padding-bottom:10px; padding-left:5px; color:#9c9d9f;"><?php echo $calendrier['epreuve'] ?></td>
    <td style="padding-bottom:10px; padding-left:5px; color:#9c9d9f;"><?php echo $calendrier['formule'] ?></td>
    <td class="bordure" style="padding-bottom:10px; padding-left:5px; color:#9c9d9f;"><?php echo $calendrier['lieux'] ?></td>
    <td style="padding-bottom:10px; padding-left:5px; color:#9c9d9f;"><center><a href="<?php echo $calendrier['upload'] ?>"><img src="images/pdf.jpg" alt="Télécharger le PDF" /></a>
    </center></td>

  </tr>
</table>
<?php } } ?>
<br />


<?php
if ($mois['mois'] == "DECEMBRE") { 
while ($calendrier = mysql_fetch_array($result)) { 
?>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
  
    <td width="16%" align="center" valign="middle" bgcolor="#5bc210" style="padding-bottom:10px;"> <strong><center><?php echo $calendrier['mois']?></center></strong></td>

    
    <td width="32%">&nbsp;</td>
    <td width="22%">&nbsp;</td>
    <td width="15%">&nbsp;</td>
    <td width="15%">&nbsp;</td>
  </tr>
  <tr bgcolor="#5bc210">
    <td style="padding-bottom:10px; padding-left:5px;">Date</td>
    <td style="padding-bottom:10px; padding-left:5px;">Epreuve</td>
    <td style="padding-bottom:10px; padding-left:5px;">Formule</td>
    <td style="padding-bottom:10px; padding-left:5px;">Lieux</td>
    <td style="padding-bottom:10px; padding-left:5px;">Infos</td>
  </tr>
  
  <tr bgcolor="#252525">
    <td style="padding-bottom:10px; padding-left:5px; color:#9c9d9f;"><?php echo $calendrier['date'] ?></td>
    <td class="bordure" style="padding-bottom:10px; padding-left:5px; color:#9c9d9f;"><?php echo $calendrier['epreuve'] ?></td>
    <td style="padding-bottom:10px; padding-left:5px; color:#9c9d9f;"><?php echo $calendrier['formule'] ?></td>
    <td class="bordure" style="padding-bottom:10px; padding-left:5px; color:#9c9d9f;"><?php echo $calendrier['lieux'] ?></td>
    <td style="padding-bottom:10px; padding-left:5px; color:#9c9d9f;"><center><a href="<?php echo $calendrier['upload'] ?>"><img src="images/pdf.jpg" alt="Télécharger le PDF" /></a>
    </center></td>

  </tr>
</table>
<?php } } ?>
<br />

</fieldset>

<?php
//------------------------------------------- bas de page -----------------------------------
 include_once("bas.php"); 
//-------------------------------------------------------------------------------------------
?>
Merci beaucoup.