Skip to content

Instantly share code, notes, and snippets.

/index.php
Created Feb 2, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.