Page 1 sur 1

une boucle foreach, while ou ... ?

Posté : 22 juil. 2013, 18:39
par cheste
Bonjour,
Je cherche désespérément à afficher une liste de produit trié par catégorie
A cette heure j'ai ma requête qui me renvoie toutes les infos dont j'ai besoin et j'arrive à présenter tout ca dans un tableau.

Par contre pour "aérer" le tableau j'aimerai que la balise td catprod s'affiche une fois et qu'ensuite les produits de cette catégorie s'affiche.
J'ai essaye de faire ca avec des boucles foreach (que je maitrise mal) mais sans succès

Avez vous des pistes à me donner ?

le code :
        <?php
            mysql_query("SET NAMES 'utf8'");
            mysql_select_db($database_gestcostever, $gestcostever);
            $query_rselectlistprod = "SELECT tc.idtarclt, tc.idclt, tc.idprod, tc.infoprod, tc.pvuht, p.idprod, p.nomprod, p.idcattocat, p.refprod, cat.idcattocat, cat.descat FROM tarifclient tc, produit p, cattocat cat WHERE tc.idclt = '".$selectclt."' AND tc.idprod = p.idprod AND p.idcattocat = cat.idcattocat ORDER BY cat.descat, p.nomprod ASC";
            $rselectlistprod = mysql_query($query_rselectlistprod, $gestcostever) or die(mysql_error());
            $row_rselectlistprod = mysql_fetch_assoc($rselectlistprod);
            $totalRows_rselectlistprod = mysql_num_rows($rselectlistprod);
            
            ?>
            <table id="tabtar">
    
            <tr>
                  <td class="intitule">Référence</td>
                <td class="intitule">Produit</td>		
                <td class="intitule">PUHT</td>
            </tr>
             <?php 
				
				do{ 
                echo "<div class=\"blockprod\">";
				echo "<tr><td colspan=3 class=\"catprod\">".$row_rselectlistprod['descat']."</td>\n";
                echo "<tr><td class=\"refprod\">".$row_rselectlistprod['refprod']."</td>\n";
                echo "<td class=\"nomprod\">".$row_rselectlistprod['nomprod']."</td>\n";
                echo "<td class=\"prix\">".$row_rselectlistprod['pvuht']."</td></tr>\n";
                echo "<tr><td class=\"refrod\"></td>\n<td class=\"infoprod\">".$row_rselectlistprod['infoprod']."</td></tr></div>\n";
                }
                while ($row_rselectlistprod = mysql_fetch_assoc($rselectlistprod));
                $row=mysql_num_rows($rselectlistprod);
                if ($row > 0) {
                    mysql_data_seek($rselectlistprod, 0);
                    $row_rselectlistprod = mysql_fetch_assoc($rselectlistprod);
                    }
                    ?>
            </table>

Re: une boucle foreach, while ou ... ?

Posté : 22 juil. 2013, 19:02
par Zahnzao
Salut,

il suffit de tester le changement de catégorie dans ta boucle
$newCat="";
while(){
     if($newCat != $row_rselectlistprod['descat']){
             // tu affiche ton <td>
             $newCat=$row_rselectlistprod['descat'];
     }
}
Donc à la première boucle $newCat ne vaut rien donc forcément il affiche ton TD, si l'élément suivant fait partie de la même catégorie, $row_rselectlistprod['descat'] vaudra $newCat donc il ne l'affichera pas.

;)

Re: une boucle foreach, while ou ... ?

Posté : 22 juil. 2013, 19:33
par cheste
Zahnzao,

Tout d'abord merci de m'avoir répondu mais surtout un GRAND MERCI pour la solution