Created
February 2, 2016 11:12
-
-
Save anonymous/9fb96409918afc0f6191 to your computer and use it in GitHub Desktop.
Agrupar produtos por categoria.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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