Loading data from MySQL to table horizontally

JoeDoe

I'm loading some data from MySQL database and it looks like this:

| Gray |  S  |
| Gray |  M  |
| Gray |  L  |
| Red  |  S  |
| Red  |  M  |
| Red  |  L  |

I want to load data to my table horizontally like this, grouped by color:

| Gray |  S  |  M  |  L  |  XL  |
| Red  |  S  |  M  |  L  |  XL  |

Here's my code:

<table>
<?php foreach($products as $product) {?>
    <tr>
    <td><?php echo $product['color'];?></td>
    <td><?php echo $product['size'];?></td>
    </tr>       
<?php   }
?>      
</table>

Question: how to load data to my table grouped by color per one line? Can I do this without separate MySQL query?

Scuzzy

A little bit of manual iteration would be your friend...

$DatabaseResult = [ ['Gray','S'],['Gray','M'],['Gray','L'],['Red','S'],['Red','M'],['Red','L'] ];

$Grouped = array_fill_keys( array_unique( array_column( $DatabaseResult, 0 ) ), array() );

foreach( $DatabaseResult as $row )
{
  $Grouped[ $row[0] ][] = $row[1];
}

print_r( $Grouped );

//Array
//(
//    [Gray] => Array
//        (
//            [0] => S
//            [1] => M
//            [2] => L
//        )
//
//    [Red] => Array
//        (
//            [0] => S
//            [1] => M
//            [2] => L
//        )
//)

You would then create two loops, the outer is your "colour" row an the inner is your "size" column

<table border="1">
  <?php foreach( $Grouped as $colour => $sizes ) {?>
    <tr>
      <td><?php echo $colour;?></td>
      </td>
      <?php foreach($sizes as $size) {?>
        <td><?php echo $size;?></td>
      <?php } ?>
    </tr>
  <?php } ?>
</table>

In addition, if you needed to print a size chart of availability you could do this...

$Products = [
  ['color'=>'Gray','size'=>'S','quantity'=>2],
  ['color'=>'Gray','size'=>'M','quantity'=>4],
  ['color'=>'Gray','size'=>'L','quantity'=>7],
  ['color'=>'Red','size'=>'S','quantity'=>8],
  ['color'=>'Red','size'=>'M','quantity'=>6],
  ['color'=>'Red','size'=>'XXL','quantity'=>1],
];

// Helper function to locate products from the array
function SearchProductArray( $products, $color, $size )
{
  foreach( $products as $product )
  {
    if( $product[ 'color' ] == $color and $product[ 'size' ] == $size )
    {
      return $product;
    }
  }
  return false;
}

// Locate all the unique Values
$Colours = array_unique( array_column( $Products, 'color' ) );
$Sizes = ['SS','S','M','L','XL','XXL'];

?>

<table border="1">
  <tr>
    <td>Colour</td>
    <?php foreach( $Sizes as $Size ) {?>
      <td><?php echo $Size;?></td>
    <?php } ?>
  </tr>
  <?php foreach( $Colours as $Colour ) {?>
    <tr>
      <td><?php echo $Colour;?></td>
      <?php foreach( $Sizes as $Size ) { $Product = SearchProductArray( $Products, $Colour, $Size ); ?>
        <td><?php echo ( $Product ) ? $Product['quantity'] : '&nbsp'; ?></td>
      <?php } ?>
    </tr>
  <?php } ?>
</table>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related