IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

PHP & Base de données Discussion :

Utiliser 2 fois while dans une page ? [PDO]


Sujet :

PHP & Base de données

  1. #1
    Membre confirmé
    Femme Profil pro
    piano
    Inscrit en
    Décembre 2011
    Messages
    131
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Suisse

    Informations professionnelles :
    Activité : piano
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Décembre 2011
    Messages : 131
    Par défaut Utiliser 2 fois while dans une page ?
    Bonjour,

    Grâce à l'aide de Sabotage, j'ai pu adapter plusieurs page PHP / MYSQL en PHP / PDO mais j'ai un nouveau petit problème, dans cette page j'utilise deux fois le while avec MYSQL et je ne sais pas comment faire avec PDO, de plus je ne sais pas comment compter les rows, j'imagine que c'est avec rowCount, d'après ce que j'ai lu sur le manuel PHP, mais je n'ai pas très bien compris comment l'appliquer à ma page.

    Voici la page en PHP / MYSQL

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    <?php
    include("menu.php");
    //reception de la variable
    $id = isset($_POST['truc']) ? $_POST['truc'] : '';
    $id = $_POST['truc'];
     
    echo '<br /><center><font color="blue"><big><b>ID concert: ' . $id . '</b></center></font></big>'; //nombre de résultats
     
    ?>
     
    <link rel="stylesheet" href="menu/dropdown_three.css" type="text/css">
    <link rel="stylesheet" href="style.css" type="text/css">
     
    <style type="text/css">
    </style>
     
    <?php
     
    function display($string)
    {
        echo htmlentities($string, ENT_QUOTES, 'utf8_general_ci	');
    }
     
    include("include/variables.inc.php");
    $liendb  =  mysql_connect($bddserver,  $bddlogin,  $bddpassword);
    mysql_select_db  ($bdd);
    mysql_set_charset( 'utf8' );
     
     
    $sql = "SELECT *
    		FROM Tab_NumPlace
    		
    		INNER JOIN Personnes ON Tab_NumPlace.IDPersonne = Personnes.IDPersonne
    		INNER JOIN Instruments ON Personnes.IDInstruments = Instruments.IDInstruments
    		INNER JOIN Concert ON Tab_NumPlace.IDconcert = Concert.IDconcert 
    		INNER JOIN Zones ON Tab_NumPlace.IDZone = Zones.IDZone 
    		INNER JOIN Prix ON Zones.IDPrix = Prix.IDPrix
    
    WHERE Concert.IDconcert = '" . $id . "'
    ORDER BY Nom_Personne, Prenom_Personne, Zone, NumPlace";
     
    $resultat  =  mysql_query  ($sql);
    $resultat = mysql_query($sql) or die(mysql_error()); 
     
    while  ($Tab_NumPlace = mysql_fetch_array  ($resultat))
     
    $result = mysql_query($sql);
     
    $nombre_resultats = mysql_num_rows($result); //compte le nombre d'entrées sélectionnées par la recherche
    if ($nombre_resultats == 0) //s'il n'y a pas de résultat
    {
     
    echo '<font color="red"><b><big>Aucun resultat&nbsp;&nbsp;&nbsp;&nbsp;</font></big>
    <input type="button" onclick="location.href=\''.( ( $variable == 1 ) ? 'listeconcerts.php' : 'listeconcerts.php' ).'\'" value="Recommencer" />';	
    /*	
    echo 'aucun resultat.<a href="listeconcerts_resultat.php">recommencer</a>';
    */
    }
    else //il y a au moins un résultat
    {
    echo '<br /><br /><center><font color="blue"><b><big>Nombre de places réservées: ' . $nombre_resultats . '</b></center></font></big><br /><br />'; //nombre de résultats
    }
     
    if($result)
    {
     
    ?>
     
     
    <table class="bicolor" border="1"  align="center">
     
            <tr>
                <th>Date concert</th>
                <th>Zone</th>
                <th>Numéro de place</th>
         	    <th>Prix</th>
                <th>Reférence</th>
                <th>Nom, prénom</th>
    	</tr>
     
    <?php
            while($Tab_NumPlace = mysql_fetch_assoc($result))
    {
    ?>
     
     
                <tr>
    				<td><?php display($Tab_NumPlace['dateconcert']); ?></td>
    				<td><?php display($Tab_NumPlace['Zone']); ?></td>
                                    <td><div class="alindr"><?php display($Tab_NumPlace['NumPlace']); ?></div></td>		
    				<td><div class="alindr"><?php display($Tab_NumPlace['Prix']); ?></div></td>
    				<td><?php display($Tab_NumPlace['Reférence']); ?></td>
                                    <td><?php display($Tab_NumPlace['Nom_Personne'] . ' - ' .  $Tab_NumPlace['Prenom_Personne']); ?></td>
    <?php
     
    }
    ?>
        </table>
     
    <br />
    <br/>
    <center>
    <?php
    echo '<input type="button" onclick="location.href=\''.( ( $variable == 1 ) ? 'listeconcerts.php' : 'listeconcerts.php' ).'\'" value="Recommencer" />';
    ?>
    </center>
     
    <br />
     
    <center>
    <?php
    echo '<input type="button" onclick="location.href=\''.( ( $variable == 1 ) ? 'places.php' : 'places.php' ).'\'" value="Voir toutes les places réservées" />';
    ?>
    </center>
    <?php
    }
     
    mysql_close($liendb);
    ?>
    Et voici ce que j'ai commencé à faire avec PDO :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    <?php
    require 'include/sqlconnect.php';
    include("menu.php");
    $id = isset($_POST['truc']) ? $_POST['truc'] : '';
    $id = $_POST['truc'];
     
    echo '<br /><center><font color="blue"><big><b>ID concert: ' . $id . '</b></center></font></big>'; //nombre de résultats
     
    ?>
     
    <link rel="stylesheet" href="menu/dropdown_three.css" type="text/css">
    <link rel="stylesheet" href="style.css" type="text/css">
     
    <style type="text/css">
    </style>
     
    <?php
     
    function display($string)
    {
        echo htmlentities($string, ENT_QUOTES, 'utf8_general_ci	');
    }
    $sth = $bdd->query("SELECT *
    		FROM Tab_NumPlace
    		
    		INNER JOIN Personnes ON Tab_NumPlace.IDPersonne = Personnes.IDPersonne
    		INNER JOIN Instruments ON Personnes.IDInstruments = Instruments.IDInstruments
    		INNER JOIN Concert ON Tab_NumPlace.IDconcert = Concert.IDconcert 
    		INNER JOIN Zones ON Tab_NumPlace.IDZone = Zones.IDZone 
    		INNER JOIN Prix ON Zones.IDPrix = Prix.IDPrix
    
    WHERE Concert.IDconcert = '" . $id . "'
    ORDER BY Nom_Personne, Prenom_Personne, Zone, NumPlace");
    while($row = $sth->fetch(PDO::FETCH_ASSOC))
     
    	$count = $row->rowCount();
     
     
    //$nombre_resultats = mysql_num_rows($result); //compte le nombre d'entrées sélectionnées par la recherche
    if ($count == 0) //s'il n'y a pas de résultat
    {
     
    echo '<font color="red"><b><big>Aucun resultat&nbsp;&nbsp;&nbsp;&nbsp;</font></big>
    <input type="button" onclick="location.href=\''.( ( $variable == 1 ) ? 'listeconcerts.php' : 'listeconcerts.php' ).'\'" value="Recommencer" />';	
    /*	
    echo 'aucun resultat.<a href="listeconcerts_resultat.php">recommencer</a>';
    */
    }
    else //il y a au moins un résultat
    {
    echo '<br /><br /><center><font color="blue"><b><big>Nombre de places réservées: ' . $count . '</b></center></font></big><br /><br />'; //nombre de résultats
    }
     
    if($count)
    {
     
    ?>
     
     
    <table class="bicolor" border="1"  align="center">
     
            <tr>
                <th>Date concert</th>
    			<th>Zone</th>
                <th>Numéro de place</th>
         	    <th>Prix</th>
    	        <th>Reférence</th>
    			<th>Nom, prénom</th>
    	</tr>
     
    <?php
            while($row = $sth->fetch(PDO::FETCH_ASSOC))
    {
    ?>
     
     
                <tr>
    				<td><?php display($row['dateconcert']); ?></td>
    				<td><?php display($row['Zone']); ?></td>
                    <td><div class="alindr"><?php display($row['NumPlace']); ?></div></td>		
    				<td><div class="alindr"><?php display($row['Prix']); ?></div></td>
    				<td><?php display($row['Reférence']); ?></td>
                    <td><?php display($row['Nom_Personne'] . ' - ' .  $row['Prenom_Personne']); ?></td>
    <?php
     
    }
    ?>
        </table>
     
    <br />
    <br/>
    <center>
    <?php
    echo '<input type="button" onclick="location.href=\''.( ( $variable == 1 ) ? 'listeconcerts.php' : 'listeconcerts.php' ).'\'" value="Recommencer" />';
    ?>
    </center>
     
    <br />
     
    <center>
    <?php
    echo '<input type="button" onclick="location.href=\''.( ( $variable == 1 ) ? 'places.php' : 'places.php' ).'\'" value="Voir toutes les places réservées" />';
    ?>
    </center>
    <?php
    }
     
    $sth->closeCursor(); 
    ?>
    c'est à partir de la ligne 36 ci-dessus.

    Merci pour votre aide et bonne soirée

  2. #2
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Par défaut
    Pourquoi il y a deux fois mysql_query() avec la même requête dans le premier code ?
    N'oubliez pas de consulter les FAQ PHP et les cours et tutoriels PHP

  3. #3
    Membre confirmé
    Femme Profil pro
    piano
    Inscrit en
    Décembre 2011
    Messages
    131
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Suisse

    Informations professionnelles :
    Activité : piano
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Décembre 2011
    Messages : 131
    Par défaut
    je crois que c'est une fois pour compter le nombre de billets et une fois pour afficher les résultats

    Concernant ma page PDO, je suis presque arrivée à quelque chose, ça compte bien le nombre de billets, mais cela n'affiche pas les données

    Voici où j'en suis actuellement :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    <?php
    require 'include/sqlconnect.php';
    include("menu.php");
    $id = isset($_POST['truc']) ? $_POST['truc'] : '';
    $id = $_POST['truc'];
     
    echo '<br /><center><font color="blue"><big><b>ID concert: ' . $id . '</b></center></font></big>'; //nombre de résultats
    ?>
    <link rel="stylesheet" href="menu/dropdown_three.css" type="text/css">
    <link rel="stylesheet" href="style.css" type="text/css">
    <style type="text/css">
    </style>
    <?php
    function display($string)
    {
        echo htmlentities($string, ENT_QUOTES, 'utf8_general_ci	');
    }
    $sth = $bdd->query("SELECT *
    		FROM Tab_NumPlace
    		
    		INNER JOIN Personnes ON Tab_NumPlace.IDPersonne = Personnes.IDPersonne
    		INNER JOIN Instruments ON Personnes.IDInstruments = Instruments.IDInstruments
    		INNER JOIN Concert ON Tab_NumPlace.IDconcert = Concert.IDconcert 
    		INNER JOIN Zones ON Tab_NumPlace.IDZone = Zones.IDZone 
    		INNER JOIN Prix ON Zones.IDPrix = Prix.IDPrix
    
    WHERE Concert.IDconcert = '" . $id . "'
    ORDER BY Nom_Personne, Prenom_Personne, Zone, NumPlace");
    while($row = $sth->fetch(PDO::FETCH_ASSOC))
     
    $count = $sth->rowCount();
    //$nombre_resultats = mysql_num_rows($result); //compte le nombre d'entrées sélectionnées par la recherche
    if ($count == 0) //s'il n'y a pas de résultat
    {
    echo '<font color="red"><b><big>Aucun resultat&nbsp;&nbsp;&nbsp;&nbsp;</font></big>
    <input type="button" onclick="location.href=\''.( ( $variable == 1 ) ? 'listeconcerts.php' : 'listeconcerts.php' ).'\'" value="Recommencer" />';	
    /*	
    echo 'aucun resultat.<a href="listeconcerts_resultat.php">recommencer</a>';
    */
    }
    else //il y a au moins un résultat
    {
    echo '<br /><br /><center><font color="blue"><b><big>Nombre de places réservées: ' . $count . '</b></center></font></big><br /><br />'; //nombre de résultats
    }
     
    if($count)
    {
    ?>
    <table class="bicolor" border="1"  align="center">
            <tr>
                <th>Date concert</th>
                <th>Zone</th>
                <th>Numéro de place</th>
         	    <th>Prix</th>
    	    <th>Reférence</th>
                <th>Nom, prénom</th>
    	</tr>
    <?php
            while($row = $sth->fetch(PDO::FETCH_ASSOC))
    {
        echo  "<tr>";
    	echo  "<td>" . $row['dateconcert'] . "</td>";
    	echo  "<td>" . $row['Zone'] . "</td>";
    	echo  "<td>" . $row['NumPlace'] . "</td>";
    	echo  "<td>" . $row['Prix'] . "</td>";
    	echo  "<td>" . $row['Reférence'] . "</td>";
    	echo  "<td>" . $row['Nom_Personne'] . ' - ' .  $row['Prenom_Personne'] ."</td>";
            echo  "<td>";
    	echo  "<a  href=personnes_edite.php?id=$id&amp;idp=$idp&amp>Editer</a>";
            echo  "</td>";
            echo  "</tr>";	
    }
    ?>
     
    </table>
    <br />
    <br/>
    <center>
    <?php
    echo '<input type="button" onclick="location.href=\''.( ( $variable == 1 ) ? 'listeconcerts.php' : 'listeconcerts.php' ).'\'" value="Recommencer" />';
    ?>
    </center>
    <br />
    <center>
    <?php
    echo '<input type="button" onclick="location.href=\''.( ( $variable == 1 ) ? 'places.php' : 'places.php' ).'\'" value="Voir toutes les places réservées" />';
    ?>
    </center>
    <?php
    }
    $sth->closeCursor(); 
    ?>
    D'avance merci de bien vouloir m'aider

  4. #4
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Par défaut
    Le count et le if qui va avec ne doivent pas être dans la boucle while.
    La boucle while lit les résultats, donc si tu es dedans, c'est qu'il y a des résultats et s'il n'y a pas de résultats, tu n'iras jamais dedans.
    N'oubliez pas de consulter les FAQ PHP et les cours et tutoriels PHP

  5. #5
    Membre confirmé
    Femme Profil pro
    piano
    Inscrit en
    Décembre 2011
    Messages
    131
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Suisse

    Informations professionnelles :
    Activité : piano
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Décembre 2011
    Messages : 131
    Par défaut


    Super ! j'ai déplacé le while après le count et ça fonctionne

    Encore une chose apprise et rangée dans mon petit cerveau

    Une fois de plus merci pour ton aide

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Plugin] Utiliser plusieurs fois je même jqgrid dans une page
    Par Christophe Charron dans le forum jQuery
    Réponses: 0
    Dernier message: 06/10/2012, 17h26
  2. [MediaWiki] Utiliser des variables externes dans une page wiki
    Par mayok dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 3
    Dernier message: 16/08/2011, 05h20
  3. Utiliser deux dll différentes dans une page aspx
    Par Sylvain_57 dans le forum Développement Sharepoint
    Réponses: 3
    Dernier message: 21/03/2011, 12h31
  4. utilisation de controles activex dans une page web
    Par raimo dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 02/05/2008, 16h24
  5. Utiliser un service web dans une page ASP
    Par REMACC1 dans le forum ASP
    Réponses: 2
    Dernier message: 30/08/2006, 10h17

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo