mostrando categoría y subcategorías php y mysql

mysql_select_db($database_test2, $test2);
$query_Recordset1 = "Select   color.name As color,   item.name As Item,   typ.name As Typ,   color.id From   color Inner Join   item On color.id = item.color_id Inner Join   typ On typ.id = item.typ_id Group By   item.name, typ.name, color.id Order By   color.id, typ.name, item.name";
$Recordset1 = mysql_query($query_Recordset1, $test2) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<p><?php echo $row_Recordset1['color']; ?>
</p>
<p><?php echo $row_Recordset1['Typ']; ?></p>
<?php do { ?>
  <p><?php echo $row_Recordset1['Item']; ?></p>
  <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

i used the above code and gives this output which is not what i want to have

verde

metal

elemento 1

elemento 2

elemento 3

elemento 4

elemento 5

elemento 6

i would like to have it like this

verde

metal

.item 1 wood

.item 2

amarillo

metal

.item3

madera

.item 4

rojo

metal

.item5

madera

.item 6

can someone be kind to help me

muchas gracias

preguntado el 08 de noviembre de 11 a las 17:11

1 Respuestas

In your do... while loop you print only the item. Do you have any reason to use do..while instead of while loop?

Esto debería funcionar:

<body>
<?php while ( $row_Recordset1 = mysql_fetch_assoc($Recordset1) ) { ?>
   <p><?php echo $row_Recordset1['color']; ?></p>
   <p><?php echo $row_Recordset1['Typ']; ?></p>
   <p><?php echo $row_Recordset1['Item']; ?></p>
   <?php } ?>
</body>

And you will also have to remove the first mysql_fetch_assoc, which is before mysql_num_rows.

EDIT: Sorry the requirements were not clear when the text was all in one line. Since you get the rows ordered by color, you can output a color only if it is different from the previous:

<body>
<?php 
$color = '';
while ( $row_Recordset1 = mysql_fetch_assoc($Recordset1) ) {
   if ( $color != $row_Recordset1[ 'color' ]; ) {
       ?><p><?php echo $row_Recordset1['color']; ?></p><?php
       $color = $row_Recordset1[ 'color' ];
   }
   ?><p><?php echo $row_Recordset1['Typ']; ?></p>
   <p><?php echo $row_Recordset1['Item']; ?></p>
   <?php } ?>
</body>

respondido 09 nov., 11:06

using the above code it repeats the color value like green metal item 1 green wood item 2 - user1036077

Please help with the above code the out put includes the code itself - user1036077

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.