vediamo di fare un esempio:
devo estrarre quante categorie sono associate ad un marchio, marchio che ha anche un immagine e ovviamente un nome il tutto stampato all'interno di una tabella html, quindi
<table>
<tr>
<td>nome marchio: ($marchio)</td>
<td>categoria/e di appartenenza: while($categorie)</td>
<td>img marchio: ($img)</td>
</tr>
</table>
se inserisco tutto nel while
$sql = "SELECT * FROM tabella.....";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
echo $row['categoria'] . '
' . $row['img'] . '
' . $row['nome'];
}
mi stampa una tabella con ripetuti i nomi del marchio/immagini quante sono le categorie...mi spiego?
mentre io voglio che mi stampi nel while il numero di categorie e per 1 marchio e 1 immagine...
ritornando alla domanda iniziale: è possibile fare un ciclo while solo per $categorie e stampare il nome marchio e immagine una sola volta?
potrebbe essere che la query non fa quello che vorrei oppure il DB non è progettato bene, ma questo è un'altro discorso, nel senso che se con php riesco a ottenere quello che mi serve, ok, altrimenti trovo un altra strada
cmq questo è il codice:
che fatica....
Codice PHP:
$sql = "SELECT DISTINCT(cat_name), img_thumbnail, img_name, i.id_accessorio,i.cat_id FROM tbl_category AS c
JOIN tbl_id_item AS i
ON i.cat_id = c.cat_id
JOIN tbl_marchi AS m
ON m.id_accessorio = i.id_accessorio
ORDER BY img_name ASC";
$result = dbQuery(getPagingQuery($sql, $rowsPerPage));
$pagingLink = getPagingLink($sql, $rowsPerPage, $queryString); $categoryList = buildCategoryOptions($catId); ?>
<div style="text-align:center;margin:auto">
<form action="index.php?view=add_category" method="post" name="frmListProduct" id="frmListProduct">
<table class="tabella">
<tr>
<th>Nome Marchio</th>
<th>Categoria</th>
<th>Immagine</th>
<th>Modifica</th>
<th>Cancella</th>
</tr>
<?php
$parentId = 0;
if (dbNumRows($result) > 0) {
$i = 0;
while($row = dbFetchAssoc($result)) {
extract($row);
if ($img_thumbnail) {
$img_thumbnail = WEB_ROOT . 'images/product/' . $img_thumbnail;
} else {
$img_thumbnail = WEB_ROOT . 'images/no-image-small.gif'; }
if ($i%2) {
$class = 'row1';
} else {
$class = 'row2';
}
$i += 1; ?>
<tr class="<?php echo $class; ?>">
<td class="invio"><?php echo $img_name; ?></td>
<td class="invio"><?php echo $cat_name; ?></td>
<td class="invio">[img]<?php echo $img_thumbnail; ?>[/img]</td>
<td class="invio"><a href="javascript:modifyMarchio(<?php echo $id_accessorio; ?>,
<?php echo $cat_id; ?>);">Modifica</a></td> <td class="invio">[url="javascript:deleteCatMarchio(<?php echo $id_accessorio; ?>, <?php echo $cat_id; ?>);"]Elimina[/url]</td> </tr>
anche qui, tutto è nel while come avete proposto voi, la modifica che vorrei fare è quella della domanda....si può fare o no?