Skip to content

Instantly share code, notes, and snippets.

Created February 2, 2016 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/9fb96409918afc0f6191 to your computer and use it in GitHub Desktop.
Save anonymous/9fb96409918afc0f6191 to your computer and use it in GitHub Desktop.
Agrupar produtos por categoria.
<?php
$pdo = new PDO("mysql:host=localhost;dbname=loja", "root", "");
$sql = "SELECT p.id, p.name, c.name AS categoryName, c.id AS category_id FROM product p INNER JOIN category c ON c.product_id = p.id ORDER BY c.name ASC";
$results = $pdo->query($sql);
?>
<?php
$group = '';
foreach($results as $result) : ?>
<?php if($group !== $result['category_id']) : ?>
<h2><?php echo $result['categoryName']?></h2>
<?php $group = $result['category_id']?>
<?php endif;?>
<p><?php echo $result['name']?></p>
<?php endforeach;?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment